Exemplo n.º 1
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();
            }
        }
Exemplo n.º 2
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();
                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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        /**
         * TODO: fix and enable
         */
        //[Test]
        public void disabled_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);
        }
Exemplo n.º 5
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.");
        }