/// <summary> /// This method updates the the xmp metadata of a given pdf file. /// </summary> /// <param name="sourceFilename">Name of the pdf source file.</param> /// <param name="destinationFilename">Name of the pdf destination file.</param> private ActionResult UpdateXMPMetadata(string sourceFilename, string destinationFilename) { var ms = new PDFMetadataStrings("", "", "", "", "", "", "", ""); var reader = new PdfReader(sourceFilename); var doc = new XmlDocument(); string xmlMetadataString = Encoding.UTF8.GetString(reader.Metadata); doc.LoadXml(xmlMetadataString); string pdfa = ""; if (xmlMetadataString.IndexOf("pdfaid:part='1'", StringComparison.InvariantCultureIgnoreCase) >= 0) { pdfa = "<pdfaid:part>1</pdfaid:part>\n"; } else if (xmlMetadataString.IndexOf("pdfaid:part='2'", StringComparison.InvariantCultureIgnoreCase) >= 0) { pdfa = "<pdfaid:part>2</pdfaid:part>\n"; } string pdfaConformance = ""; if (xmlMetadataString.IndexOf("pdfaid:conformance='A'", StringComparison.InvariantCultureIgnoreCase) >= 0) { pdfaConformance = "<pdfaid:conformance>A</pdfaid:conformance>\n"; } else if (xmlMetadataString.IndexOf("pdfaid:conformance='B'", StringComparison.InvariantCultureIgnoreCase) >= 0) { pdfaConformance = "<pdfaid:conformance>B</pdfaid:conformance>\n"; } var documentIDs = (PdfArray)(reader.Trailer.Get(PdfName.ID)); string sDocumentID = GetHEXString(GetRandomString(16)); if (documentIDs != null) { var o = (PdfObject)documentIDs.ArrayList[0]; string s = ""; s = o.ToString(); if (s.Length > 0) { sDocumentID = GetHEXString(s); } } string xmlAuthor = ""; if (reader.Info.ContainsKey("Author")) { ms.Author = reader.Info["Author"]; xmlAuthor = " <dc:creator>\n" + " <rdf:Seq>\n" + " <rdf:li>" + ms.Author + "</rdf:li>\n" + " </rdf:Seq>\n" + " </dc:creator>\n"; } if (reader.Info.ContainsKey("CreationDate")) { ms.CreationDate = reader.Info["CreationDate"]; } if (reader.Info.ContainsKey("Creator")) { ms.Creator = reader.Info["Creator"].ToString(); } string xmlKeywords = "", xmlKeywords2 = ""; if (reader.Info.ContainsKey("Keywords")) { ms.Keywords = reader.Info["Keywords"].ToString(); xmlKeywords = " <dc:subject>\n" + " <rdf:Bag>\n" + " <rdf:li>" + ms.Keywords + "</rdf:li>\n" + " </rdf:Bag>\n" + " </dc:subject>\n"; xmlKeywords2 = " <pdf:Keywords>" + ms.Keywords + "</pdf:Keywords>\n"; } if (reader.Info.ContainsKey("ModDate")) { ms.ModDate = reader.Info["ModDate"].ToString(); } if (reader.Info.ContainsKey("Producer")) { ms.Producer = reader.Info["Producer"].ToString(); } string xmlSubject = ""; if (reader.Info.ContainsKey("Subject")) { ms.Subject = reader.Info["Subject"].ToString(); xmlSubject = " <dc:description>\n" + " <rdf:Alt>\n" + " <rdf:li xml:lang='x-default'>" + ms.Subject + "</rdf:li>\n" + " </rdf:Alt>\n" + " </dc:description>\n"; } string xmlTitle = ""; if (reader.Info.ContainsKey("Title")) { ms.Title = reader.Info["Title"].ToString(); xmlTitle = " <dc:title>\n" + " <rdf:Alt>\n" + " <rdf:li xml:lang='x-default'>" + ms.Title + "</rdf:li>\n" + " </rdf:Alt>\n" + " </dc:title>\n"; } string metadataStr = ""; metadataStr = "<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>\n" + " <x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='Adobe XMP Core 4.2.1-c041 52.342996, 2008/05/07-20:48:00'>\n" + " <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n" + " <rdf:Description rdf:about='' xmlns:pdfaid='http://www.aiim.org/pdfa/ns/id/'>\n" + pdfa + pdfaConformance + " </rdf:Description>\n" + " <rdf:Description rdf:about=''\n" + " xmlns:xmp='http://ns.adobe.com/xap/1.0/'>\n" + " <xmp:CreateDate>" + GetXMPDate(ms.CreationDate) + "</xmp:CreateDate>\n" + " <xmp:ModifyDate>" + GetXMPDate(ms.ModDate) + "</xmp:ModifyDate>\n" + " <xmp:CreatorTool>" + ms.Creator + "</xmp:CreatorTool>\n" + " </rdf:Description>\n" + " <rdf:Description rdf:about=''\n" + " xmlns:dc='http://purl.org/dc/elements/1.1/'>\n" + " <dc:format>application/pdf</dc:format>\n" + xmlTitle + xmlSubject + xmlAuthor + xmlKeywords + " </rdf:Description>\n" + " <rdf:Description rdf:about=''\n" + " xmlns:xmpMM='http://ns.adobe.com/xap/1.0/mm/'\n" + " xmlns:stEvt='http://ns.adobe.com/xap/1.0/sType/ResourceEvent#'>\n" + " <xmpMM:DocumentID>uuid:" + sDocumentID + "</xmpMM:DocumentID>\n" + " <xmpMM:History><rdf:Seq><rdf:li rdf:parseType='Resource'></rdf:li></rdf:Seq></xmpMM:History>\n" + " </rdf:Description>\n" + " <rdf:Description rdf:about=''\n" + " xmlns:pdf='http://ns.adobe.com/pdf/1.3/'>\n" + " <pdf:Producer>" + ms.Producer + "</pdf:Producer>\n" + xmlKeywords2 + " </rdf:Description>\n" + " </rdf:RDF>\n" + " </x:xmpmeta>\n" + "<?xpacket end='w'?>"; Encoding textEncoding = Encoding.GetEncoding("iso-8859-1"); byte[] newMetadata = Encoding.Convert(Encoding.Default, Encoding.UTF8, textEncoding.GetBytes(metadataStr)); var os = new FileStream(destinationFilename, FileMode.Create); var stamper = new PdfStamper(reader, os); stamper.Writer.XmpMetadata = newMetadata; stamper.Close(); os.Close(); return(new ActionResult()); //////////////////////////////////////////////////////////////////////////////////////////////// }
private void DoUpdateXmpMetadata(PdfStamper stamper, ConversionProfile profile) { if ((profile.OutputFormat != OutputFormat.PdfA1B) && (profile.OutputFormat != OutputFormat.PdfA2B)) { return; } Logger.Debug("Start updateing XMP Metadata for PDF/A"); var ms = new PDFMetadataStrings("", "", "", "", "", "", "", ""); //var reader = new PdfReader(sourceFilename); var reader = stamper.Reader; var doc = new XmlDocument(); var xmlMetadataString = Encoding.UTF8.GetString(reader.Metadata); doc.LoadXml(xmlMetadataString); var pdfa = ""; if (xmlMetadataString.IndexOf("pdfaid:part='1'", StringComparison.InvariantCultureIgnoreCase) >= 0) { pdfa = "<pdfaid:part>1</pdfaid:part>\n"; } else if (xmlMetadataString.IndexOf("pdfaid:part='2'", StringComparison.InvariantCultureIgnoreCase) >= 0) { pdfa = "<pdfaid:part>2</pdfaid:part>\n"; } var pdfaConformance = ""; if (xmlMetadataString.IndexOf("pdfaid:conformance='A'", StringComparison.InvariantCultureIgnoreCase) >= 0) { pdfaConformance = "<pdfaid:conformance>A</pdfaid:conformance>\n"; } else if (xmlMetadataString.IndexOf("pdfaid:conformance='B'", StringComparison.InvariantCultureIgnoreCase) >= 0) { pdfaConformance = "<pdfaid:conformance>B</pdfaid:conformance>\n"; } var documentIDs = (PdfArray)reader.Trailer.Get(PdfName.ID); var sDocumentId = GetHexString(GetRandomString(16)); if (documentIDs != null) { var o = documentIDs.ArrayList[0]; var s = o.ToString(); if (s.Length > 0) { sDocumentId = GetHexString(s); } } var xmlAuthor = ""; if (reader.Info.ContainsKey("Author")) { ms.Author = reader.Info["Author"]; xmlAuthor = " <dc:creator>\n" + " <rdf:Seq>\n" + " <rdf:li>" + ms.Author + "</rdf:li>\n" + " </rdf:Seq>\n" + " </dc:creator>\n"; } if (reader.Info.ContainsKey("CreationDate")) { ms.CreationDate = reader.Info["CreationDate"]; } if (reader.Info.ContainsKey("Creator")) { ms.Creator = reader.Info["Creator"]; } string /*xmlKeywords = "",*/ xmlKeywords2 = ""; if (reader.Info.ContainsKey("Keywords")) { ms.Keywords = reader.Info["Keywords"]; /* * xmlKeywords = " <dc:subject>\n" + * " <rdf:Bag>\n" + * " <rdf:li>" + ms.Keywords + "</rdf:li>\n" + * " </rdf:Bag>\n" + * " </dc:subject>\n"; */ xmlKeywords2 = " <pdf:Keywords>" + ms.Keywords + "</pdf:Keywords>\n"; } if (reader.Info.ContainsKey("ModDate")) { ms.ModDate = reader.Info["ModDate"]; } if (reader.Info.ContainsKey("Producer")) { ms.Producer = reader.Info["Producer"]; } var xmlSubject = ""; if (reader.Info.ContainsKey("Subject")) { ms.Subject = reader.Info["Subject"]; xmlSubject = " <dc:description>\n" + " <rdf:Alt>\n" + " <rdf:li xml:lang='x-default'>" + ms.Subject + "</rdf:li>\n" + " </rdf:Alt>\n" + " </dc:description>\n"; } var xmlTitle = ""; if (reader.Info.ContainsKey("Title")) { ms.Title = reader.Info["Title"]; xmlTitle = " <dc:title>\n" + " <rdf:Alt>\n" + " <rdf:li xml:lang='x-default'>" + ms.Title + "</rdf:li>\n" + " </rdf:Alt>\n" + " </dc:title>\n"; } var conformance = GetPdfAConformance(profile.OutputFormat); var metadataStr = "<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>\n" + " <x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='Adobe XMP Core 4.2.1-c041 52.342996, 2008/05/07-20:48:00'>\n" + " <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n" + " <rdf:Description rdf:about='' xmlns:pdfaid='http://www.aiim.org/pdfa/ns/id/' pdfaid:part='" + conformance.Item1 + "' pdfaid:conformance='" + conformance.Item2 + "'>\n" + pdfa + pdfaConformance + " </rdf:Description>\n" + " <rdf:Description rdf:about=''\n" + " xmlns:xmp='http://ns.adobe.com/xap/1.0/'>\n" + " <xmp:CreateDate>" + GetXmpDate(ms.CreationDate) + "</xmp:CreateDate>\n" + " <xmp:ModifyDate>" + GetXmpDate(ms.ModDate) + "</xmp:ModifyDate>\n" + " <xmp:CreatorTool>" + ms.Creator + "</xmp:CreatorTool>\n" + " </rdf:Description>\n" + " <rdf:Description rdf:about=''\n" + " xmlns:dc='http://purl.org/dc/elements/1.1/'>\n" + " <dc:format>application/pdf</dc:format>\n" + xmlTitle + xmlSubject + xmlAuthor + //xmlKeywords + " </rdf:Description>\n" + " <rdf:Description rdf:about=''\n" + " xmlns:xmpMM='http://ns.adobe.com/xap/1.0/mm/'\n" + " xmlns:stEvt='http://ns.adobe.com/xap/1.0/sType/ResourceEvent#'>\n" + " <xmpMM:DocumentID>uuid:" + sDocumentId + "</xmpMM:DocumentID>\n" + " <xmpMM:History><rdf:Seq><rdf:li rdf:parseType='Resource'></rdf:li></rdf:Seq></xmpMM:History>\n" + " </rdf:Description>\n" + " <rdf:Description rdf:about=''\n" + " xmlns:pdf='http://ns.adobe.com/pdf/1.3/'>\n" + " <pdf:Producer>" + ms.Producer + "</pdf:Producer>\n" + xmlKeywords2 + " </rdf:Description>\n" + " </rdf:RDF>\n" + " </x:xmpmeta>\n" + "<?xpacket end='w'?>"; var textEncoding = Encoding.GetEncoding("iso-8859-1"); var newMetadata = Encoding.Convert(Encoding.Default, Encoding.UTF8, textEncoding.GetBytes(metadataStr)); stamper.Writer.XmpMetadata = newMetadata; }
private void DoUpdateXmpMetadata(PdfDocument pdfDocument, ConversionProfile profile, Job job) { var conformance = GetPdfAConformance(profile.OutputFormat); var xmlAuthor = ""; var xmlKeywords = ""; var xmlKeywords2 = ""; var xmlSubject = ""; var xmlTitle = ""; Logger.Debug("Start updateing XMP Metadata for PDF/A"); var ms = new PDFMetadataStrings("", "", "", "", "", "", "", ""); var documentIDs = pdfDocument.GetOriginalDocumentId(); string sDocumentId = GetHexString(GetRandomString(16)); if (documentIDs != null) { var o = documentIDs; string s = o.ToString(); if (s.Length > 0) { sDocumentId = GetHexString(s); } } if (!string.IsNullOrEmpty(job.JobInfo.Metadata.Author)) { ms.Author = job.JobInfo.Metadata.Author; xmlAuthor = " <dc:creator>\n" + " <rdf:Seq>\n" + " <rdf:li>" + ms.Author + "</rdf:li>\n" + " </rdf:Seq>\n" + " </dc:creator>\n"; } if (pdfDocument.GetDocumentInfo().GetMoreInfo("CreationDate") != null) { ms.CreationDate = pdfDocument.GetDocumentInfo().GetMoreInfo("CreationDate"); } if (!string.IsNullOrEmpty(job.Producer)) { ms.Creator = job.Producer; ms.Producer = job.Producer; } if (!string.IsNullOrEmpty(job.JobInfo.Metadata.Keywords)) { ms.Keywords = job.JobInfo.Metadata.Keywords; xmlKeywords = " <dc:subject>\n" + " <rdf:Bag>\n" + " <rdf:li>" + ms.Keywords + "</rdf:li>\n" + " </rdf:Bag>\n" + " </dc:subject>\n"; xmlKeywords2 = " <pdf:Keywords>" + ms.Keywords + "</pdf:Keywords>\n"; } if (pdfDocument.GetDocumentInfo().GetMoreInfo("ModDate") != null) { ms.ModDate = pdfDocument.GetDocumentInfo().GetMoreInfo("ModDate"); } if (!string.IsNullOrEmpty(job.JobInfo.Metadata.Subject)) { ms.Subject = job.JobInfo.Metadata.Subject; xmlSubject = " <dc:description>\n" + " <rdf:Alt>\n" + " <rdf:li xml:lang='x-default'>" + ms.Subject + "</rdf:li>\n" + " </rdf:Alt>\n" + " </dc:description>\n"; } if (!string.IsNullOrEmpty(job.JobInfo.Metadata.Title)) { ms.Title = job.JobInfo.Metadata.Title; xmlTitle = " <dc:title>\n" + " <rdf:Alt>\n" + " <rdf:li xml:lang='x-default'>" + ms.Title + "</rdf:li>\n" + " </rdf:Alt>\n" + " </dc:title>\n"; } string metadataStr = "<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>\n" + " <x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='Adobe XMP Core 4.2.1-c041 52.342996, 2008/05/07-20:48:00'>\n" + " <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n" + " <rdf:Description rdf:about='' xmlns:pdfaid='http://www.aiim.org/pdfa/ns/id/' pdfaid:part='" + conformance.Item1 + "' pdfaid:conformance='" + conformance.Item2 + "'>\n" + " </rdf:Description>\n" + " <rdf:Description rdf:about=''\n" + " xmlns:xmp='http://ns.adobe.com/xap/1.0/'>\n" + " <xmp:CreateDate>" + GetXmpDate(ms.CreationDate) + "</xmp:CreateDate>\n" + " <xmp:ModifyDate>" + GetXmpDate(ms.ModDate) + "</xmp:ModifyDate>\n" + " <xmp:CreatorTool>" + ms.Creator + "</xmp:CreatorTool>\n" + " </rdf:Description>\n" + " <rdf:Description rdf:about=''\n" + " xmlns:dc='http://purl.org/dc/elements/1.1/'>\n" + " <dc:format>application/pdf</dc:format>\n" + xmlTitle + xmlSubject + xmlAuthor + xmlKeywords + " </rdf:Description>\n" + " <rdf:Description rdf:about=''\n" + " xmlns:xmpMM='http://ns.adobe.com/xap/1.0/mm/'\n" + " xmlns:stEvt='http://ns.adobe.com/xap/1.0/sType/ResourceEvent#'>\n" + " <xmpMM:DocumentID>uuid:" + sDocumentId + "</xmpMM:DocumentID>\n" + " <xmpMM:History><rdf:Seq><rdf:li rdf:parseType='Resource'></rdf:li></rdf:Seq></xmpMM:History>\n" + " </rdf:Description>\n" + " <rdf:Description rdf:about=''\n" + " xmlns:pdf='http://ns.adobe.com/pdf/1.3/'>\n" + " <pdf:Producer>" + ms.Producer + "</pdf:Producer>\n" + xmlKeywords2 + " </rdf:Description>\n" + " </rdf:RDF>\n" + " </x:xmpmeta>\n" + "<?xpacket end='w'?>"; pdfDocument.SetXmpMetadata(new XMPMetaImpl(new XMPNode("XMP", metadataStr, new PropertyOptions()))); }