使用自定义属性从PDF添加/删除/检索信息 [英] Add / delete / retrieve information from a PDF using a custom property

查看:304
本文介绍了使用自定义属性从PDF添加/删除/检索信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起我昨天发布的不清楚的问题。我想在自定义属性中存储2个字符串(string1和string2),以便字符串与pdf.I已经有字符串,但我不知道如何将它存储在自定义属性区域下。自定义属性区域引用FILE ---> Properties ----> Custom ----> Custom属性,它们成对获得Name和Value。我希望Value中的string1存储和Name中的string2存储。

Sorry for the unclear question yesterday i post. I want to store 2 strings(string1 and string2) in the custom property,so that the string is with the pdf.I have the string already, but i do not know how to store it under the custom property area. The custom property area refers to FILE--->Properties---->Custom ---->Custom properties , which got "Name" and "Value" in pairs. I want the string1 stores in the "Value" and string2 stores in the "Name".

稍后,我想检索/删除自定义属性区域中的字符串。
请问如何用itext实现它?

Later, I want to retrieve/delete the strings in the custom property area. May I ask how to achieve it with itext?

谢谢和问候,

Brian

推荐答案

感谢您的澄清。根据您对如何使用Acrobat添加所需数据的说明,我们现在知道您指的是元数据。

Thank you for clarifying. Based on your description on how to add the data you want using Acrobat, we now know that you were referring to metadata.

我使用Acrobat添加自定义元数据名为Test的条目,其值为test,当您查看该文件时,您可以看到此键/值对出现在两个位置(标有红点):

I've used Acrobat to add a custom metadata entry named "Test" with value "test" and when you look inside that file, you can see that this key/value pair turns up on two places (marked with a red dot):


  1. 它出现在信息词典中,它是存储元数据的传统位置。

  2. 它存在于XMP元数据流中作为名为Test的标签,带有前缀pdfx(用于自定义标签)。

  1. It is present in the Info dictionary, which is the traditional place to store metadata.
  2. It is present in the XMP metadata stream as a tag named Test with prefix pdfx (for custom tags).

使用iText时,可以轻松地向信息词典添加额外的值。也可以更新XMP元数据,但是您必须自己创建XMP流,并且可能在您的情况下过度。也许你的PDF只有一个信息字典,没有XMP。

Adding an extra value to the Info dictionary is easy when using iText. Updating the XMP metadata is also possible, but you'll have to create the XMP stream yourself and maybe it's overkill in your case. Maybe your PDF only has an Info dictionary and no XMP.

此外:你说拥有该密钥的目的是检索其值并在之后删除自定义条目。在这种情况下,在信息词典中添加额外条目就足够了。

Moreover: you say that the purpose of having that key is to retrieve its value and to delete the custom entry afterwards. In that case, it's sufficient to add the extra entry in the Info dictionary.

取决于您是否要将信息字典中的自定义条目添加到从中创建的PDF您需要以下示例中的一个或现有PDF:

Depending on whether you want to add a custom entry to the Info dictionary to a PDF created from scratch or to an existing PDF you need one of the following examples:

CustomMetaEntry ,我们为标题和名为Test的自定义条目添加标准元数据条目:

In CustomMetaEntry, we add a standard metadata entry for the title and a custom entry named Test:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.addTitle("Some example");
    document.add(new Header("Test", "test"));
    document.open();
    Paragraph p = new Paragraph("Hello World");
    document.add(p);
    document.close();
}

如你所见,iText有 addX() 添加标题,作者,...元数据的方法。但是,如果要添加自定义条目,则需要使用 add()方法添加标题实例。您需要在打开文档之前添加元数据

As you can see, iText had addX() methods to add Title, Author,... metadata. However, if you want to add a custom entry, you need to use the add() method to add a Header instance. You need to add the metadata before opening the document.

如果要将条目添加到现有PDF的信息词典中,您可以以我的书籍为例,例如 MetadataPdf

If you want to add entries to the info dictionary of an existing PDF, you can take an example from my book, for instance MetadataPdf:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    Map<String, String> info = reader.getInfo();
    info.put("Title", "Hello World stamped");
    info.put("Subject", "Hello World with changed metadata");
    info.put("Keywords", "iText in Action, PdfStamper");
    info.put("Creator", "Silly standalone example");
    info.put("Author", "Also Bruno Lowagie");
    stamper.setMoreInfo(info);
    stamper.close();
    reader.close();
}

在这个例子中,我们从 PdfReader 实例使用 getInfo()方法。

In this example, we get the info dictionary from a PdfReader instance using the getInfo() method.

这也回答了如何从PDF中检索自定义数据。如果 Map 包含一个键为 Test 的条目,您可以得到如下值:

This also answers how to retrieve the custom data from a PDF. If the Map contains an entry with key Test, you can get its value like this:

String test = info.get("Test");

您现在可以添加额外的字符串这个地图
在示例中,我们为元数据添加标准密钥,但您也可以使用自定义密钥。

You can now add extra pairs of Strings to this Map. In the example, we add standard keys for metadata, but you can also use custom keys.

从现有PDF文件中删除条目是在与添加条目相同的方式。添加 null 值就足够了。例如:

Removing an entry from an existing PDF file is done in the same way as adding an entry. It's sufficient to add a null value. For instance:

info.put("Test", null);

这将删除一个名为 Test的自定义条目如果您的信息词典中存在此类值。

This will remove a custom entry named Test in case such a value was present in your Info dictionary.

这篇关于使用自定义属性从PDF添加/删除/检索信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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