如何使用iTextSharp将XMP元数据添加到现有PDF的每个页面 [英] How do I add XMP metadata to each page of an existing PDF using iTextSharp

查看:163
本文介绍了如何使用iTextSharp将XMP元数据添加到现有PDF的每个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,所有示例似乎都涉及在创建新文档时添加元数据。我想采用现有的PDF并使用压模为每个页面的XMP添加GUID

So far all the examples seem to involve adding metadata while creating a new Document. I want to take an existing PDF and add a GUID to each page's XMP using the stamper

推荐答案

XMP最常见的用途PDF的上下文是当您为从PDF的根词典(也称为目录)引用的整个文档添加XMP流时。

The most common use of XMP in the context of PDF is when you add an XMP stream for the whole document that is referred to from the root dictionary of the PDF (aka the Catalog).

但是,如果您可以参考PDF规范,您会注意到XMP可以在PDF中的许多其他对象的上下文中使用,页面级别就是其中之一。如果查看规范,您会发现 / Metadata 是页面字典中的可选键。它期望引用XMP流。

However, if you consult the PDF specification, you notice that XMP can be used in the context of many other objects inside a PDF, the page level being one of them. If you look at the spec, you will discover that /Metadata is an optional key in the page dictionary. It expects a reference to an XMP stream.

如果您要使用iText从头开始创建PDF文档,您将找不到添加此元数据的特定方法,但您可以使用 PdfWriter 中提供的通用 addPageDictEntry()。您将传递 PdfName.METADATA 作为键,并将对已添加到 PdfWriter 的流的引用作为值。

If you would use iText to create a PDF document from scratch, you wouldn't find a specific method to add this metadata, but you could use the generic addPageDictEntry() that is available in PdfWriter. You would pass PdfName.METADATA as key and a reference to a stream that is already added to the PdfWriter as value.

您的问题不是从头开始创建PDF,而是关于修改现有PDF。在这种情况下,您还需要页面字典。获取这些词典非常容易:

Your question isn't about creating a PDF from scratch, but about modifying an existing PDF. In that case, you also need the page dictionary. It is very easy to obtain these dictionaries:

PdfReader reader = new PdfReader(src);
int n = reader.getNumberOfPages();
PdfDictionary page;
for (int p = 1; p <= n; p++) {
    page = reader.getPageN(p);
    // do stuff with the page dictionary
}

这个片段被拍了来自 Rotate90Degrees 的示例。在该示例中,我们查看 / Rotate 条目,这是一个数字:

This snippet was taken fro the Rotate90Degrees example. In that example, we look at the /Rotate entry which is a number:

PdfNumber rotate = page.getAsNumber(PdfName.ROTATE);

您需要查找 /元数据 entry,这是一个流:

You need to look for the /Metadata entry, which is a stream:

PRStream stream = (PRStream) page.getAsStream(PdfName.METADATA);

也许这个流是 null ,在此例如,您需要添加一个 / Metadata 条目,如 AddXmpToPage 示例:

Maybe this stream is null, in this case, you need to add a /Metadata entry as is shown in the AddXmpToPage example:

// We create some XMP bytes
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XmpWriter xmp = new XmpWriter(baos, new PdfDictionary());
xmp.close();
// We add the XMP bytes to the writer
PdfIndirectObject ref = stamper.getWriter().addToBody(new PdfStream(baos.toByteArray()));
// We add a reference to the XMP bytes to the page dictionary
page.put(PdfName.METADATA, ref.getIndirectReference());

如果有XMP流,您希望保留它并向其中添加内容。

If there is an XMP stream, you want to keep it and add something to it.

这是你得到XMP字节的方式:

This is how you get the XMP bytes:

byte[] xmpBytes = PdfReader.getStreamBytes(stream);

您在这些字节上执行XML魔术,从而产生一个新的 byte [] 名为newXmpBytes。用这些新字节替换原始字节,如下所示:

You perform your XML magic on these bytes, resulting a a new byte[] named newXmpBytes. You replace the original bytes with these new bytes like this:

stream.setData(newXmpBytes);

所有这些操作都在位于 PdfReader <的现有文件上完成/ code>对象。你现在必须坚持这样的改变:

All these operations are done on the existing file that resides in the PdfReader object. You now have to persist the changes like this:

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
reader.close();

这篇关于如何使用iTextSharp将XMP元数据添加到现有PDF的每个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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