使用 iText 7 添加元数据

Ger*_*rry 2 .net itext

如何在 iText 7 (.Net) 中添加元数据(标题、作者等)。我发现的所有线程和示例似乎都使用旧格式

\n\n
document.addTitle(\xe2\x80\x9cTitle\xe2\x80\x9d); \n
Run Code Online (Sandbox Code Playgroud)\n\n

看来你可以\xe2\x80\x99t 在 iText 7 中执行此操作。

\n\n

谢谢

\n

Bru*_*gie 5

请查看第 7 章,更具体地查看XMP 元数据小节。在该小节中,您将找到以下示例:

public void createPdf(String dest) throws IOException {
    PdfDocument pdf = new PdfDocument(
        new PdfWriter(dest,
            new WriterProperties()
                .addXmpMetadata()
                .setPdfVersion(PdfVersion.PDF_1_6)));
    PdfDocumentInfo info = pdf.getDocumentInfo();
    info.setTitle("The Strange Case of Dr. Jekyll and Mr. Hyde");
    info.setAuthor("Robert Louis Stevenson");
    info.setSubject("A novel");
    info.setKeywords("Dr. Jekyll, Mr. Hyde");
    info.setCreator("A simple tutorial example");
    Document document = new Document(pdf);
    document.add(new Paragraph("Mr. Jekyl and Mr. Hyde"));
    document.close();
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,元数据不再直接添加到文档中,而是添加到PdfDocumentInfoPdfDocument实例获取的对象中。该PdfDocumentInfo对象用于创建 Info 字典(旧式元数据)以及 XMP 流(新式元数据)。仅当您addXmpMetadata()WriterProperties.

注意:由于在 PDF 2.0 中信息字典已被弃用,取而代之的是 XMP 元数据,因此这将在 iText 的未来版本中发生变化。在这些版本中,我们将优先使用 XMP,而不是使用 Info 字典。

  • 我知道你不应该在评论中说谢谢,但我已经搜索了很长时间并且完全错过了这一点。那谢谢啦。 (2认同)