iText设置创建日期& sandbox.stamper.SuperImpose.java中的修改日期 [英] iText setting Creation Date & Modified Date in sandbox.stamper.SuperImpose.java

查看:153
本文介绍了iText设置创建日期& sandbox.stamper.SuperImpose.java中的修改日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置创建日期&



后者的元数据如下所示:





您可以看到标题和日期已更改。



现在看一下 ChangeMetadata 示例,了解如何完成此操作:

  public void manipulatePdf(String src,String dest)抛出IOException,DocumentException {
PdfReader reader = new PdfReader(src) );
PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(dest));
Map info = reader.getInfo();
info.put(标题,新标题);
info.put(CreationDate,new PdfDate()。toString());
stamper.setMoreInfo(info);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XmpWriter xmp = new XmpWriter(baos,info);
xmp.close();
stamper.setXmpMetadata(baos.toByteArray());
stamper.close();
reader.close();
}

更改标题很简单:

  info.put(标题,新标题); 

更改创建日期需要您使用特定的日期格式,这就是我使用<$的原因c $ c> PdfDate 对象:

  info.put(CreationDate,新的PdfDate()的ToString()); 

旧版iText可能不允许更改创建日期,所以请确保你是使用最新的iText版本。



修改日期会自动更改。使用当前日期,您无法覆盖此日期。



以下行仅更改信息词典中的元数据:

  Map info = reader.getInfo(); 
info.put(标题,新标题);
info.put(CreationDate,new PdfDate()。toString());
stamper.setMoreInfo(info);

如果您使用的是旧版Adobe Reader,您会看到更改,但更新的PDF查看器优先考虑存储在XMP元数据流中的元数据。这意味着您还必须创建一个新的XMP流:

  ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
XmpWriter xmp = new XmpWriter(baos,info);
xmp.close();
stamper.setXmpMetadata(baos.toByteArray());

当你说你在信息词典中更改了标题而你没看到更改时,您应该尝试更改XMP元数据。在某些情况下(例如,当您需要满足PDF / A合规性时),具有两组彼此矛盾的不同元数据的PDF被视为无效PDF。


I'm trying to set Creation Date & Modified Date in the Superimposing content from one PDF into another PDF example sandbox.stamper.SuperImpose.java.

The principle is (I think) clear:

use getInfo() & then do

info.put(PdfName.CREATIONDATE, new PdfDate(calendar));

or

info.put("CreationDate", "D:20160508090344+02'00'");

depending on whether a HashMap<String, String> or PdfDictionary is available.

But where? I just can't quite seem to find the right place to insert the code... I'm also having trouble overwriting the original Title attribute.

解决方案

Please take a look at the following files state.pdf and state_metadata.pdf.

The metadata of the former looks like this:

The metadata of the latter looks like this:

You can see that the title and the dates have changed.

Now take a look at the ChangeMetadata example to find out how this was done:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    Map info = reader.getInfo();
    info.put("Title", "New title");
    info.put("CreationDate", new PdfDate().toString());
    stamper.setMoreInfo(info);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XmpWriter xmp = new XmpWriter(baos, info);
    xmp.close();
    stamper.setXmpMetadata(baos.toByteArray());
    stamper.close();
    reader.close();
}

Changing the title is easy:

info.put("Title", "New title");

Changing the creation date requires that you use a specific date format, which is why I used the PdfDate object:

info.put("CreationDate", new PdfDate().toString());

Old versions of iText may not allow the creation date to be changed, so please make sure you're using a recent iText version.

The modification date is changed automatically. The current date is used and you can't override this.

The following lines only change the metadata in the Info dictionary:

Map info = reader.getInfo();
info.put("Title", "New title");
info.put("CreationDate", new PdfDate().toString());
stamper.setMoreInfo(info);

If you use an old version of Adobe Reader, you will see the change, but more recent PDF viewers give preference to the metadata stored in the XMP metadata stream. This means that you also have to create a new XMP stream:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
XmpWriter xmp = new XmpWriter(baos, info);
xmp.close();
stamper.setXmpMetadata(baos.toByteArray());

When you say that you've changed the title in the info dictionary and that you don't see the change, you should try changing the XMP metadata too. A PDF with two different sets of metadata that contradict each other is considered being an invalid PDF in some cases (e.g. when you need to meet PDF/A compliance).

这篇关于iText设置创建日期&amp; sandbox.stamper.SuperImpose.java中的修改日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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