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).