获取并设置itext pdf文档的元数据 [英] Get and set metadata for itext pdf document
问题描述
我有一个iText 文档
对象,我想在其中写入一些元数据或从中读取。
我该怎么做?
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.
您不应该从文档
对象中读取元数据。
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屋!