How to set metadata metadata of an existing PDF using iTextSharp for C #

How can I set copyright metadata to an existing (i.e. pdf file downloaded from a file or data stream) pdf file using iTextSharp for C #?

thanks a lot

+4
source share
1 answer

XMP's own structures do not have copyright (or at least they are not recognized by Adobe Reader.) To do this, you can reverse engineer what Adobe will release and write 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(); } } } 
+2
source

Source: https://habr.com/ru/post/979138/


All Articles