9

I am trying to use iTextSharp to read/modify PDF metadata. I figured out how to do it using pdfreader and pdfstamper. I was wondering if I could also read/modify additional metadata information like copyright information and few others within the XMP photoshop namespace.

I would greatly appreciate any pointers to the solution.

Thank you, Murugesh.

muruge
  • 4,083
  • 3
  • 38
  • 45

2 Answers2

16

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();
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • Thanks for the response. It works. I am able to put values to any schema within XMP now. But whenever I insert a value other than the common metadata fields (Author, Title, Subject, Keywords) it adds them as a custom field which goes under "pdfx" schema in addition to the schema where I am inserting it. I don't want them to be added as custom fields. Any pointers? Thanks, Murugesh. – muruge May 06 '10 at 01:55
  • @muruge - No sorry I don't have any pointers; I've never tried what you're doing. I also couldn't find much on the iTextSharp.text.xml.xmp namespace. – Jay Riggs May 06 '10 at 06:13
  • Hi, Has anyone got a basic C# example of adding MetaData? I cant seem yto convert this to C#.Thanks. – Mark Redman Jan 10 '12 at 05:55
7

Try the examples in the iTextSharp book there are examples on modifying any part of the pdf file!

Gerhard
  • 6,850
  • 8
  • 51
  • 81
Majd
  • 1,358
  • 3
  • 15
  • 29
  • 1
    Thanks for sharing that. I am already done with this application. But hopefully this will help someone who is looking for an answer to this question. – muruge Mar 02 '11 at 15:33
  • 1
    Link dead. To be expected after 11 years. That's why there are rules about link only answers – Steve Jan 30 '22 at 22:00
  • Thanks for the update @Gerhard, appreciate your effort. – Steve Jan 31 '22 at 07:58