private void WriteXmpMetaData(string imageFilePath, MasterImage masterImage, bool alwaysWriteMetadata, bool preview) { bool writeMetadata = false; IXmpMeta xmp = XmpMetaFactory.Create(); if ((!String.IsNullOrEmpty(masterImage.Caption) && !IsEquivalent(masterImage.Caption, imageFilePath)) || alwaysWriteMetadata) { xmp.AppendArrayItem(XmpConstants.NsDC, "dc:title", new PropertyOptions { IsArrayAlternate = true }, masterImage.Caption, null); writeMetadata = true; } if (!String.IsNullOrEmpty(masterImage.Comment)) { xmp.AppendArrayItem(XmpConstants.NsDC, "dc:description", new PropertyOptions { IsArrayAlternate = true }, masterImage.Comment, null); writeMetadata = true; } if (masterImage.Rating != null && (int)masterImage.Rating > 0) { xmp.SetProperty(XmpConstants.NsXmp, "xmp:Rating", ((int)masterImage.Rating).ToString()); writeMetadata = true; } // TODO: Handle faces. if (writeMetadata) { string metaFilePath = Path.ChangeExtension(imageFilePath, ".xmp"); if (File.Exists(metaFilePath)) { Console.Error.WriteLine("ERROR: XMP meta file already exists, skipping '" + metaFilePath + "'."); } else if (!preview) { Directory.CreateDirectory(Path.GetDirectoryName(metaFilePath)); using (var stream = File.OpenWrite(metaFilePath)) { XmpMetaFactory.Serialize(xmp, stream, new SerializeOptions { OmitPacketWrapper = true }); } numMetadataFilesCreated++; } } }
/** * Creates an XmpWriter. * @param os * @param utfEncoding * @param extraSpace * @throws IOException */ public XmpWriter(Stream os, String utfEncoding, int extraSpace) { outputStream = os; serializeOptions = new SerializeOptions(); if (UTF16BE.Equals(utfEncoding) || UTF16.Equals(utfEncoding)) { serializeOptions.EncodeUtf16Be = true; } else if (UTF16LE.Equals(utfEncoding)) { serializeOptions.EncodeUtf16Le = true; } serializeOptions.Padding = extraSpace; xmpMeta = XmpMetaFactory.Create(); xmpMeta.ObjectName = XmpConst.TAG_XMPMETA; xmpMeta.ObjectName = ""; try { xmpMeta.SetProperty(XmpConst.NS_DC, DublinCoreProperties.FORMAT, "application/pdf"); xmpMeta.SetProperty(XmpConst.NS_PDF, PdfProperties.PRODUCER, Version.GetInstance().GetVersion); } catch (XmpException) {} }
/** * Test getProperty, deleteProperty and related methods. * @param meta a predefined <code>XmpMeta</code> object. * @throws XmpException Forwards exceptions */ private static void CoverGetPropertyMethods(IXmpMeta meta) { writeMajorLabel("Test getProperty, deleteProperty and related methods"); meta.DeleteProperty(TestData.NS1, "QualProp1"); // ! Start with fresh qualifiers. meta.DeleteProperty(TestData.NS1, "ns1:QualProp2"); meta.DeleteProperty(TestData.NS1, "ns1:QualProp3"); meta.DeleteProperty(TestData.NS1, "QualProp4"); writeMinorLabel("Set properties with qualifier"); meta.SetProperty(TestData.NS1, "QualProp1", "Prop value"); meta.SetQualifier(TestData.NS1, "QualProp1", TestData.NS2, "Qual1", "Qual1 value"); meta.SetProperty(TestData.NS1, "QualProp2", "Prop value"); meta.SetQualifier(TestData.NS1, "QualProp2", XmpConstants.NsXml, "lang", "en-us"); meta.SetProperty(TestData.NS1, "QualProp3", "Prop value"); meta.SetQualifier(TestData.NS1, "QualProp3", XmpConstants.NsXml, "lang", "en-us"); meta.SetQualifier(TestData.NS1, "QualProp3", TestData.NS2, "Qual", "Qual value"); meta.SetProperty(TestData.NS1, "QualProp4", "Prop value"); meta.SetQualifier(TestData.NS1, "QualProp4", TestData.NS2, "Qual", "Qual value"); meta.SetQualifier(TestData.NS1, "QualProp4", XmpConstants.NsXml, "lang", "en-us"); printXmpMeta(meta, "XMP object"); writeMinorLabel("Get simple properties"); var property = meta.GetProperty(TestData.NS1, "Prop"); log.WriteLine("getProperty ns1:Prop = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetProperty(TestData.NS1, "ns1:XMLProp"); log.WriteLine("getProperty ns1:XMLProp = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetProperty(TestData.NS1, "ns1:URIProp"); log.WriteLine("getProperty ns1:URIProp = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetArrayItem(TestData.NS1, "Bag", 2); log.WriteLine("getArrayItem ns1:Bag[2] = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); try { meta.GetArrayItem(null, "ns1:Bag", 1); } catch (XmpException e) { log.WriteLine("getArrayItem with no schema URI - threw XmpException #" + e.GetErrorCode() + " : " + e.Message + ")"); } writeMinorLabel("Get array items and struct fields"); property = meta.GetArrayItem(TestData.NS1, "ns1:Seq", 1); log.WriteLine("getArrayItem ns1:Seq[1] = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetArrayItem(TestData.NS1, "ns1:Alt", 1); log.WriteLine("getArrayItem ns1:Alt[1] = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); log.WriteLine(); property = meta.GetStructField(TestData.NS1, "Struct", TestData.NS2, "Field1"); log.WriteLine("getStructField ns1:Struct/ns2:Field1 = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "Field2"); log.WriteLine("getStructField ns1:Struct/ns2:Field2 = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "ns2:Field3"); log.WriteLine("getStructField ns1:Struct/ns2:Field3 = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "ns2:Field3"); log.WriteLine("getStructField ns1:Struct/ns2:Field3 = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "ns2:Field3"); log.WriteLine("getStructField ns1:Struct/ns2:Field3 = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); log.WriteLine(); writeMinorLabel("Get qualifier"); property = meta.GetQualifier(TestData.NS1, "QualProp1", TestData.NS2, "Qual1"); log.WriteLine("getQualifier ns1:QualProp1/?ns2:Qual1 = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); try { meta.GetQualifier(null, "ns1:QualProp1", TestData.NS2, "Qual1"); } catch (XmpException e) { log.WriteLine("getQualifier with no schema URI - threw XmpException #" + e.GetErrorCode() + " : " + e.Message); } property = meta.GetQualifier(TestData.NS1, "QualProp3", XmpConstants.NsXml, "xml:lang"); log.WriteLine("getQualifier ns1:QualProp3/@xml-lang = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetQualifier(TestData.NS1, "QualProp3", TestData.NS2, "ns2:Qual"); log.WriteLine("getQualifier ns1:QualProp3/?ns2:Qual = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); log.WriteLine(); writeMinorLabel("Get non-simple properties"); property = meta.GetProperty(TestData.NS1, "Bag"); log.WriteLine("getProperty ns1:Bag = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetProperty(TestData.NS1, "Seq"); log.WriteLine("getProperty ns1:Seq = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetProperty(TestData.NS1, "Alt"); log.WriteLine("getProperty ns1:Alt = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetProperty(TestData.NS1, "Struct"); log.WriteLine("getProperty ns1:Struct = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); log.WriteLine(); writeMinorLabel("Get not existing properties"); try { meta.GetProperty("ns:bogus/", "Bogus"); } catch (XmpException e) { log.WriteLine("getProperty with bogus schema URI - threw XmpException #" + e.GetErrorCode() + " : " + e.Message); } property = meta.GetProperty(TestData.NS1, "Bogus"); log.WriteLine("getProperty ns1:Bogus (not existing) = " + property); property = meta.GetArrayItem(TestData.NS1, "Bag", 99); log.WriteLine("ArrayItem ns1:Bag[99] (not existing) = " + property); property = meta.GetStructField(TestData.NS1, "Struct", TestData.NS2, "Bogus"); log.WriteLine("getStructField ns1:Struct/ns2:Bogus (not existing) = " + property); property = meta.GetQualifier(TestData.NS1, "Prop", TestData.NS2, "Bogus"); log.WriteLine("getQualifier ns1:Prop/?ns2:Bogus (not existing) = " + property); }
/** * Creates an XmpWriter. * @param os * @param utfEncoding * @param extraSpace * @throws IOException */ public XmpWriter(Stream os, String utfEncoding, int extraSpace) { outputStream = os; serializeOptions = new SerializeOptions(); if (UTF16BE.Equals(utfEncoding) || UTF16.Equals(utfEncoding)) serializeOptions.EncodeUtf16Be = true; else if (UTF16LE.Equals(utfEncoding)) serializeOptions.EncodeUtf16Le = true; serializeOptions.Padding = extraSpace; xmpMeta = XmpMetaFactory.Create(); xmpMeta.ObjectName = XmpConst.TAG_XMPMETA; xmpMeta.ObjectName = ""; try { xmpMeta.SetProperty(XmpConst.NS_DC, DublinCoreProperties.FORMAT, "application/pdf"); xmpMeta.SetProperty(XmpConst.NS_PDF, PdfProperties.PRODUCER, Version.GetInstance().GetVersion); } catch (XmpException) {} }
/** * Adds part. * * @param xmpMeta * @param part */ public static void SetPart(IXmpMeta xmpMeta, String part) { xmpMeta.SetProperty(XmpConst.NS_PDFA_ID, PdfAProperties.PART, part); }
/** * Adds the creation date. * * @param xmpMeta * @param date */ public static void SetCreateDate(IXmpMeta xmpMeta, String date) { xmpMeta.SetProperty(XmpConst.NS_XMP, CREATEDATE, date); }
/** Adds the nickname. * * @param xmpMeta * @param name */ public static void SetNickname(IXmpMeta xmpMeta, String name) { xmpMeta.SetProperty(XmpConst.NS_XMP, NICKNAME, name); }
/** * Adds the version. * * @param xmpMeta * @param version */ public static void SetVersion(IXmpMeta xmpMeta, String version) { xmpMeta.SetProperty(XmpConst.NS_PDF, VERSION, version); }
/** * Adds keywords. * * @param xmpMeta * @param keywords */ public static void SetKeywords(IXmpMeta xmpMeta, String keywords) { xmpMeta.SetProperty(XmpConst.NS_PDF, KEYWORDS, keywords); }
/** * Test getProperty, deleteProperty and related methods. * @param meta a predefined <code>XmpMeta</code> object. * @throws XmpException Forwards exceptions */ private static void CoverGetPropertyMethods(IXmpMeta meta) { writeMajorLabel ("Test getProperty, deleteProperty and related methods"); meta.DeleteProperty (TestData.NS1, "QualProp1"); // ! Start with fresh qualifiers. meta.DeleteProperty (TestData.NS1, "ns1:QualProp2"); meta.DeleteProperty (TestData.NS1, "ns1:QualProp3"); meta.DeleteProperty (TestData.NS1, "QualProp4"); writeMinorLabel("Set properties with qualifier"); meta.SetProperty (TestData.NS1, "QualProp1", "Prop value"); meta.SetQualifier (TestData.NS1, "QualProp1", TestData.NS2, "Qual1", "Qual1 value"); meta.SetProperty (TestData.NS1, "QualProp2", "Prop value"); meta.SetQualifier (TestData.NS1, "QualProp2", XmpConstants.NsXml, "lang", "en-us"); meta.SetProperty (TestData.NS1, "QualProp3", "Prop value"); meta.SetQualifier (TestData.NS1, "QualProp3", XmpConstants.NsXml, "lang", "en-us"); meta.SetQualifier (TestData.NS1, "QualProp3", TestData.NS2, "Qual", "Qual value"); meta.SetProperty (TestData.NS1, "QualProp4", "Prop value"); meta.SetQualifier (TestData.NS1, "QualProp4", TestData.NS2, "Qual", "Qual value"); meta.SetQualifier (TestData.NS1, "QualProp4", XmpConstants.NsXml, "lang", "en-us"); printXmpMeta (meta, "XMP object"); writeMinorLabel("Get simple properties"); var property = meta.GetProperty(TestData.NS1, "Prop"); log.WriteLine("getProperty ns1:Prop = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetProperty(TestData.NS1, "ns1:XMLProp"); log.WriteLine("getProperty ns1:XMLProp = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetProperty(TestData.NS1, "ns1:URIProp"); log.WriteLine("getProperty ns1:URIProp = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetArrayItem(TestData.NS1, "Bag", 2); log.WriteLine("getArrayItem ns1:Bag[2] = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); try { meta.GetArrayItem(null, "ns1:Bag", 1); } catch (XmpException e) { log.WriteLine("getArrayItem with no schema URI - threw XmpException #" + e.GetErrorCode() +" : " + e.Message + ")"); } writeMinorLabel("Get array items and struct fields"); property = meta.GetArrayItem(TestData.NS1, "ns1:Seq", 1); log.WriteLine("getArrayItem ns1:Seq[1] = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetArrayItem(TestData.NS1, "ns1:Alt", 1); log.WriteLine("getArrayItem ns1:Alt[1] = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); log.WriteLine(); property = meta.GetStructField(TestData.NS1, "Struct", TestData.NS2, "Field1"); log.WriteLine("getStructField ns1:Struct/ns2:Field1 = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "Field2"); log.WriteLine("getStructField ns1:Struct/ns2:Field2 = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "ns2:Field3"); log.WriteLine("getStructField ns1:Struct/ns2:Field3 = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "ns2:Field3"); log.WriteLine("getStructField ns1:Struct/ns2:Field3 = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetStructField(TestData.NS1, "ns1:Struct", TestData.NS2, "ns2:Field3"); log.WriteLine("getStructField ns1:Struct/ns2:Field3 = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); log.WriteLine(); writeMinorLabel("Get qualifier"); property = meta.GetQualifier(TestData.NS1, "QualProp1", TestData.NS2, "Qual1"); log.WriteLine("getQualifier ns1:QualProp1/?ns2:Qual1 = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); try { meta.GetQualifier(null, "ns1:QualProp1", TestData.NS2, "Qual1"); } catch (XmpException e) { log.WriteLine("getQualifier with no schema URI - threw XmpException #" + e.GetErrorCode() + " : " + e.Message); } property = meta.GetQualifier(TestData.NS1, "QualProp3", XmpConstants.NsXml, "xml:lang"); log.WriteLine("getQualifier ns1:QualProp3/@xml-lang = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetQualifier(TestData.NS1, "QualProp3", TestData.NS2, "ns2:Qual"); log.WriteLine("getQualifier ns1:QualProp3/?ns2:Qual = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); log.WriteLine(); writeMinorLabel("Get non-simple properties"); property = meta.GetProperty(TestData.NS1, "Bag"); log.WriteLine("getProperty ns1:Bag = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetProperty(TestData.NS1, "Seq"); log.WriteLine("getProperty ns1:Seq = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetProperty(TestData.NS1, "Alt"); log.WriteLine("getProperty ns1:Alt = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); property = meta.GetProperty(TestData.NS1, "Struct"); log.WriteLine("getProperty ns1:Struct = " + property.Value + " (" + property.Options.GetOptionsString() + ")"); log.WriteLine(); writeMinorLabel("Get not existing properties"); try { meta.GetProperty("ns:bogus/", "Bogus"); } catch (XmpException e) { log.WriteLine("getProperty with bogus schema URI - threw XmpException #" + e.GetErrorCode() + " : " + e.Message); } property = meta.GetProperty (TestData.NS1, "Bogus"); log.WriteLine ("getProperty ns1:Bogus (not existing) = " + property); property = meta.GetArrayItem(TestData.NS1, "Bag", 99); log.WriteLine ("ArrayItem ns1:Bag[99] (not existing) = " + property); property = meta.GetStructField(TestData.NS1, "Struct", TestData.NS2, "Bogus"); log.WriteLine ("getStructField ns1:Struct/ns2:Bogus (not existing) = " + property); property = meta.GetQualifier (TestData.NS1, "Prop", TestData.NS2, "Bogus"); log.WriteLine ("getQualifier ns1:Prop/?ns2:Bogus (not existing) = " + property); }
/** * Adds the meta data date. * * @param xmpMeta * @param date */ public static void SetMetaDataDate(IXmpMeta xmpMeta, String date) { xmpMeta.SetProperty(XmpConst.NS_XMP, METADATADATE, date); }
/** * Adds the modification date. * * @param xmpMeta * @param date */ public static void SetModDate(IXmpMeta xmpMeta, String date) { xmpMeta.SetProperty(XmpConst.NS_XMP, MODIFYDATE, date); }
/** * Adds the creatortool. * * @param xmpMeta * @param creator */ public static void SetCreatorTool(IXmpMeta xmpMeta, String creator) { xmpMeta.SetProperty(XmpConst.NS_XMP, CREATORTOOL, creator); }
/** * Adds the conformance. * * @param xmpMeta * @param conformance */ public static void SetConformance(IXmpMeta xmpMeta, String conformance) { xmpMeta.SetProperty(XmpConst.NS_PDFA_ID, PdfAProperties.CONFORMANCE, conformance); }
/** * Adds the producer. * * @param xmpMeta * @param producer */ public static void SetProducer(IXmpMeta xmpMeta, String producer) { xmpMeta.SetProperty(XmpConst.NS_PDF, PRODUCER, producer); }
/** * @param schemaNS The namespace URI for the property. Has the same usage as in getProperty. * @param propName The name of the property. * Has the same usage as in <code>getProperty()</code>. * @param value the value for the property (only leaf properties have a value). * Arrays and non-leaf levels of structs do not have values. * Must be <code>null</code> if the value is not relevant.<br/> * The value is automatically detected: Boolean, Integer, Long, Double, XMPDateTime and * byte[] are handled, on all other <code>toString()</code> is called. * @throws XMPException Wraps all errors and exceptions that may occur. */ virtual public void SetProperty(String schemaNS, String propName, Object value) { xmpMeta.SetProperty(schemaNS, propName, value); }