virtual public void CreatePdfTest() { String fileName = "xmp_metadata.pdf"; // step 1 Document document = new Document(); // step 2 PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(OUT_FOLDER + fileName, FileMode.Create)); MemoryStream os = new MemoryStream(); XmpWriter xmp = new XmpWriter(os, XmpWriter.UTF16, 2000); DublinCoreProperties.AddSubject(xmp.XmpMeta, "Hello World"); DublinCoreProperties.AddSubject(xmp.XmpMeta, "XMP & Metadata"); DublinCoreProperties.AddSubject(xmp.XmpMeta, "Metadata"); PdfProperties.SetKeywords(xmp.XmpMeta, "Hello World, XMP & Metadata, Metadata"); PdfProperties.SetVersion(xmp.XmpMeta, "1.4"); xmp.Close(); writer.XmpMetadata = os.ToArray(); // step 3 document.Open(); // step 4 document.Add(new Paragraph("Hello World")); // step 5 document.Close(); CompareResults(fileName, fileName); }
virtual public void DeprecatedLogicTest() { String fileName = "xmp_metadata_deprecated.pdf"; // step 1 Document document = new Document(); // step 2 PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(OUT_FOLDER + fileName, FileMode.Create)); MemoryStream os = new MemoryStream(); XmpWriter xmp = new XmpWriter(os); XmpSchema dc = new DublinCoreSchema(); XmpArray subject = new XmpArray(XmpArray.UNORDERED); subject.Add("Hello World"); subject.Add("XMP & Metadata"); subject.Add("Metadata"); dc.SetProperty(DublinCoreSchema.SUBJECT, subject); xmp.AddRdfDescription(dc.Xmlns, dc.ToString()); PdfSchema pdf = new PdfSchema(); pdf.AddKeywords("Hello World, XMP & Metadata, Metadata"); pdf.AddVersion("1.4"); xmp.AddRdfDescription(pdf); xmp.Close(); writer.XmpMetadata = os.ToArray(); // step 3 document.Open(); // step 4 document.Add(new Paragraph("Hello World")); // step 5 document.Close(); CompareResults("xmp_metadata.pdf", fileName); }
virtual public void ManipulatePdfTest() { String fileName = "xmp_metadata_added.pdf"; PdfReader reader = new PdfReader(CMP_FOLDER + "pdf_metadata.pdf"); PdfStamper stamper = new PdfStamper(reader, new FileStream(OUT_FOLDER + fileName, FileMode.Create)); Dictionary <String, String> info = reader.Info; MemoryStream baos = new MemoryStream(); XmpWriter xmp = new XmpWriter(baos, info); xmp.Close(); stamper.XmpMetadata = baos.ToArray(); stamper.Close(); reader.Close(); CompareResults(fileName, fileName); }
virtual public void XmpEncodingTest() { String fileName = "xmp_UTF-16BE-encoding"; Document document = new Document(); PdfSmartCopy copy = new PdfSmartCopy(document, new FileStream(OUT_FOLDER + fileName, FileMode.Create)); document.Open(); PdfReader reader = new PdfReader(CMP_FOLDER + "pdf_metadata.pdf"); int pageCount = reader.NumberOfPages; for (int currentPage = 1; currentPage <= pageCount; currentPage++) { PdfImportedPage page = copy.GetImportedPage(reader, currentPage); copy.AddPage(page); } MemoryStream os = new MemoryStream(); XmpWriter xmp = new XmpWriter(os, XmpWriter.UTF16, 2000); DublinCoreProperties.AddSubject(xmp.XmpMeta, "Hello World"); DublinCoreProperties.AddSubject(xmp.XmpMeta, "XMP & Metadata"); DublinCoreProperties.AddSubject(xmp.XmpMeta, "Metadata"); xmp.Close(); copy.XmpMetadata = os.ToArray(); string metadataXml = System.Text.Encoding.GetEncoding("UTF-16BE").GetString(copy.XmpMetadata); XmlDocument doc = new XmlDocument(); doc.LoadXml(metadataXml); //<-- This is where the exception is thrown document.Close(); copy.Close(); reader.Close(); }
// --------------------------------------------------------------------------- /** * Creates a PDF document. */ public byte[] CreatePdf() { using (MemoryStream ms = new MemoryStream()) { // step 1 using (Document document = new Document()) { // step 2 PdfWriter writer = PdfWriter.GetInstance(document, ms); using (MemoryStream msXmp = new MemoryStream()) { XmpWriter xmp = new XmpWriter(msXmp); XmpSchema dc = new DublinCoreSchema(); XmpArray subject = new XmpArray(XmpArray.UNORDERED); subject.Add("Hello World"); subject.Add("XMP & Metadata"); subject.Add("Metadata"); dc.SetProperty(DublinCoreSchema.SUBJECT, subject); xmp.AddRdfDescription(dc); PdfSchema pdf = new PdfSchema(); /* * iTextSharp uses Item property instead of Java setProperty() method * * pdf.SetProperty(PdfSchema.KEYWORDS, "Hello World, XMP, Metadata"); * pdf.SetProperty(PdfSchema.VERSION, "1.4"); */ pdf[PdfSchema.KEYWORDS] = "Hello World, XMP, Metadata"; pdf[PdfSchema.VERSION] = "1.4"; xmp.AddRdfDescription(pdf); xmp.Close(); writer.XmpMetadata = ms.ToArray(); } // step 3 document.Open(); // step 4 document.Add(new Paragraph("Hello World")); } return ms.ToArray(); } }
virtual public void ManipulatePdfTest() { String fileName = "xmp_metadata_added.pdf"; PdfReader reader = new PdfReader(CMP_FOLDER + "pdf_metadata.pdf"); PdfStamper stamper = new PdfStamper(reader, new FileStream(OUT_FOLDER + fileName, FileMode.Create)); Dictionary<String, String> info = reader.Info; MemoryStream baos = new MemoryStream(); XmpWriter xmp = new XmpWriter(baos, info); xmp.Close(); stamper.XmpMetadata = baos.ToArray(); stamper.Close(); reader.Close(); CompareResults(fileName, fileName); }
protected internal virtual void Close(IDictionary<String, String> moreInfo) { if (closed) return; if (useVp) { SetViewerPreferences(); } if (flat) FlatFields(); if (flatFreeText) FlatFreeTextFields(); AddFieldResources(); PdfDictionary catalog = reader.Catalog; PdfDictionary acroForm = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.ACROFORM), reader.Catalog); if (acroFields != null && acroFields.Xfa.Changed) { MarkUsed(acroForm); if (!flat) acroFields.Xfa.SetXfa(this); } if (sigFlags != 0) { if (acroForm != null) { acroForm.Put(PdfName.SIGFLAGS, new PdfNumber(sigFlags)); MarkUsed(acroForm); MarkUsed(catalog); } } closed = true; AddSharedObjectsToBody(); SetOutlines(); SetJavaScript(); AddFileAttachments(); if (openAction != null) { catalog.Put(PdfName.OPENACTION, openAction); } if (pdf.pageLabels != null) catalog.Put(PdfName.PAGELABELS, pdf.pageLabels.GetDictionary(this)); // OCG if (documentOCG.Count > 0) { FillOCProperties(false); PdfDictionary ocdict = catalog.GetAsDict(PdfName.OCPROPERTIES); if (ocdict == null) { reader.Catalog.Put(PdfName.OCPROPERTIES, OCProperties); } else { ocdict.Put(PdfName.OCGS, OCProperties.Get(PdfName.OCGS)); PdfDictionary ddict = ocdict.GetAsDict(PdfName.D); if (ddict == null) { ddict = new PdfDictionary(); ocdict.Put(PdfName.D, ddict); } ddict.Put(PdfName.ORDER, OCProperties.GetAsDict(PdfName.D).Get(PdfName.ORDER)); ddict.Put(PdfName.RBGROUPS, OCProperties.GetAsDict(PdfName.D).Get(PdfName.RBGROUPS)); ddict.Put(PdfName.OFF, OCProperties.GetAsDict(PdfName.D).Get(PdfName.OFF)); ddict.Put(PdfName.AS, OCProperties.GetAsDict(PdfName.D).Get(PdfName.AS)); } } // metadata int skipInfo = -1; PdfObject oInfo = reader.Trailer.Get(PdfName.INFO); PRIndirectReference iInfo = null; PdfDictionary oldInfo = null; if (oInfo is PRIndirectReference) iInfo = (PRIndirectReference)oInfo; if (iInfo != null) oldInfo = (PdfDictionary)PdfReader.GetPdfObject(iInfo); else if (oInfo is PdfDictionary) oldInfo = (PdfDictionary)oInfo; String producer = null; if (iInfo != null) skipInfo = iInfo.Number; if (oldInfo != null && oldInfo.Get(PdfName.PRODUCER) != null) producer = oldInfo.GetAsString(PdfName.PRODUCER).ToUnicodeString(); Version version = Version.GetInstance(); if (producer == null) { producer = version.GetVersion; } else if (producer.IndexOf(version.Product) == -1) { StringBuilder buf = new StringBuilder(producer); buf.Append("; modified using "); buf.Append(version.GetVersion); producer = buf.ToString(); } PdfIndirectReference info = null; PdfDictionary newInfo = new PdfDictionary(); if (oldInfo != null) { foreach (PdfName key in oldInfo.Keys) { PdfObject value = PdfReader.GetPdfObject(oldInfo.Get(key)); newInfo.Put(key, value); } } if (moreInfo != null) { foreach (KeyValuePair<string,string> entry in moreInfo) { PdfName keyName = new PdfName(entry.Key); String value = entry.Value; if (value == null) newInfo.Remove(keyName); else newInfo.Put(keyName, new PdfString(value, PdfObject.TEXT_UNICODE)); } } PdfDate date = new PdfDate(); newInfo.Put(PdfName.MODDATE, date); newInfo.Put(PdfName.PRODUCER, new PdfString(producer, PdfObject.TEXT_UNICODE)); if (append) { if (iInfo == null) info = AddToBody(newInfo, false).IndirectReference; else info = AddToBody(newInfo, iInfo.Number, false).IndirectReference; } else { info = AddToBody(newInfo, false).IndirectReference; } // XMP byte[] altMetadata = null; PdfObject xmpo = PdfReader.GetPdfObject(catalog.Get(PdfName.METADATA)); if (xmpo != null && xmpo.IsStream()) { altMetadata = PdfReader.GetStreamBytesRaw((PRStream)xmpo); PdfReader.KillIndirect(catalog.Get(PdfName.METADATA)); } if (xmpMetadata != null) { altMetadata = xmpMetadata; } if (altMetadata != null) { PdfStream xmp; try { XmpReader xmpr; if (moreInfo == null || xmpMetadata != null) { xmpr = new XmpReader(altMetadata); if (!(xmpr.ReplaceNode("http://ns.adobe.com/pdf/1.3/", "Producer", producer) || xmpr.ReplaceDescriptionAttribute("http://ns.adobe.com/pdf/1.3/", "Producer", producer))) xmpr.Add("rdf:Description", "http://ns.adobe.com/pdf/1.3/", "Producer", producer); if (!(xmpr.ReplaceNode("http://ns.adobe.com/xap/1.0/", "ModifyDate", date.GetW3CDate()) || xmpr.ReplaceDescriptionAttribute("http://ns.adobe.com/xap/1.0/", "ModifyDate", date.GetW3CDate()))) xmpr.Add("rdf:Description", "http://ns.adobe.com/xap/1.0/", "ModifyDate", date.GetW3CDate()); if (!(xmpr.ReplaceNode("http://ns.adobe.com/xap/1.0/", "MetadataDate", date.GetW3CDate()) || xmpr.ReplaceDescriptionAttribute("http://ns.adobe.com/xap/1.0/", "MetadataDate", date.GetW3CDate()))) { } } else { MemoryStream baos = new MemoryStream(); try { XmpWriter xmpw = new XmpWriter(baos, newInfo, PDFXConformance); xmpw.Close(); } catch (IOException) { } xmpr = new XmpReader(baos.ToArray()); } xmp = new PdfStream(xmpr.SerializeDoc()); } catch { xmp = new PdfStream(altMetadata); } xmp.Put(PdfName.TYPE, PdfName.METADATA); xmp.Put(PdfName.SUBTYPE, PdfName.XML); if (crypto != null && !crypto.IsMetadataEncrypted()) { PdfArray ar = new PdfArray(); ar.Add(PdfName.CRYPT); xmp.Put(PdfName.FILTER, ar); } if (append && xmpo != null) { body.Add(xmp, xmpo.IndRef); } else { catalog.Put(PdfName.METADATA, body.Add(xmp).IndirectReference); MarkUsed(catalog); } } Close(info, skipInfo); }
internal void Close(IDictionary<String, String> moreInfo) { if (closed) return; if (useVp) { reader.SetViewerPreferences(viewerPreferences); MarkUsed(reader.Trailer.Get(PdfName.ROOT)); } if (flat) FlatFields(); if (flatFreeText) FlatFreeTextFields(); AddFieldResources(); PdfDictionary catalog = reader.Catalog; PdfDictionary pages = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.PAGES)); pages.Put(PdfName.ITXT, new PdfString(Document.Release)); MarkUsed(pages); PdfDictionary acroForm = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.ACROFORM), reader.Catalog); if (acroFields != null && acroFields.Xfa.Changed) { MarkUsed(acroForm); if (!flat) acroFields.Xfa.SetXfa(this); } if (sigFlags != 0) { if (acroForm != null) { acroForm.Put(PdfName.SIGFLAGS, new PdfNumber(sigFlags)); MarkUsed(acroForm); MarkUsed(catalog); } } closed = true; AddSharedObjectsToBody(); SetOutlines(); SetJavaScript(); AddFileAttachments(); if (openAction != null) { catalog.Put(PdfName.OPENACTION, openAction); } if (pdf.pageLabels != null) catalog.Put(PdfName.PAGELABELS, pdf.pageLabels.GetDictionary(this)); // OCG if (documentOCG.Count > 0) { FillOCProperties(false); PdfDictionary ocdict = catalog.GetAsDict(PdfName.OCPROPERTIES); if (ocdict == null) { reader.Catalog.Put(PdfName.OCPROPERTIES, OCProperties); } else { ocdict.Put(PdfName.OCGS, OCProperties.Get(PdfName.OCGS)); PdfDictionary ddict = ocdict.GetAsDict(PdfName.D); if (ddict == null) { ddict = new PdfDictionary(); ocdict.Put(PdfName.D, ddict); } ddict.Put(PdfName.ORDER, OCProperties.GetAsDict(PdfName.D).Get(PdfName.ORDER)); ddict.Put(PdfName.RBGROUPS, OCProperties.GetAsDict(PdfName.D).Get(PdfName.RBGROUPS)); ddict.Put(PdfName.OFF, OCProperties.GetAsDict(PdfName.D).Get(PdfName.OFF)); ddict.Put(PdfName.AS, OCProperties.GetAsDict(PdfName.D).Get(PdfName.AS)); } } // metadata int skipInfo = -1; PdfObject oInfo = reader.Trailer.Get(PdfName.INFO); PRIndirectReference iInfo = null; PdfDictionary oldInfo = null; if (oInfo is PRIndirectReference) iInfo = (PRIndirectReference)oInfo; if (iInfo != null) oldInfo = (PdfDictionary)PdfReader.GetPdfObject(iInfo); else if (oInfo is PdfDictionary) oldInfo = (PdfDictionary)oInfo; String producer = null; if (iInfo != null) skipInfo = iInfo.Number; if (oldInfo != null && oldInfo.Get(PdfName.PRODUCER) != null) producer = oldInfo.GetAsString(PdfName.PRODUCER).ToUnicodeString(); if (producer == null) { producer = Document.Version; } else if (producer.IndexOf(Document.Product) == -1) { StringBuilder buf = new StringBuilder(producer); buf.Append("; modified using "); buf.Append(Document.Version); producer = buf.ToString(); } PdfIndirectReference info = null; PdfDictionary newInfo = new PdfDictionary(); if (oldInfo != null) { foreach (PdfName key in oldInfo.Keys) { PdfObject value = PdfReader.GetPdfObject(oldInfo.Get(key)); newInfo.Put(key, value); } } if (moreInfo != null) { foreach (KeyValuePair<string,string> entry in moreInfo) { PdfName keyName = new PdfName(entry.Key); String value = entry.Value; if (value == null) newInfo.Remove(keyName); else newInfo.Put(keyName, new PdfString(value, PdfObject.TEXT_UNICODE)); } } PdfDate date = new PdfDate(); newInfo.Put(PdfName.MODDATE, date); newInfo.Put(PdfName.PRODUCER, new PdfString(producer, PdfObject.TEXT_UNICODE)); if (append) { if (iInfo == null) info = AddToBody(newInfo, false).IndirectReference; else info = AddToBody(newInfo, iInfo.Number, false).IndirectReference; } else { info = AddToBody(newInfo, false).IndirectReference; } // XMP byte[] altMetadata = null; PdfObject xmpo = PdfReader.GetPdfObject(catalog.Get(PdfName.METADATA)); if (xmpo != null && xmpo.IsStream()) { altMetadata = PdfReader.GetStreamBytesRaw((PRStream)xmpo); PdfReader.KillIndirect(catalog.Get(PdfName.METADATA)); } if (xmpMetadata != null) { altMetadata = xmpMetadata; } if (altMetadata != null) { PdfStream xmp; try { XmpReader xmpr; if (moreInfo == null) { xmpr = new XmpReader(altMetadata); if (!(xmpr.ReplaceNode("http://ns.adobe.com/pdf/1.3/", "Producer", producer) || xmpr.ReplaceDescriptionAttribute("http://ns.adobe.com/pdf/1.3/", "Producer", producer))) xmpr.Add("rdf:Description", "http://ns.adobe.com/pdf/1.3/", "Producer", producer); if (!(xmpr.ReplaceNode("http://ns.adobe.com/xap/1.0/", "ModifyDate", date.GetW3CDate()) || xmpr.ReplaceDescriptionAttribute("http://ns.adobe.com/xap/1.0/", "ModifyDate", date.GetW3CDate()))) xmpr.Add("rdf:Description", "http://ns.adobe.com/xap/1.0/", "ModifyDate", date.GetW3CDate()); if (!(xmpr.ReplaceNode("http://ns.adobe.com/xap/1.0/", "MetadataDate", date.GetW3CDate()) || xmpr.ReplaceDescriptionAttribute("http://ns.adobe.com/xap/1.0/", "MetadataDate", date.GetW3CDate()))) { } } else { MemoryStream baos = new MemoryStream(); try { XmpWriter xmpw = new XmpWriter(baos, newInfo, PDFXConformance); xmpw.Close(); } catch (IOException) { } xmpr = new XmpReader(baos.ToArray()); } xmp = new PdfStream(xmpr.SerializeDoc()); } catch { xmp = new PdfStream(altMetadata); } xmp.Put(PdfName.TYPE, PdfName.METADATA); xmp.Put(PdfName.SUBTYPE, PdfName.XML); if (crypto != null && !crypto.IsMetadataEncrypted()) { PdfArray ar = new PdfArray(); ar.Add(PdfName.CRYPT); xmp.Put(PdfName.FILTER, ar); } if (append && xmpo != null) { body.Add(xmp, xmpo.IndRef); } else { catalog.Put(PdfName.METADATA, body.Add(xmp).IndirectReference); MarkUsed(catalog); } } try { file.ReOpen(); AlterContents(); int rootN = ((PRIndirectReference)reader.trailer.Get(PdfName.ROOT)).Number; if (append) { int[] keys = marked.GetKeys(); for (int k = 0; k < keys.Length; ++k) { int j = keys[k]; PdfObject obj = reader.GetPdfObjectRelease(j); if (obj != null && skipInfo != j && j < initialXrefSize) { AddToBody(obj, j, j != rootN); } } for (int k = initialXrefSize; k < reader.XrefSize; ++k) { PdfObject obj = reader.GetPdfObject(k); if (obj != null) { AddToBody(obj, GetNewObjectNumber(reader, k, 0)); } } } else { for (int k = 1; k < reader.XrefSize; ++k) { PdfObject obj = reader.GetPdfObjectRelease(k); if (obj != null && skipInfo != k) { AddToBody(obj, GetNewObjectNumber(reader, k, 0), k != rootN); } } } } finally { try { file.Close(); } catch { // empty on purpose } } PdfIndirectReference encryption = null; PdfObject fileID = null; if (crypto != null) { if (append) { encryption = reader.GetCryptoRef(); } else { PdfIndirectObject encryptionObject = AddToBody(crypto.GetEncryptionDictionary(), false); encryption = encryptionObject.IndirectReference; } fileID = crypto.FileID; } else fileID = PdfEncryption.CreateInfoId(PdfEncryption.CreateDocumentId()); PRIndirectReference iRoot = (PRIndirectReference)reader.trailer.Get(PdfName.ROOT); PdfIndirectReference root = new PdfIndirectReference(0, GetNewObjectNumber(reader, iRoot.Number, 0)); // write the cross-reference table of the body body.WriteCrossReferenceTable(os, root, info, encryption, fileID, prevxref); if (fullCompression) { byte[] tmp = GetISOBytes("startxref\n"); os.Write(tmp, 0, tmp.Length); tmp = GetISOBytes(body.Offset.ToString()); os.Write(tmp, 0, tmp.Length); tmp = GetISOBytes("\n%%EOF\n"); os.Write(tmp, 0, tmp.Length); } else { PdfTrailer trailer = new PdfTrailer(body.Size, body.Offset, root, info, encryption, fileID, prevxref); trailer.ToPdf(this, os); } os.Flush(); if (CloseStream) os.Close(); reader.Close(); }
/** * @return an XmpMetadata byte array */ private byte[] CreateXmpMetadataBytes() { MemoryStream baos = new MemoryStream(); try { XmpWriter xmp = new XmpWriter(baos, pdf.Info, pdfxConformance.PDFXConformance); xmp.Close(); } catch(IOException) { } return baos.ToArray(); }
public byte[] GetStreamedMetaData() { MemoryStream os = new MemoryStream(); XmpWriter xmp = new XmpWriter(os, _info); xmp.Close(); return os.ToArray(); }
public byte[] getStreamedMetaData() { MemoryStream os = new System.IO.MemoryStream(); XmpWriter xmp = new XmpWriter(os, this.info); xmp.Close(); return os.ToArray(); }
// --------------------------------------------------------------------------- /** * Manipulates a PDF file src with the file dest as result * @param src the original PDF */ public byte[] ManipulatePdf(byte[] src) { PdfReader reader = new PdfReader(src); using (MemoryStream ms = new MemoryStream()) { using (PdfStamper stamper = new PdfStamper(reader, ms)) { Dictionary<String, String> info = reader.Info; using (MemoryStream msXmp = new MemoryStream()) { XmpWriter xmp = new XmpWriter(msXmp, info); xmp.Close(); stamper.XmpMetadata = msXmp.ToArray(); } } return ms.ToArray(); } }