/** * Returns the core document properties, eg author */ public String GetCorePropertiesText() { StringBuilder text = new StringBuilder(); PackagePropertiesPart props = Document.GetProperties().CoreProperties.GetUnderlyingProperties(); AppendIfPresent(text, "Category", props.GetCategoryProperty()); AppendIfPresent(text, "Category", props.GetCategoryProperty()); AppendIfPresent(text, "ContentStatus", props.GetContentStatusProperty()); AppendIfPresent(text, "ContentType", props.GetContentTypeProperty()); AppendIfPresent(text, "Created", props.GetCreatedProperty().Value); AppendIfPresent(text, "CreatedString", props.GetCreatedPropertyString()); AppendIfPresent(text, "Creator", props.GetCreatorProperty()); AppendIfPresent(text, "Description", props.GetDescriptionProperty()); AppendIfPresent(text, "Identifier", props.GetIdentifierProperty()); AppendIfPresent(text, "Keywords", props.GetKeywordsProperty()); AppendIfPresent(text, "Language", props.GetLanguageProperty()); AppendIfPresent(text, "LastModifiedBy", props.GetLastModifiedByProperty()); AppendIfPresent(text, "LastPrinted", props.GetLastPrintedProperty()); AppendIfPresent(text, "LastPrintedString", props.GetLastPrintedPropertyString()); AppendIfPresent(text, "Modified", props.GetModifiedProperty()); AppendIfPresent(text, "ModifiedString", props.GetModifiedPropertyString()); AppendIfPresent(text, "Revision", props.GetRevisionProperty()); AppendIfPresent(text, "Subject", props.GetSubjectProperty()); AppendIfPresent(text, "Title", props.GetTitleProperty()); AppendIfPresent(text, "Version", props.GetVersionProperty()); return(text.ToString()); }
public void WorkbookProperties() { XSSFWorkbook workbook = new XSSFWorkbook(); try { POIXMLProperties props = workbook.GetProperties(); Assert.IsNotNull(props); //the Application property must be set for new workbooks, see Bugzilla #47559 Assert.AreEqual("NPOI", props.ExtendedProperties.GetUnderlyingProperties().Application); PackagePropertiesPart opcProps = props.CoreProperties.GetUnderlyingProperties(); Assert.IsNotNull(opcProps); opcProps.SetTitleProperty("Testing Bugzilla #47460"); Assert.AreEqual("NPOI", opcProps.GetCreatorProperty()); opcProps.SetCreatorProperty("*****@*****.**"); XSSFWorkbook wbBack = (XSSFWorkbook)XSSFTestDataSamples.WriteOutAndReadBack(workbook); Assert.AreEqual("NPOI", wbBack.GetProperties().ExtendedProperties.GetUnderlyingProperties().Application); opcProps = wbBack.GetProperties().CoreProperties.GetUnderlyingProperties(); Assert.AreEqual("Testing Bugzilla #47460", opcProps.GetTitleProperty()); Assert.AreEqual("*****@*****.**", opcProps.GetCreatorProperty()); } finally { workbook.Close(); } }
public void TestEntitiesInCoreProps_56164() { Stream is1 = OpenXml4NetTestDataSamples.OpenSampleStream("CorePropertiesHasEntities.ooxml"); OPCPackage p = OPCPackage.Open(is1); is1.Close(); // Should have 3 root relationships bool foundDocRel = false, foundCorePropRel = false, foundExtPropRel = false; foreach (PackageRelationship pr in p.Relationships) { if (pr.RelationshipType.Equals(PackageRelationshipTypes.CORE_DOCUMENT)) { foundDocRel = true; } if (pr.RelationshipType.Equals(PackageRelationshipTypes.CORE_PROPERTIES)) { foundCorePropRel = true; } if (pr.RelationshipType.Equals(PackageRelationshipTypes.EXTENDED_PROPERTIES)) { foundExtPropRel = true; } } Assert.IsTrue(foundDocRel, "Core/Doc Relationship not found in " + p.Relationships); Assert.IsTrue(foundCorePropRel, "Core Props Relationship not found in " + p.Relationships); Assert.IsTrue(foundExtPropRel, "Ext Props Relationship not found in " + p.Relationships); // Get the Core Properties PackagePropertiesPart props = (PackagePropertiesPart)p.GetPackageProperties(); // Check Assert.AreEqual("Stefan Kopf", props.GetCreatorProperty()); }
/** * Marshall package core properties to an XML document. Always return * <code>true</code>. */ public virtual bool Marshall(PackagePart part, Stream out1) { if (!(part is PackagePropertiesPart)) { throw new ArgumentException( "'part' must be a PackagePropertiesPart instance."); } _propsPart = (PackagePropertiesPart)part; // Configure the document XmlDoc = new XDocument(); XNamespace ns = NamespaceCoreProperties; this.RootElem = new XElement(ns + "coreProperties", new XAttribute(XNamespace.Xmlns + "cp", PackagePropertiesPart.NAMESPACE_CP_URI), new XAttribute(XNamespace.Xmlns + "dc", PackagePropertiesPart.NAMESPACE_DC_URI), new XAttribute(XNamespace.Xmlns + "dcterms", PackagePropertiesPart.NAMESPACE_DCTERMS_URI), new XAttribute(XNamespace.Xmlns + "xsi", PackagePropertiesPart.NAMESPACE_XSI_URI) ); //this.RootElem.Name = // (XNamespace)NamespaceCoreProperties + RootElem.Name.LocalName; //using (var reader = XmlDoc.CreateReader()) //{ // Nsmgr = new XmlNamespaceManager(reader.NameTable); //} //Nsmgr.AddNamespace("cp", PackagePropertiesPart.NAMESPACE_CP_URI); //Nsmgr.AddNamespace("dc", PackagePropertiesPart.NAMESPACE_DC_URI); //Nsmgr.AddNamespace("dcterms", PackagePropertiesPart.NAMESPACE_DCTERMS_URI); //Nsmgr.AddNamespace("xsi", PackagePropertiesPart.NAMESPACE_XSI_URI); XmlDoc.Add(RootElem); Prop(() => _propsPart.GetCategoryProperty(), NamespaceCoreProperties, "cp", KeywordCategory); Prop(() => _propsPart.GetContentStatusProperty(), NamespaceCoreProperties, "cp", KeywordContentStatus); Prop(() => _propsPart.GetContentTypeProperty(), NamespaceCoreProperties, "cp", KeywordContentType); var created = Prop(() => _propsPart.GetCreatedProperty(), NamespaceDcTerms, "dcterms", KeywordCreated, () => _propsPart.GetCreatedPropertyString()); created?.SetAttribute("type", NamespaceXsi, "dcterms:W3CDTF"); Prop(() => _propsPart.GetCreatorProperty(), NamespaceDc, "dc", KeywordCreator); Prop(() => _propsPart.GetDescriptionProperty(), NamespaceDc, "dc", KeywordDescription); Prop(() => _propsPart.GetIdentifierProperty(), NamespaceDc, "dc", KeywordIdentifier); Prop(() => _propsPart.GetKeywordsProperty(), NamespaceCoreProperties, "cp", KeywordKeywords); Prop(() => _propsPart.GetLanguageProperty(), NamespaceDc, "dc", KeywordLanguage); Prop(() => _propsPart.GetLastModifiedByProperty(), NamespaceCoreProperties, "cp", KeywordLastModifiedBy); Prop(() => _propsPart.GetLastPrintedProperty(), NamespaceCoreProperties, "cp", KeywordLastPrinted, () => _propsPart.GetLastPrintedPropertyString()); var modified = Prop(() => _propsPart.GetModifiedProperty(), NamespaceDcTerms, "dcterms", KeywordModified, () => _propsPart.GetModifiedPropertyString()); modified?.SetAttribute("type", NamespaceXsi, "dcterms:W3CDTF"); Prop(() => _propsPart.GetRevisionProperty(), NamespaceCoreProperties, "cp", KeywordRevision); Prop(() => _propsPart.GetSubjectProperty(), NamespaceDc, "dc", KeywordSubject); Prop(() => _propsPart.GetTitleProperty(), NamespaceDc, "dc", KeywordTitle); Prop(() => _propsPart.GetVersionProperty(), NamespaceCoreProperties, "cp", KeywordVersion); return(true); }
public void TestCoreProperties_bug51374() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); String strDate = "2007-05-12T08:00:00Z"; DateTime date = DateTime.Parse(strDate).ToUniversalTime(); OPCPackage pkg = new ZipPackage(); PackagePropertiesPart props = (PackagePropertiesPart)pkg.GetPackageProperties(); // Created Assert.AreEqual("", props.GetCreatedPropertyString()); Assert.IsNull(props.GetCreatedProperty()); props.SetCreatedProperty((String)null); Assert.AreEqual("", props.GetCreatedPropertyString()); Assert.IsNull(props.GetCreatedProperty()); props.SetCreatedProperty(new Nullable <DateTime>()); Assert.AreEqual("", props.GetCreatedPropertyString()); Assert.IsNull(props.GetCreatedProperty()); props.SetCreatedProperty(new Nullable <DateTime>(date)); Assert.AreEqual(strDate, props.GetCreatedPropertyString()); Assert.AreEqual(date, props.GetCreatedProperty()); props.SetCreatedProperty(strDate); Assert.AreEqual(strDate, props.GetCreatedPropertyString()); Assert.AreEqual(date, props.GetCreatedProperty()); // lastPrinted Assert.AreEqual("", props.GetLastPrintedPropertyString()); Assert.IsNull(props.GetLastPrintedProperty()); props.SetLastPrintedProperty((String)null); Assert.AreEqual("", props.GetLastPrintedPropertyString()); Assert.IsNull(props.GetLastPrintedProperty()); props.SetLastPrintedProperty(new Nullable <DateTime>()); Assert.AreEqual("", props.GetLastPrintedPropertyString()); Assert.IsNull(props.GetLastPrintedProperty()); props.SetLastPrintedProperty(new Nullable <DateTime>(date)); Assert.AreEqual(strDate, props.GetLastPrintedPropertyString()); Assert.AreEqual(date, props.GetLastPrintedProperty()); props.SetLastPrintedProperty(strDate); Assert.AreEqual(strDate, props.GetLastPrintedPropertyString()); Assert.AreEqual(date, props.GetLastPrintedProperty()); // modified Assert.IsNull(props.GetModifiedProperty()); props.SetModifiedProperty((String)null); Assert.IsNull(props.GetModifiedProperty()); props.SetModifiedProperty(new Nullable <DateTime>()); Assert.IsNull(props.GetModifiedProperty()); props.SetModifiedProperty(new Nullable <DateTime>(date)); Assert.AreEqual(strDate, props.GetModifiedPropertyString()); Assert.AreEqual(date, props.GetModifiedProperty()); props.SetModifiedProperty(strDate); Assert.AreEqual(strDate, props.GetModifiedPropertyString()); Assert.AreEqual(date, props.GetModifiedProperty()); pkg.Close(); }
public void TestAlternateCorePropertyTimezones() { Stream is1 = OpenXml4NetTestDataSamples.OpenSampleStream("OPCCompliance_CoreProperties_AlternateTimezones.docx"); OPCPackage pkg = OPCPackage.Open(is1); PackagePropertiesPart props = (PackagePropertiesPart)pkg.GetPackageProperties(); is1.Close(); // We need predictable dates for testing! //SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.fff'Z'"); //use fff for millisecond. df.TimeZone = TimeZoneInfo.Utc; // Check text properties first Assert.AreEqual("Lorem Ipsum", props.GetTitleProperty()); Assert.AreEqual("Apache POI", props.GetCreatorProperty()); // Created at has a +3 timezone and milliseconds // 2006-10-13T18:06:00.123+03:00 // = 2006-10-13T15:06:00.123+00:00 Assert.AreEqual("2006-10-13T15:06:00Z", props.GetCreatedPropertyString()); Assert.AreEqual("2006-10-13T15:06:00.123Z", df.Format(props.GetCreatedProperty())); // Modified at has a -13 timezone but no milliseconds // 2007-06-20T07:59:00-13:00 // = 2007-06-20T20:59:00-13:00 Assert.AreEqual("2007-06-20T20:59:00Z", props.GetModifiedPropertyString()); Assert.AreEqual("2007-06-20T20:59:00.000Z", df.Format(props.GetModifiedProperty())); // Ensure we can change them with other timezones and still read back OK props.SetCreatedProperty("2007-06-20T20:57:00+13:00"); props.SetModifiedProperty("2007-06-20T20:59:00.123-13:00"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); pkg.Save(baos); pkg = OPCPackage.Open(new ByteArrayInputStream(baos.ToByteArray())); // Check text properties first - should be unchanged Assert.AreEqual("Lorem Ipsum", props.GetTitleProperty()); Assert.AreEqual("Apache POI", props.GetCreatorProperty()); // Check the updated times // 2007-06-20T20:57:00+13:00 // = 2007-06-20T07:57:00Z Assert.AreEqual("2007-06-20T07:57:00.000Z", df.Format(props.GetCreatedProperty().Value)); // 2007-06-20T20:59:00.123-13:00 // = 2007-06-21T09:59:00.123Z Assert.AreEqual("2007-06-21T09:59:00.123Z", df.Format(props.GetModifiedProperty().Value)); }
/** * Marshall package core properties to an XML document. Always return * <code>true</code>. */ public virtual bool Marshall(PackagePart part, Stream out1) { if (!(part is PackagePropertiesPart)) { throw new ArgumentException( "'part' must be a PackagePropertiesPart instance."); } propsPart = (PackagePropertiesPart)part; // Configure the document xmlDoc = new XmlDocument(); XmlElement rootElem = xmlDoc.CreateElement("coreProperties", namespaceCoreProperties); nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("cp", PackagePropertiesPart.NAMESPACE_CP_URI); nsmgr.AddNamespace("dc", PackagePropertiesPart.NAMESPACE_DC_URI); nsmgr.AddNamespace("dcterms", PackagePropertiesPart.NAMESPACE_DCTERMS_URI); nsmgr.AddNamespace("xsi", PackagePropertiesPart.NAMESPACE_XSI_URI); rootElem.SetAttribute("xmlns:cp", PackagePropertiesPart.NAMESPACE_CP_URI); rootElem.SetAttribute("xmlns:dc", PackagePropertiesPart.NAMESPACE_DC_URI); rootElem.SetAttribute("xmlns:dcterms", PackagePropertiesPart.NAMESPACE_DCTERMS_URI); rootElem.SetAttribute("xmlns:xsi", PackagePropertiesPart.NAMESPACE_XSI_URI); xmlDoc.AppendChild(rootElem); AddCategory(); AddContentStatus(); AddContentType(); AddCreated(); AddCreator(); AddDescription(); AddIdentifier(); AddKeywords(); AddLanguage(); AddLastModifiedBy(); AddLastPrinted(); AddModified(); AddRevision(); AddSubject(); AddTitle(); AddVersion(); return(true); }
/** * Marshall package core properties to an XML document. Always return * <code>true</code>. */ public virtual bool Marshall(PackagePart part, Stream out1) { if (!(part is PackagePropertiesPart)) throw new ArgumentException( "'part' must be a PackagePropertiesPart instance."); propsPart = (PackagePropertiesPart)part; // Configure the document xmlDoc = new XmlDocument(); XmlElement rootElem = xmlDoc.CreateElement("coreProperties",namespaceCoreProperties); nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("cp",PackagePropertiesPart.NAMESPACE_CP_URI); nsmgr.AddNamespace("dc",PackagePropertiesPart.NAMESPACE_DC_URI); nsmgr.AddNamespace("dcterms",PackagePropertiesPart.NAMESPACE_DCTERMS_URI); nsmgr.AddNamespace("xsi", PackagePropertiesPart.NAMESPACE_XSI_URI); rootElem.SetAttribute("xmlns:cp", PackagePropertiesPart.NAMESPACE_CP_URI); rootElem.SetAttribute("xmlns:dc", PackagePropertiesPart.NAMESPACE_DC_URI); rootElem.SetAttribute("xmlns:dcterms", PackagePropertiesPart.NAMESPACE_DCTERMS_URI); rootElem.SetAttribute("xmlns:xsi", PackagePropertiesPart.NAMESPACE_XSI_URI); xmlDoc.AppendChild(rootElem); AddCategory(); AddContentStatus(); AddContentType(); AddCreated(); AddCreator(); AddDescription(); AddIdentifier(); AddKeywords(); AddLanguage(); AddLastModifiedBy(); AddLastPrinted(); AddModified(); AddRevision(); AddSubject(); AddTitle(); AddVersion(); return true; }
internal CoreProperties(PackagePropertiesPart part) { this.part = part; }
// TODO Load element with XMLBeans or dynamic table // TODO Check every element/namespace for compliance public PackagePart Unmarshall(UnmarshallContext context, Stream in1) { PackagePropertiesPart coreProps = new PackagePropertiesPart(context .Package, context.PartName); // If the input stream is null then we try to get it from the // package. if (in1 == null) { if (context.ZipEntry != null) { in1 = ((ZipPackage)context.Package).ZipArchive .GetInputStream(context.ZipEntry); } else if (context.Package != null) { // Try to retrieve the part inputstream from the URI ZipEntry zipEntry; try { zipEntry = ZipHelper .GetCorePropertiesZipEntry((ZipPackage)context .Package); } catch (OpenXml4NetException) { throw new IOException( "Error while trying to get the part input stream."); } in1 = ((ZipPackage)context.Package).ZipArchive .GetInputStream(zipEntry); } else throw new IOException( "Error while trying to get the part input stream."); } XmlDocument xmlDoc = null; try { xmlDoc = DocumentHelper.LoadDocument(in1); nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("cp", namespaceCP); nsmgr.AddNamespace("dc", namespaceDC); nsmgr.AddNamespace("dcterms", namespaceDcTerms); nsmgr.AddNamespace("xsi", namespaceXSI); nsmgr.AddNamespace("cml", PackageNamespaces.MARKUP_COMPATIBILITY); nsmgr.AddNamespace("dcmitype", PackageNamespaces.DCMITYPE); //xmlDoc.ReadNode(reader); //try { //xmlDoc = xmlReader.read(in1); /* Check OPC compliance */ // Rule M4.2, M4.3, M4.4 and M4.5/ CheckElementForOPCCompliance(xmlDoc.DocumentElement); /* End OPC compliance */ //} catch (DocumentException e) { // throw new IOException(e.getMessage()); //} } catch (XmlException ex) { if (ex.Message.Contains("MaxCharactersFromEntities")) { // } else throw ex; } if (xmlDoc!=null && xmlDoc.DocumentElement != null) { coreProps.SetCategoryProperty(LoadCategory(xmlDoc)); coreProps.SetContentStatusProperty(LoadContentStatus(xmlDoc)); coreProps.SetContentTypeProperty(LoadContentType(xmlDoc)); coreProps.SetCreatedProperty(LoadCreated(xmlDoc)); coreProps.SetCreatorProperty(LoadCreator(xmlDoc)); coreProps.SetDescriptionProperty(LoadDescription(xmlDoc)); coreProps.SetIdentifierProperty(LoadIdentifier(xmlDoc)); coreProps.SetKeywordsProperty(LoadKeywords(xmlDoc)); coreProps.SetLanguageProperty(LoadLanguage(xmlDoc)); coreProps.SetLastModifiedByProperty(LoadLastModifiedBy(xmlDoc)); coreProps.SetLastPrintedProperty(LoadLastPrinted(xmlDoc)); coreProps.SetModifiedProperty(LoadModified(xmlDoc)); coreProps.SetRevisionProperty(LoadRevision(xmlDoc)); coreProps.SetSubjectProperty(LoadSubject(xmlDoc)); coreProps.SetTitleProperty(LoadTitle(xmlDoc)); coreProps.SetVersionProperty(LoadVersion(xmlDoc)); } return coreProps; }
// TODO Load element with XMLBeans or dynamic table // TODO Check every element/namespace for compliance public PackagePart Unmarshall(UnmarshallContext context, Stream in1) { PackagePropertiesPart coreProps = new PackagePropertiesPart(context .Package, context.PartName); // If the input stream is null then we try to get it from the // package. if (in1 == null) { if (context.ZipEntry != null) { in1 = ((ZipPackage)context.Package).ZipArchive .GetInputStream(context.ZipEntry); } else if (context.Package != null) { // Try to retrieve the part inputstream from the URI ZipEntry zipEntry; try { zipEntry = ZipHelper .GetCorePropertiesZipEntry((ZipPackage)context .Package); } catch (OpenXml4NetException) { throw new IOException( "Error while trying to get the part input stream."); } in1 = ((ZipPackage)context.Package).ZipArchive .GetInputStream(zipEntry); } else { throw new IOException( "Error while trying to get the part input stream."); } } XmlDocument xmlDoc = null; try { xmlDoc = DocumentHelper.LoadDocument(in1); nsmgr = new XmlNamespaceManager(xmlDoc.NameTable); nsmgr.AddNamespace("cp", namespaceCP); nsmgr.AddNamespace("dc", namespaceDC); nsmgr.AddNamespace("dcterms", namespaceDcTerms); nsmgr.AddNamespace("xsi", namespaceXSI); nsmgr.AddNamespace("cml", PackageNamespaces.MARKUP_COMPATIBILITY); nsmgr.AddNamespace("dcmitype", PackageNamespaces.DCMITYPE); //xmlDoc.ReadNode(reader); //try { //xmlDoc = xmlReader.read(in1); /* Check OPC compliance */ // Rule M4.2, M4.3, M4.4 and M4.5/ CheckElementForOPCCompliance(xmlDoc.DocumentElement); /* End OPC compliance */ //} catch (DocumentException e) { // throw new IOException(e.getMessage()); //} } catch (XmlException ex) { throw new IOException(ex.Message, ex); } if (xmlDoc != null && xmlDoc.DocumentElement != null) { coreProps.SetCategoryProperty(LoadCategory(xmlDoc)); coreProps.SetContentStatusProperty(LoadContentStatus(xmlDoc)); coreProps.SetContentTypeProperty(LoadContentType(xmlDoc)); coreProps.SetCreatedProperty(LoadCreated(xmlDoc)); coreProps.SetCreatorProperty(LoadCreator(xmlDoc)); coreProps.SetDescriptionProperty(LoadDescription(xmlDoc)); coreProps.SetIdentifierProperty(LoadIdentifier(xmlDoc)); coreProps.SetKeywordsProperty(LoadKeywords(xmlDoc)); coreProps.SetLanguageProperty(LoadLanguage(xmlDoc)); coreProps.SetLastModifiedByProperty(LoadLastModifiedBy(xmlDoc)); coreProps.SetLastPrintedProperty(LoadLastPrinted(xmlDoc)); coreProps.SetModifiedProperty(LoadModified(xmlDoc)); coreProps.SetRevisionProperty(LoadRevision(xmlDoc)); coreProps.SetSubjectProperty(LoadSubject(xmlDoc)); coreProps.SetTitleProperty(LoadTitle(xmlDoc)); coreProps.SetVersionProperty(LoadVersion(xmlDoc)); } return(coreProps); }