2

我正在尝试使用 iText 5.5.2 创建一个 pdf/a 1a 文档

我可以使用 hello world 创建一个简单的 pdf/a,但我无法在文档中添加图像而不会出现错误:

com.itextpdf.text.pdf.PdfAConformanceException: Alt entry should specify alternate description for /Figure element.

下面是我的代码试用。我不知道如何为图像添加 Alt 条目 Figure 。我尝试使用 PdfDictionary,但它不起作用。我没主意了。有人给我小费吗?

final float MARGIN_OF_ONE_CM = 28.8f;
    final com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4
        , MARGIN_OF_ONE_CM
        , MARGIN_OF_ONE_CM
        , MARGIN_OF_ONE_CM
        , MARGIN_OF_ONE_CM);
    ByteArrayOutputStream pdfAsStream = new ByteArrayOutputStream();
    PdfAWriter writer = PdfAWriter.getInstance(document,
        new FileOutputStream("D:\\tmp\\pdf\\test.pdf"), PdfAConformanceLevel.PDF_A_1A);
    document.addAuthor("Author");
    document.addSubject("Subject");
    document.addLanguage("nl-nl");
    document.addCreationDate();
    document.addCreator("Creator");
    document.addTitle("title");

    writer.setPdfVersion(PdfName.VERSION);
    writer.setTagged();
    writer.createXmpMetadata();
    document.open();

    final String FONT = "./src/main/resources/fonts/arial.ttf";
    Font font = FontFactory.getFont(FONT, BaseFont.CP1252, BaseFont.EMBEDDED);

    final Paragraph element = new Paragraph("Hello World", font);
    document.add(element);

    final InputStream logo = this.getClass().getResourceAsStream("/logos/logo.jpg");
    final byte[] bytes = IOUtils.toByteArray(logo);
    Image logoImage = Image.getInstance(bytes);
    document.add(logoImage);

    final String colorProfile = "/color/sRGB Color Space Profile.icm";
    final InputStream resourceAsStream = this.getClass().getResourceAsStream(colorProfile);
    ICC_Profile icc = ICC_Profile.getInstance(resourceAsStream);
    writer.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);

    document.close();
4

1 回答 1

1

通常,您需要像这样添加备用描述:

logoImage.setAccessibleAttribute(PdfName.ALT, new PdfString("Logo"));

这适用于 PDF/A2-A 和 PDF/A3-A,但不适用于 PDF/A1-A。我对此进行了测试,发现您发现了一个错误。此错误现已修复。修复将在下一个版本中。

于 2014-12-04T12:46:38.333 回答