Wednesday, 20 September 2017

Read/Modify PDF Metadata using iTextSharp

You can read metadata using `PdfReader'. I've read metadata like this:
PdfReader reader = new PdfReader("HelloWorldNoMetadata.pdf");
string s = reader.Info["Author"];
You can try the iTextSharp.text.xml.xmp.XmpWriter class to write metadata. I've never done it but I found this code below:
PdfReader reader = new PdfReader("HelloWorldNoMetadata.pdf");
PdfStamper stamper = new PdfStamper(reader,
 new FileOutputStream("HelloWorldStampedMetadata.pdf"));
HashMap info = reader.getInfo();
info.put("Author", "Bruno Lowagie");
info.put("Title", "Hello World stamped");
stamper.setMoreInfo(info);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XmpWriter xmp = new XmpWriter(baos, info);
xmp.close();
stamper.setXmpMetadata(baos.toByteArray());
stamper.close();

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Blog Archive