private void DoSomethingToContentPartsRecursively(OPCPackage package, PartInfo part, PartProcessingDelegate whatToDo)
        {
            using (Stream data = part.GetContent())
            {
                whatToDo(data, part);
            }

            foreach (PartInfo pi in part.GetRelatedObjects())
            {
                DoSomethingToContentPartsRecursively(package, pi, whatToDo);
            }

        }
Exemplo n.º 2
0
        public void CreateForWriting()
        {
            string fileName = TESTFILE_DIR + "testCreate.docx";
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            try
            {
                using (Stream s = new FileStream(fileName, FileMode.CreateNew))
                {
                    List<DefaultInfo> ldi = new List<DefaultInfo>();
                    ldi.Add(new DefaultInfo("xls", "application/xml"));
                    ldi.Add(new DefaultInfo("rels", "application/vnd.openxmlformats-package.relationships+xml"));
                    using (OPCPackage package = new OPCPackage(s, ldi, OpenXmlFormatDefinition.OpenXmlFormat.Transitional))
                    {
                        using (Stream str = File.Open(TESTFILE_DIR + "document1.xml", FileMode.Open))
                        {
                            PartInfo rel = new PartInfo(package);
                            rel.Type = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
                            rel.Id = "rId1";
                            rel.Target = "/word/document.xml";
                            rel.RelatedTo = "";
                            package.AddContent(rel, str, OpenXmlFormatDefinition.OpenXmlFormat.Transitional);
                        }
                    }
                }
                

                Assert.IsTrue(File.Exists(fileName));
                using (OPCPackage readPackage = new OPCPackage(File.Open(fileName, FileMode.Open)))
                {
                    PartInfo rel = readPackage.GetRelatedObjects("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument")[0];
                    Stream str = rel.GetContent();
                    Assert.IsNotNull(str, "whoops, no content found");
                    DocxTestUtilities.ValidateDocxMainStream(str, TESTFILE_DIR);
                    Assert.AreEqual("word/document.xml", rel.Target);

                    Assert.AreEqual(2, readPackage.GetDefaultTypes().Count, "Expected the written package to containt eh 2 defaults in the original package");

                }
            }
            finally
            {
                File.Delete(fileName);

            }
        }