Example #1
0
        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 string GetCorePropertiesText()
        {
            StringBuilder         text = new StringBuilder();
            PackagePropertiesPart underlyingProperties = this.Document.GetProperties().GetCoreProperties().GetUnderlyingProperties();

            this.AppendIfPresent(text, "Category", underlyingProperties.GetCategoryProperty());
            this.AppendIfPresent(text, "Category", underlyingProperties.GetCategoryProperty());
            this.AppendIfPresent(text, "ContentStatus", underlyingProperties.GetContentStatusProperty());
            this.AppendIfPresent(text, "ContentType", underlyingProperties.GetContentTypeProperty());
            this.AppendIfPresent(text, "Created", new DateTime?(underlyingProperties.GetCreatedProperty().Value));
            this.AppendIfPresent(text, "CreatedString", underlyingProperties.GetCreatedPropertyString());
            this.AppendIfPresent(text, "Creator", underlyingProperties.GetCreatorProperty());
            this.AppendIfPresent(text, "Description", underlyingProperties.GetDescriptionProperty());
            this.AppendIfPresent(text, "Identifier", underlyingProperties.GetIdentifierProperty());
            this.AppendIfPresent(text, "Keywords", underlyingProperties.GetKeywordsProperty());
            this.AppendIfPresent(text, "Language", underlyingProperties.GetLanguageProperty());
            this.AppendIfPresent(text, "LastModifiedBy", underlyingProperties.GetLastModifiedByProperty());
            this.AppendIfPresent(text, "LastPrinted", underlyingProperties.GetLastPrintedProperty());
            this.AppendIfPresent(text, "LastPrintedString", underlyingProperties.GetLastPrintedPropertyString());
            this.AppendIfPresent(text, "Modified", underlyingProperties.GetModifiedProperty());
            this.AppendIfPresent(text, "ModifiedString", underlyingProperties.GetModifiedPropertyString());
            this.AppendIfPresent(text, "Revision", underlyingProperties.GetRevisionProperty());
            this.AppendIfPresent(text, "Subject", underlyingProperties.GetSubjectProperty());
            this.AppendIfPresent(text, "Title", underlyingProperties.GetTitleProperty());
            this.AppendIfPresent(text, "Version", underlyingProperties.GetVersionProperty());
            return(text.ToString());
        }
        /**
         * Returns the core document properties, eg author
         */
        public String GetCorePropertiesText()
        {
            if (Document == null)
            {  // event based extractor does not have a document
                return("");
            }
            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());
        }
        /**
         * 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);
        }
        /**
         * Add title property if needed.
         */
        private void AddTitle()
        {
            if (propsPart.GetTitleProperty() == null)
            {
                return;
            }

            XmlNodeList elems = xmlDoc.DocumentElement.GetElementsByTagName(KEYWORD_TITLE, namespaceDC);
            XmlElement  elem  = null;

            if (elems.Count == 0)
            {
                // Missing, we Add it
                elem = xmlDoc.CreateElement("dc", KEYWORD_TITLE, namespaceDC);
                xmlDoc.DocumentElement.AppendChild(elem);
            }
            else
            {
                elem          = (XmlElement)elems[0];
                elem.InnerXml = "";// clear the old value
            }
            elem.InnerText = propsPart.GetTitleProperty();
        }
Example #6
0
 public String GetTitle()
 {
     return(part.GetTitleProperty());
 }