Пример #1
0
        public void TestSetProperties()
        {
            String inputPath = OpenXml4NetTestDataSamples.GetSampleFileName("TestPackageThumbnail.docx");

            String imagePath = OpenXml4NetTestDataSamples.GetSampleFileName("thumbnail.jpg");

            FileInfo outputFile = OpenXml4NetTestDataSamples.GetOutputFile("TestPackageThumbnailOUTPUT.docx");

            // Open namespace
            OPCPackage p  = OPCPackage.Open(inputPath, PackageAccess.READ_WRITE);
            FileStream fs = outputFile.OpenWrite();

            p.AddThumbnail(imagePath);
            // 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);

            if (p2.GetRelationshipsByType(PackageRelationshipTypes.THUMBNAIL)
                .Size == 0)
            {
                Assert.Fail("Thumbnail not Added to the namespace !");
            }
            p2.Revert();
            File.Delete(outputFile.Name);
        }
Пример #2
0
        public void TestCreateGetsContentTypes()
        {
            FileInfo targetFile = OpenXml4NetTestDataSamples.GetOutputFile("TestCreatePackageTMP.docx");

            // Zap the target file, in case of an earlier run
            if (File.Exists(targetFile.FullName))
            {
                File.Delete(targetFile.FullName);
                Assert.IsFalse(File.Exists(targetFile.FullName));
            }


            OPCPackage pkg = OPCPackage.Create(targetFile.FullName);

            // Check it has content types for rels and xml
            ContentTypeManager ctm = GetContentTypeManager(pkg);

            Assert.AreEqual(
                "application/xml",
                ctm.GetContentType(
                    PackagingUriHelper.CreatePartName("/foo.xml")
                    )
                );
            Assert.AreEqual(
                ContentTypes.RELATIONSHIPS_PART,
                ctm.GetContentType(
                    PackagingUriHelper.CreatePartName("/foo.rels")
                    )
                );
            Assert.IsNull(
                ctm.GetContentType(
                    PackagingUriHelper.CreatePartName("/foo.txt")
                    )
                );
        }
Пример #3
0
        public void TestSaveToOutputStream()
        {
            String   originalFile = OpenXml4NetTestDataSamples.GetSampleFileName("TestPackageCommon.docx");
            FileInfo targetFile   = OpenXml4NetTestDataSamples.GetOutputFile("TestPackageOpenSaveTMP.docx");

            OPCPackage p = OPCPackage.Open(originalFile, PackageAccess.READ_WRITE);

            try
            {
                FileStream fs = targetFile.OpenWrite();
                try
                {
                    p.Save(fs);
                }
                finally
                {
                    fs.Close();
                }

                // Compare the original and newly saved document
                Assert.IsTrue(File.Exists(targetFile.FullName));
                ZipFileAssert.AssertEqual(new FileInfo(originalFile), targetFile);
                File.Delete(targetFile.FullName);
            }
            finally
            {
                p.Revert();
            }
        }
Пример #4
0
        public void TestOpenPackage()
        {
            FileInfo targetFile = OpenXml4NetTestDataSamples.GetOutputFile("TestOpenPackageTMP.docx");

            FileInfo inputFile = OpenXml4NetTestDataSamples.GetSampleFile("TestOpenPackageINPUT.docx");

            FileInfo expectedFile = OpenXml4NetTestDataSamples.GetSampleFile("TestOpenPackageOUTPUT.docx");

            // Copy the input file in the output directory
            FileHelper.CopyFile(inputFile.FullName, targetFile.FullName);

            // Create a namespace
            OPCPackage pkg = OPCPackage.Open(targetFile.FullName);

            // Modify core part
            PackagePartName corePartName = PackagingUriHelper
                                           .CreatePartName("/word/document.xml");

            PackagePart corePart = pkg.GetPart(corePartName);

            // Delete some part to have a valid document
            foreach (PackageRelationship rel in corePart.Relationships)
            {
                corePart.RemoveRelationship(rel.Id);
                pkg.RemovePart(PackagingUriHelper.CreatePartName(PackagingUriHelper
                                                                 .ResolvePartUri(corePart.PartName.URI, rel
                                                                                 .TargetUri)));
            }

            //// Create a content
            //Document doc = DocumentHelper.CreateDocument();
            //Namespace nsWordProcessinML = new Namespace("w",
            //        "http://schemas.openxmlformats.org/wordProcessingml/2006/main");
            //Element elDocument = doc.AddElement(new QName("document",
            //        nsWordProcessinML));
            //Element elBody = elDocument.AddElement(new QName("body",
            //        nsWordProcessinML));
            //Element elParagraph = elBody.AddElement(new QName("p",
            //        nsWordProcessinML));
            //Element elRun = elParagraph
            //        .AddElement(new QName("r", nsWordProcessinML));
            //Element elText = elRun.AddElement(new QName("t", nsWordProcessinML));
            //elText.SetText("Hello Open XML !");

            //StreamHelper.saveXmlInStream(doc, corePart.GetOutputStream());

            //// Save and close
            //try
            //{
            //    pkg.Close();
            //}
            //catch (IOException e)
            //{
            //    Assert.Fail();
            //}

            //ZipFileAssert.AssertEqual(expectedFile, targetFile);
            //File.Delete(targetFile.FullName);
        }
