Read/Modify PDF Metadata using iTextSharp
Asked Answered
D

2

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.

Dilapidate answered 3/5, 2010 at 22:30 Comment(0)
B
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();
Betancourt answered 4/5, 2010 at 23:57 Comment(3)
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.Dilapidate
@Dilapidate - 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.Betancourt
Hi, Has anyone got a basic C# example of adding MetaData? I cant seem yto convert this to C#.Thanks.Mekong
T
7

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

Thromboplastic answered 1/3, 2011 at 11:39 Comment(3)
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.Dilapidate
Link dead. To be expected after 11 years. That's why there are rules about link only answersAvelinaaveline
Thanks for the update @Gerhard, appreciate your effort.Avelinaaveline

© 2022 - 2024 — McMap. All rights reserved.