public void TestSetProperties() { String inputPath = OpenXml4NetTestDataSamples.GetSampleFileName("TestPackageCoreProperiesSetters.docx"); FileInfo outputFile = OpenXml4NetTestDataSamples.GetOutputFile("TestPackageCoreProperiesSettersOUTPUT.docx"); // Open namespace OPCPackage p = OPCPackage.Open(inputPath, PackageAccess.READ_WRITE); try { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); DateTime dateToInsert = DateTime.Parse("2007-05-12T08:00:00Z").ToUniversalTime(); PackageProperties props = p.GetPackageProperties(); props.SetCategoryProperty("MyCategory"); props.SetContentStatusProperty("MyContentStatus"); props.SetContentTypeProperty("MyContentType"); props.SetCreatedProperty(new DateTime?(dateToInsert)); props.SetCreatorProperty("MyCreator"); props.SetDescriptionProperty("MyDescription"); props.SetIdentifierProperty("MyIdentifier"); props.SetKeywordsProperty("MyKeywords"); props.SetLanguageProperty("MyLanguage"); props.SetLastModifiedByProperty("Julien Chable"); props.SetLastPrintedProperty(new Nullable <DateTime>(dateToInsert)); props.SetModifiedProperty(new Nullable <DateTime>(dateToInsert)); props.SetRevisionProperty("2"); props.SetTitleProperty("MyTitle"); props.SetSubjectProperty("MySubject"); props.SetVersionProperty("2"); using (FileStream fs = outputFile.OpenWrite()) { // Save the namespace in the output directory p.Save(fs); } // Open the newly Created file to check core properties saved values. OPCPackage p2 = OPCPackage.Open(outputFile.Name, PackageAccess.READ); try { CompareProperties(p2); p2.Revert(); } finally { p2.Close(); } outputFile.Delete(); } finally { // use revert to not re-write the input file p.Revert(); } }
public void TestSetProperties() { String inputPath = OpenXml4NetTestDataSamples.GetSampleFileName("TestPackageCoreProperiesSetters.docx"); FileInfo outputFile = OpenXml4NetTestDataSamples.GetOutputFile("TestPackageCoreProperiesSettersOUTPUT.docx"); // Open namespace OPCPackage p = OPCPackage.Open(inputPath, PackageAccess.READ_WRITE); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); df.TimeZone = TimeZoneInfo.Utc; DateTime dateToInsert = df.Parse("2007-05-12T08:00:00Z"); SimpleDateFormat msdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.fff'Z'"); msdf.TimeZone = TimeZoneInfo.Utc; PackageProperties props = p.GetPackageProperties(); //test various date formats props.SetCreatedProperty("2007-05-12T08:00:00Z"); Assert.AreEqual(dateToInsert, props.GetCreatedProperty().Value); props.SetCreatedProperty("2007-05-12T08:00:00"); //no Z, assume Z Assert.AreEqual(dateToInsert, props.GetCreatedProperty().Value); props.SetCreatedProperty("2007-05-12T08:00:00.123Z");//millis Assert.AreEqual(msdf.Parse("2007-05-12T08:00:00.123Z"), props.GetCreatedProperty().Value); props.SetCreatedProperty("2007-05-12T10:00:00+0200"); Assert.AreEqual(dateToInsert, props.GetCreatedProperty().Value); props.SetCreatedProperty("2007-05-12T10:00:00+02:00");//colon in tz Assert.AreEqual(dateToInsert, props.GetCreatedProperty().Value); props.SetCreatedProperty("2007-05-12T06:00:00-0200"); Assert.AreEqual(dateToInsert, props.GetCreatedProperty().Value); props.SetCreatedProperty("2007-05-12T10:00:00.123+0200"); Assert.AreEqual(msdf.Parse("2007-05-12T08:00:00.123Z"), props.GetCreatedProperty().Value); props.SetCategoryProperty("MyCategory"); props.SetCategoryProperty("MyCategory"); props.SetContentStatusProperty("MyContentStatus"); props.SetContentTypeProperty("MyContentType"); //props.SetCreatedProperty(new DateTime?(dateToInsert)); props.SetCreatorProperty("MyCreator"); props.SetDescriptionProperty("MyDescription"); props.SetIdentifierProperty("MyIdentifier"); props.SetKeywordsProperty("MyKeywords"); props.SetLanguageProperty("MyLanguage"); props.SetLastModifiedByProperty("Julien Chable"); props.SetLastPrintedProperty(new Nullable <DateTime>(dateToInsert)); props.SetModifiedProperty(new Nullable <DateTime>(dateToInsert)); props.SetRevisionProperty("2"); props.SetTitleProperty("MyTitle"); props.SetSubjectProperty("MySubject"); props.SetVersionProperty("2"); // Save the package in the output directory p.Save(outputFile.FullName); p.Revert(); // Open the newly Created file to check core properties saved values. OPCPackage p2 = OPCPackage.Open(outputFile.FullName, PackageAccess.READ); CompareProperties(p2); p2.Revert(); outputFile.Delete(); }