Пример #5
0
        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();
            }
        }
Пример #6
0
        public void TestCreatePackageAddPart()
        {
            FileInfo targetFile = OpenXml4NetTestDataSamples.GetOutputFile("TestCreatePackageTMP.docx");

            FileInfo expectedFile = OpenXml4NetTestDataSamples.GetSampleFile("TestCreatePackageOUTPUT.docx");

            // Zap the target file, in case of an earlier run
            if (targetFile.Exists)
            {
                targetFile.Delete();
                targetFile.Refresh();
                Assert.IsFalse(targetFile.Exists);
            }

            // Create a namespace
            OPCPackage      pkg          = OPCPackage.Create(targetFile.FullName);
            PackagePartName corePartName = PackagingUriHelper
                                           .CreatePartName("/word/document.xml");

            pkg.AddRelationship(corePartName, TargetMode.Internal,
                                PackageRelationshipTypes.CORE_DOCUMENT, "rId1");

            PackagePart corePart = pkg
                                   .CreatePart(
                corePartName,
                "application/vnd.openxmlformats-officedocument.wordProcessingml.document.main+xml");

            XmlDocument doc = new XmlDocument();

            XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
            string wuri             = "http://schemas.openxmlformats.org/wordProcessingml/2006/main";

            mgr.AddNamespace("w", wuri);
            XmlElement elDocument = doc.CreateElement("w:document", wuri);

            doc.AppendChild(elDocument);
            XmlElement elBody = doc.CreateElement("w:body", wuri);

            elDocument.AppendChild(elBody);
            XmlElement elParagraph = doc.CreateElement("w:p", wuri);

            elBody.AppendChild(elParagraph);
            XmlElement elRun = doc.CreateElement("w:r", wuri);

            elParagraph.AppendChild(elRun);
            XmlElement elText = doc.CreateElement("w:t", wuri);

            elRun.AppendChild(elText);
            elText.InnerText = ("Hello Open XML !");

            StreamHelper.SaveXmlInStream(doc, corePart.GetOutputStream());
            pkg.Close();

            ZipFileAssert.AssertEqual(expectedFile, targetFile);
            File.Delete(targetFile.FullName);
        }
Пример #7
0
        public void TestOpenSave()
        {
            String   originalFile = OpenXml4NetTestDataSamples.GetSampleFileName("TestPackageCommon.docx");
            FileInfo targetFile   = OpenXml4NetTestDataSamples.GetOutputFile("TestPackageOpenSaveTMP.docx");

            OPCPackage p = OPCPackage.Open(originalFile, PackageAccess.READ_WRITE);

            p.Save(targetFile.FullName);

            // Compare the original and newly saved document
            Assert.IsTrue(File.Exists(targetFile.FullName));
            ZipFileAssert.AssertEqual(new FileInfo(originalFile), targetFile);
            File.Delete(targetFile.FullName);
        }
Пример #8
0
        public void TestRemovePartRecursive()
        {
            String   originalFile = OpenXml4NetTestDataSamples.GetSampleFileName("TestPackageCommon.docx");
            FileInfo targetFile   = OpenXml4NetTestDataSamples.GetOutputFile("TestPackageRemovePartRecursiveOUTPUT.docx");
            FileInfo tempFile     = OpenXml4NetTestDataSamples.GetOutputFile("TestPackageRemovePartRecursiveTMP.docx");

            OPCPackage p = OPCPackage.Open(originalFile, PackageAccess.READ_WRITE);

            p.RemovePartRecursive(PackagingUriHelper.CreatePartName(new Uri(
                                                                        "/word/document.xml", UriKind.Relative)));
            p.Save(tempFile.FullName);

            // Compare the original and newly saved document
            Assert.IsTrue(File.Exists(targetFile.FullName));
            ZipFileAssert.AssertEqual(targetFile, tempFile);
            File.Delete(targetFile.FullName);
        }
