1

The following code works nicely with iText 5:

LEVEL = PdfAConformanceLevel.PDF_A_3A
writer = PdfAWriter.getInstance(document, baos, LEVEL)

What is the correct syntax for iText 7?

Where can I find examples and documentation about iText 7?

4

1 回答 1

1

为什么这么消极?好像你想给我额外的压力来写文档。这有点令人沮丧。我认为鼓励效果更好。尽管如此,您声称没有文档是错误的。

请参阅第 7 章:创建iText 7PDF/UA 和 PDF/A 文档:快速入门教程

您忽略了UnitedStates_PDFA_3a示例,其中我们有一个 CSV 文件,我们将其转换为 PDF/A-3 文档,将该 CSV 文件作为附件添加。

public void createPdf(String dest) throws IOException, XMPException {
    PdfADocument pdf = new PdfADocument(new PdfWriter(dest),
        PdfAConformanceLevel.PDF_A_3A,
        new PdfOutputIntent("Custom", "", "http://www.color.org",
                "sRGB IEC61966-2.1", new FileInputStream(INTENT)));
    Document document = new Document(pdf, PageSize.A4.rotate());
    document.setMargins(20, 20, 20, 20);

    //Setting some required parameters
    pdf.setTagged();
    pdf.getCatalog().setLang(new PdfString("en-US"));
    pdf.getCatalog().setViewerPreferences(
            new PdfViewerPreferences().setDisplayDocTitle(true));
    PdfDocumentInfo info = pdf.getDocumentInfo();
    info.setTitle("iText7 PDF/A-3 example");

    //Add attachment
    PdfDictionary parameters = new PdfDictionary();
    parameters.put(PdfName.ModDate, new PdfDate().getPdfObject());
    PdfFileSpec fileSpec = PdfFileSpec.createEmbeddedFileSpec(
        pdf, Files.readAllBytes(Paths.get(DATA)), "united_states.csv",
        "united_states.csv", new PdfName("text/csv"), parameters,
        PdfName.Data, false);
    fileSpec.put(new PdfName("AFRelationship"), new PdfName("Data"));
    pdf.addFileAttachment("united_states.csv", fileSpec);
    PdfArray array = new PdfArray();
    array.add(fileSpec.getPdfObject().getIndirectReference());
    pdf.getCatalog().put(new PdfName("AF"), array);

    //Embed fonts
    PdfFont font = PdfFontFactory.createFont(FONT, true);
    PdfFont bold = PdfFontFactory.createFont(BOLD_FONT, true);

    // Create content
    Table table = new Table(new float[]{4, 1, 3, 4, 3, 3, 3, 3, 1});
    table.setWidthPercent(100);
    BufferedReader br = new BufferedReader(new FileReader(DATA));
    String line = br.readLine();
    process(table, line, bold, true);
    while ((line = br.readLine()) != null) {
        process(table, line, font, false);
    }
    br.close();
    document.add(table);

    //Close document
    document.close();
}

public void process(Table table, String line, PdfFont font, boolean isHeader) {
    StringTokenizer tokenizer = new StringTokenizer(line, ";");
    while (tokenizer.hasMoreTokens()) {
        if (isHeader) {
            table.addHeaderCell(new Cell().setHorizontalAlignment(HorizontalAlignment.CENTER).add(new Paragraph(tokenizer.nextToken()).setHorizontalAlignment(HorizontalAlignment.CENTER).setFont(font)));
        } else {
            table.addCell(new Cell().setHorizontalAlignment(HorizontalAlignment.CENTER).add(new Paragraph(tokenizer.nextToken()).setHorizontalAlignment(HorizontalAlignment.CENTER).setFont(font)));
        }
    }
}
于 2016-06-11T09:21:23.470 回答