pdf - iText setting Creation Date & Modified Date in sandbox.stamper.SuperImpose.java -
i'm trying set creation date & modified date in superimposing content 1 pdf pdf example sandbox.stamper.superimpose.java.
the principle (i think) clear:
use getinfo()
& do
info.put(pdfname.creationdate, new pdfdate(calendar));
or
info.put("creationdate", "d:20160508090344+02'00'");
depending on whether hashmap<string, string>
or pdfdictionary available.
but where? can't quite seem find right place insert code... i'm having trouble overwriting original title attribute.
please take @ following files state.pdf , state_metadata.pdf.
the metadata of former looks this:
the metadata of latter looks this:
you can see title , dates have changed.
now take @ changemetadata example find out how 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 title easy:
info.put("title", "new title");
changing creation date requires use specific date format, why used pdfdate
object:
info.put("creationdate", new pdfdate().tostring());
old versions of itext may not allow creation date changed, please make sure you're using recent itext version.
the modification date changed automatically. current date used , can't override this.
the following lines change metadata in info dictionary:
map info = reader.getinfo(); info.put("title", "new title"); info.put("creationdate", new pdfdate().tostring()); stamper.setmoreinfo(info);
if use old version of adobe reader, see change, more recent pdf viewers give preference metadata stored in xmp metadata stream. means have create new xmp stream:
bytearrayoutputstream baos = new bytearrayoutputstream(); xmpwriter xmp = new xmpwriter(baos, info); xmp.close(); stamper.setxmpmetadata(baos.tobytearray());
when you've changed title in info dictionary , don't see change, should try changing xmp metadata too. pdf 2 different sets of metadata contradict each other considered being invalid pdf in cases (e.g. when need meet pdf/a compliance).
Comments
Post a Comment