How to set copyright metadata of an existing PDF using iTextSharp for C#
How can the copyright metadata of an existing (i.e. a pdf loaded from file or memory stre开发者_如何学Pythonam) pdf file be set using iTextSharp for C#?
Thanks a lot
The native XMP structures don't have copyright implemented (or at least they don't in a way that Adobe Reader recognizes.) To do that you can reverse engineer what Adobe kicks out and write it manually:
String inputPDF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Services.pdf");
String outputPDF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Services_Out.pdf");
PdfReader reader = new PdfReader(inputPDF);
using (FileStream fs = new FileStream(outputPDF, FileMode.Create, FileAccess.Write, FileShare.Read))
{
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
using (MemoryStream ms = new MemoryStream())
{
string CopyrightName = "YOUR NAME HERE";
string CopyrightUrl = "http://www.example.com/";
XmpWriter xmp = new XmpWriter(ms);
xmp.AddRdfDescription("xmlns:dc=\"http://purl.org/dc/elements/1.1/\"", String.Format("<dc:rights><rdf:Alt><rdf:li xml:lang=\"x-default\">{0}</rdf:li></rdf:Alt></dc:rights>", CopyrightName));
xmp.AddRdfDescription("xmlns:xmpRights=\"http://ns.adobe.com/xap/1.0/rights/\"", string.Format("<xmpRights:Marked>True</xmpRights:Marked><xmpRights:WebStatement>{0}</xmpRights:WebStatement>", CopyrightUrl));
xmp.Close();
stamper.XmpMetadata = ms.ToArray();
stamper.Close();
}
}
}
精彩评论