获取和设置 itext pdf 文档的元数据 [英] Get and set metadata for itext pdf document

查看:59
本文介绍了获取和设置 itext pdf 文档的元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 iText Document 对象,我想将一些元数据写入其中或从中读取.
我怎样才能做到这一点?

I have an iText Document object and I want to write some metadata into it or read from it.
How can I do that?

想象一下,文档被传递给一个方法,如:

Imagine that the document is beeing passed to a method like :

public void prePreccess(Object document) {
    Document pdfDocument =   ((Document) document);
    //What to do here with pdfDocument?
}

推荐答案

是否要填充 PDF 的信息字典?这在 MetadataPdf 示例中进行了解释:

Do you want to populate the info dictionary of a PDF? That's explained in the MetadataPdf example:

// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.addTitle("Hello World example");
document.addAuthor("Bruno Lowagie");
document.addSubject("This example shows how to add metadata");
document.addKeywords("Metadata, iText, PDF");
document.addCreator("My program using iText");
document.open();
// step 4
document.add(new Paragraph("Hello World"));
// step 5
document.close();

您要设置 XMP 元数据吗?这在 MetadataXmp 示例中进行了解释:

Do you want to set the XMP metadata? This is explained in the MetadataXmp example:

// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT1));
ByteArrayOutputStream os = new ByteArrayOutputStream();
XmpWriter xmp = new XmpWriter(os);
XmpSchema dc = new com.itextpdf.text.xml.xmp.DublinCoreSchema();
XmpArray subject = new XmpArray(XmpArray.UNORDERED);
subject.add("Hello World");
subject.add("XMP & Metadata");
subject.add("Metadata");
dc.setProperty(DublinCoreSchema.SUBJECT, subject);
xmp.addRdfDescription(dc);
PdfSchema pdf = new PdfSchema();
pdf.setProperty(PdfSchema.KEYWORDS, "Hello World, XMP, Metadata");
pdf.setProperty(PdfSchema.VERSION, "1.4");
xmp.addRdfDescription(pdf);
xmp.close();
writer.setXmpMetadata(os.toByteArray());
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World"));
// step 5
document.close();

请注意,此方法已被弃用:我们最近替换了 XMP 功能,但我们仍然需要使用新代码编写一些示例.

Note that this method is deprecated: we have replaced the XMP functionality recently, but we still have to write some examples using the new code.

也许您想设置填充信息字典并同时创建 XMP 元数据:

Maybe you want to set populate the info dictionary and create the XMP metadata at the same time:

// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
document.addTitle("Hello World example");
document.addSubject("This example shows how to add metadata & XMP");
document.addKeywords("Metadata, iText, step 3");
document.addCreator("My program using 'iText'");
document.addAuthor("Bruno Lowagie");
writer.createXmpMetadata();
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World"));
// step 5
document.close();

如果我是你,我会使用这个选项,因为它是最完整的解决方案.

If I were you, I'd use this option because it's the most complete solution.

您不应从 Document 对象读取元数据.

You should not read the metadata from a Document object.

您可以像这样从现有的 PDF 中读取 XMP 流:

You can read the XMP stream from an existing PDF like this:

public void readXmpMetadata(String src, String dest) throws IOException {
    PdfReader reader = new PdfReader(src);
    FileOutputStream fos = new FileOutputStream(dest);
    byte[] b = reader.getMetadata();
    fos.write(b, 0, b.length);
    fos.flush();
    fos.close();
    reader.close();
}

您可以像这样阅读信息字典中的条目:

You can read the entries in the info dictionary like this:

PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Map<String, String> info = reader.getInfo();

info 对象将包含一系列作为元数据存储在 PDF 中的键和值.

The info object will contain a series of keys and values that are stored as metadata inside the PDF.

这篇关于获取和设置 itext pdf 文档的元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