Ejemplo n.º 1
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");

            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(XDocument.Parse(doc.OuterXml), corePart.GetOutputStream());
            pkg.Close();

            ZipFileAssert.AssertEqual(expectedFile, targetFile);
            File.Delete(targetFile.FullName);
        }
Ejemplo n.º 2
0
 /**
  * Commit Changes to the underlying OPC namespace
  */
 public virtual void Commit()
 {
     if (extPart == null && !NEW_EXT_INSTANCE.ToString().Equals(ext.props.ToString()))
     {
         try
         {
             PackagePartName prtname = PackagingUriHelper.CreatePartName("/docProps/app.xml");
             pkg.AddRelationship(prtname, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties");
             extPart = pkg.CreatePart(prtname, "application/vnd.openxmlformats-officedocument.extended-properties+xml");
         }
         catch (InvalidFormatException e)
         {
             throw new POIXMLException(e);
         }
     }
     if (custPart == null && !NEW_CUST_INSTANCE.ToString().Equals(cust.props.ToString()))
     {
         try
         {
             PackagePartName prtname = PackagingUriHelper.CreatePartName("/docProps/custom.xml");
             pkg.AddRelationship(prtname, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties");
             custPart = pkg.CreatePart(prtname, "application/vnd.openxmlformats-officedocument.custom-properties+xml");
         }
         catch (InvalidFormatException e)
         {
             throw new POIXMLException(e);
         }
     }
     if (extPart != null)
     {
         Stream out1 = extPart.GetOutputStream();
         ext.props.Save(out1);
         out1.Close();
     }
     if (custPart != null)
     {
         Stream out1 = custPart.GetOutputStream();
         cust.props.Save(out1);
         out1.Close();
     }
 }
Ejemplo n.º 3
0
 protected static OPCPackage newPackage()
 {
     try
     {
         OPCPackage      opcPackage = OPCPackage.Create((Stream) new MemoryStream());
         PackagePartName partName   = PackagingUriHelper.CreatePartName(XSSFRelation.WORKBOOK.DefaultFileName);
         opcPackage.AddRelationship(partName, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument");
         opcPackage.CreatePart(partName, XSSFRelation.WORKBOOK.ContentType);
         opcPackage.GetPackageProperties().SetCreatorProperty(POIXMLDocument.DOCUMENT_CREATOR);
         return(opcPackage);
     }
     catch (Exception ex)
     {
         throw new POIXMLException(ex);
     }
 }
Ejemplo n.º 4
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);
        }
        public void TestAddRelationshipRelationshipsPartAssert_Failure()
        {
            OPCPackage      pkg   = OPCPackage.Create("DELETEIFEXISTS.docx");
            PackagePartName name1 = null;

            try {
                name1 = PackagingUriHelper
                        .CreatePartName("/test/_rels/document.xml.rels");
            }
            catch (InvalidFormatException e) {
                Assert.Fail("This exception should never happen !");
            }

            try {
                pkg.AddRelationship(name1, TargetMode.Internal,
                                    PackageRelationshipTypes.CORE_DOCUMENT);
            }
            catch (InvalidOperationException e) {
                return;
            }
            Assert.Fail("Assert.Fail test -> M1.25: The Relationships part shall not have relationships to any other part");
        }
Ejemplo n.º 6
0
        //[Ignore("add relation Uri #Sheet1!A1")]
        public void TestCreatePackageWithCoreDocument()
        {
            MemoryStream baos = new MemoryStream();
            OPCPackage   pkg  = OPCPackage.Create(baos);

            // Add a core document
            PackagePartName corePartName = PackagingUriHelper.CreatePartName("/xl/workbook.xml");

            // Create main part relationship
            pkg.AddRelationship(corePartName, TargetMode.Internal, PackageRelationshipTypes.CORE_DOCUMENT, "rId1");
            // Create main document part
            PackagePart corePart = pkg.CreatePart(corePartName, "application/vnd.Openxmlformats-officedocument.spreadsheetml.sheet.main+xml");
            // Put in some dummy content
            Stream coreOut = corePart.GetOutputStream();

            byte[] buffer = Encoding.UTF8.GetBytes("<dummy-xml />");
            coreOut.Write(buffer, 0, buffer.Length);
            coreOut.Close();

            // And another bit
            PackagePartName     sheetPartName = PackagingUriHelper.CreatePartName("/xl/worksheets/sheet1.xml");
            PackageRelationship rel           =
                corePart.AddRelationship(sheetPartName, TargetMode.Internal, "http://schemas.Openxmlformats.org/officeDocument/2006/relationships/worksheet", "rSheet1");

            Assert.IsNotNull(rel);

            PackagePart part = pkg.CreatePart(sheetPartName, "application/vnd.Openxmlformats-officedocument.spreadsheetml.worksheet+xml");

            Assert.IsNotNull(part);

            // Dummy content again
            coreOut = corePart.GetOutputStream();
            buffer  = Encoding.UTF8.GetBytes("<dummy-xml2 />");
            coreOut.Write(buffer, 0, buffer.Length);
            coreOut.Close();

            //add a relationship with internal target: "#Sheet1!A1"
            corePart.AddRelationship(PackagingUriHelper.ToUri("#Sheet1!A1"), TargetMode.Internal, "http://schemas.Openxmlformats.org/officeDocument/2006/relationships/hyperlink", "rId2");

            // Check things are as expected
            PackageRelationshipCollection coreRels =
                pkg.GetRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);

            Assert.AreEqual(1, coreRels.Size);
            PackageRelationship coreRel = coreRels.GetRelationship(0);

            Assert.IsNotNull(coreRel);
            Assert.AreEqual("/", coreRel.SourceUri.ToString());
            Assert.AreEqual("/xl/workbook.xml", coreRel.TargetUri.ToString());
            Assert.IsNotNull(pkg.GetPart(coreRel));


            // Save and re-load
            pkg.Close();
            FileInfo   tmp  = TempFile.CreateTempFile("testCreatePackageWithCoreDocument", ".zip");
            FileStream fout = new FileStream(tmp.FullName, FileMode.Create, FileAccess.ReadWrite);

            try
            {
                buffer = baos.ToArray();
                fout.Write(buffer, 0, buffer.Length);
            }
            finally
            {
                fout.Close();
            }
            pkg = OPCPackage.Open(tmp.FullName);
            //tmp.Delete();

            try
            {
                // Check still right
                coreRels = pkg.GetRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);
                Assert.AreEqual(1, coreRels.Size);
                coreRel = coreRels.GetRelationship(0);
                Assert.IsNotNull(coreRel);

                Assert.AreEqual("/", coreRel.SourceUri.ToString());
                Assert.AreEqual("/xl/workbook.xml", coreRel.TargetUri.ToString());
                corePart = pkg.GetPart(coreRel);
                Assert.IsNotNull(corePart);

                PackageRelationshipCollection rels = corePart.GetRelationshipsByType("http://schemas.Openxmlformats.org/officeDocument/2006/relationships/hyperlink");
                Assert.AreEqual(1, rels.Size);
                rel = rels.GetRelationship(0);
                Assert.IsNotNull(rel);
                Assert.Warn(" 'Sheet1!A1' and rel.TargetUri.Fragment should be equal.");
                //Assert.AreEqual("Sheet1!A1", rel.TargetUri.Fragment);

                assertMSCompatibility(pkg);
            }
            finally
            {
                pkg.Close();
            }

            Assert.AreEqual(0, Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.tmp").Length, "At Last: There are no temporary files.");
        }