/// <summary> /// Use this method to write XMP data to a new PDF /// </summary> /// <param name="writer"></param> private void CreateXmpMetadata(iTextSharp.text.pdf.PdfWriter writer) { // Set up the buffer to hold the XMP metadata byte[] buffer = new byte[65536]; System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer, true); try { // XMP supports a number of different schemas, which are made available by iTextSharp. // Here, the Dublin Core schema is chosen. iTextSharp.text.xml.xmp.XmpSchema dc = new iTextSharp.text.xml.xmp.DublinCoreSchema(); // Add Dublin Core attributes iTextSharp.text.xml.xmp.LangAlt title = new iTextSharp.text.xml.xmp.LangAlt(); title.Add("x-default", "My Science Project"); dc.SetProperty(iTextSharp.text.xml.xmp.DublinCoreSchema.TITLE, title); // Dublin Core allows multiple authors, so we create an XmpArray to hold the values iTextSharp.text.xml.xmp.XmpArray author = new iTextSharp.text.xml.xmp.XmpArray(iTextSharp.text.xml.xmp.XmpArray.ORDERED); author.Add("M. Lichtenberg"); dc.SetProperty(iTextSharp.text.xml.xmp.DublinCoreSchema.CREATOR, author); // Multiple subjects are also possible, so another XmpArray is used iTextSharp.text.xml.xmp.XmpArray subject = new iTextSharp.text.xml.xmp.XmpArray(iTextSharp.text.xml.xmp.XmpArray.UNORDERED); subject.Add("paper airplanes"); subject.Add("science project"); dc.SetProperty(iTextSharp.text.xml.xmp.DublinCoreSchema.SUBJECT, subject); // Create an XmpWriter using the MemoryStream defined earlier iTextSharp.text.xml.xmp.XmpWriter xmp = new iTextSharp.text.xml.xmp.XmpWriter(ms); xmp.AddRdfDescription(dc); // Add the completed metadata definition to the XmpWriter xmp.Close(); // This flushes the XMP metadata into the buffer //--------------------------------------------------------------------------------- // Shrink the buffer to the correct size (discard empty elements of the byte array) int bufsize = buffer.Length; int bufcount = 0; foreach (byte b in buffer) { if (b == 0) { break; } bufcount++; } System.IO.MemoryStream ms2 = new System.IO.MemoryStream(buffer, 0, bufcount); buffer = ms2.ToArray(); //--------------------------------------------------------------------------------- // Add all of the XMP metadata to the PDF doc that we're building writer.XmpMetadata = buffer; } catch (Exception ex) { throw ex; } finally { ms.Close(); ms.Dispose(); } }
/// <summary> /// Use this method to write XMP data to a new PDF /// </summary> /// <param name="writer"></param> private void CreateXmpMetadata(iTextSharp.text.pdf.PdfWriter writer) { // Set up the buffer to hold the XMP metadata byte[] buffer = new byte[65536]; System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer, true); try { // XMP supports a number of different schemas, which are made available by iTextSharp. // Here, the Dublin Core schema is chosen. iTextSharp.text.xml.xmp.XmpSchema dc = new iTextSharp.text.xml.xmp.DublinCoreSchema(); // Add Dublin Core attributes iTextSharp.text.xml.xmp.LangAlt title = new iTextSharp.text.xml.xmp.LangAlt(); title.Add("x-default", "My Science Project"); dc.SetProperty(iTextSharp.text.xml.xmp.DublinCoreSchema.TITLE, title); // Dublin Core allows multiple authors, so we create an XmpArray to hold the values iTextSharp.text.xml.xmp.XmpArray author = new iTextSharp.text.xml.xmp.XmpArray(iTextSharp.text.xml.xmp.XmpArray.ORDERED); author.Add("M. Lichtenberg"); dc.SetProperty(iTextSharp.text.xml.xmp.DublinCoreSchema.CREATOR, author); // Multiple subjects are also possible, so another XmpArray is used iTextSharp.text.xml.xmp.XmpArray subject = new iTextSharp.text.xml.xmp.XmpArray(iTextSharp.text.xml.xmp.XmpArray.UNORDERED); subject.Add("paper airplanes"); subject.Add("science project"); dc.SetProperty(iTextSharp.text.xml.xmp.DublinCoreSchema.SUBJECT, subject); // Create an XmpWriter using the MemoryStream defined earlier iTextSharp.text.xml.xmp.XmpWriter xmp = new iTextSharp.text.xml.xmp.XmpWriter(ms); xmp.AddRdfDescription(dc); // Add the completed metadata definition to the XmpWriter xmp.Close(); // This flushes the XMP metadata into the buffer //--------------------------------------------------------------------------------- // Shrink the buffer to the correct size (discard empty elements of the byte array) int bufsize = buffer.Length; int bufcount = 0; foreach (byte b in buffer) { if (b == 0) break; bufcount++; } System.IO.MemoryStream ms2 = new System.IO.MemoryStream(buffer, 0, bufcount); buffer = ms2.ToArray(); //--------------------------------------------------------------------------------- // Add all of the XMP metadata to the PDF doc that we're building writer.XmpMetadata = buffer; } catch (Exception ex) { throw ex; } finally { ms.Close(); ms.Dispose(); } }