Пример #9
0
        public void TestCreatePackageAddPart()
        {
            FileInfo targetFile = OpenXml4NetTestDataSamples.GetOutputFile("TestCreatePackageTMP.docx");

            FileInfo expectedFile = OpenXml4NetTestDataSamples.GetSampleFile("TestCreatePackageOUTPUT.docx");

            // Zap the target file, in case of an earlier run
            if (targetFile.Exists)
            {
                targetFile.Delete();
            }

            // Create a namespace
            OPCPackage      pkg          = OPCPackage.Create(targetFile.FullName);
            PackagePartName corePartName = PackagingUriHelper
                                           .CreatePartName("/word/document.xml");

            pkg.AddRelationship(corePartName, TargetMode.Internal,
                                PackageRelationshipTypes.CORE_DOCUMENT, "rId1");

            PackagePart corePart = pkg
                                   .CreatePart(
                corePartName,
                "application/vnd.openxmlformats-officedocument.wordProcessingml.document.main+xml");

            //Document doc = DocumentHelper.CreateDocument();
            //Namespace nsWordProcessinML = new Namespace("w",
            //        "http://schemas.openxmlformats.org/wordProcessingml/2006/main");
            //Element elDocument = doc.AddElement(new QName("document",
            //        nsWordProcessinML));
            //Element elBody = elDocument.AddElement(new QName("body",
            //        nsWordProcessinML));
            //Element elParagraph = elBody.AddElement(new QName("p",
            //        nsWordProcessinML));
            //Element elRun = elParagraph
            //        .AddElement(new QName("r", nsWordProcessinML));
            //Element elText = elRun.AddElement(new QName("t", nsWordProcessinML));
            //elText.SetText("Hello Open XML !");

            //StreamHelper.SaveXmlInStream(doc, corePart.GetOutputStream());
            //pkg.Close();

            //ZipFileAssert.AssertEqual(expectedFile, targetFile);
            //File.Delete(targetFile.FullName);
        }
Пример #10
0
        public void TestOpenPackage()
        {
            FileInfo targetFile = OpenXml4NetTestDataSamples.GetOutputFile("TestOpenPackageTMP.docx");

            FileInfo inputFile = OpenXml4NetTestDataSamples.GetSampleFile("TestOpenPackageINPUT.docx");

            FileInfo expectedFile = OpenXml4NetTestDataSamples.GetSampleFile("TestOpenPackageOUTPUT.docx");

            // Copy the input file in the output directory
            FileHelper.CopyFile(inputFile.FullName, targetFile.FullName);

            // Create a namespace
            OPCPackage pkg = OPCPackage.Open(targetFile.FullName);

            // Modify core part
            PackagePartName corePartName = PackagingUriHelper
                                           .CreatePartName("/word/document.xml");

            PackagePart corePart = pkg.GetPart(corePartName);

            // Delete some part to have a valid document
            foreach (PackageRelationship rel in corePart.Relationships)
            {
                corePart.RemoveRelationship(rel.Id);
                pkg.RemovePart(PackagingUriHelper.CreatePartName(PackagingUriHelper
                                                                 .ResolvePartUri(corePart.PartName.URI, rel
                                                                                 .TargetUri)));
            }

            // Create a content
            XmlDocument doc = new XmlDocument();

            XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
            string wuri             = "http://schemas.openxmlformats.org/wordProcessingml/2006/main";

            mgr.AddNamespace("w", wuri);
            XmlElement elDocument = doc.CreateElement("w:document", wuri);

            doc.AppendChild(elDocument);
            XmlElement elBody = doc.CreateElement("w:body", wuri);

            elDocument.AppendChild(elBody);
            XmlElement elParagraph = doc.CreateElement("w:p", wuri);

            elBody.AppendChild(elParagraph);
            XmlElement elRun = doc.CreateElement("w:r", wuri);

            elParagraph.AppendChild(elRun);
            XmlElement elText = doc.CreateElement("w:t", wuri);

            elRun.AppendChild(elText);
            elText.InnerText = ("Hello Open XML !");

            StreamHelper.SaveXmlInStream(doc, corePart.GetOutputStream());
            // Save and close
            try
            {
                pkg.Close();
            }
            catch (IOException)
            {
                Assert.Fail();
            }

            ZipFileAssert.AssertEqual(expectedFile, targetFile);
            File.Delete(targetFile.FullName);

            Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files.");
        }
Пример #11
0
        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();
        }