public void CanCloneDocument() { using (var file = TemporaryFile.Create()) { using (var stream = GetStream(TestFiles.Document)) using (var source = WordprocessingDocument.Open(stream, false)) using (var clone = (WordprocessingDocument)source.Clone()) { var body = clone.MainDocumentPart.Document.Body; body.InsertBefore(new Paragraph(new Run(new Text("Hello World"))), body.FirstChild); clone.SaveAs(file.Path).Close(); using (var dest = WordprocessingDocument.Open(file.Path, false)) { PackageAssert.NotEqual(source, dest); } } } }
public void CanCloneDocument() { using (var file = TemporaryFile.Create()) { using (var stream = GetStream(TestFiles.Document)) using (var source = WordprocessingDocument.Open(stream, false)) using (var clone = (WordprocessingDocument)source.Clone()) { var body = clone.MainDocumentPart.Document.Body; body.InsertBefore(new Paragraph(new Run(new Text("Hello World"))), body.FirstChild); clone.SaveAs(file.Path).Close(); using (var dest = WordprocessingDocument.Open(file.Path, false)) { // We want the documents to be different. Assert.Throws <Xunit.Sdk.EqualException>(() => PackageAssert.Equal(source, dest)); } } } }
public void CanDoMultithreadedMultipleCloning() { using (var stream = GetStream(TestFiles.Document, true)) using (var source = WordprocessingDocument.Open(stream, true)) { Parallel.For(0, 10, index => { using (var clone1 = (WordprocessingDocument)source.Clone()) { var body1 = clone1.MainDocumentPart.Document.Body; body1.GetFirstChild <Paragraph>() .InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 1")))); using (var tempFile = TemporaryFile.Create()) using (var clone2 = (WordprocessingDocument)clone1.SaveAs(tempFile.Path)) { var body2 = clone2.MainDocumentPart.Document.Body; body2.GetFirstChild <Paragraph>() .InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 2")))); } // Clone clone1 again. var clone3 = (WordprocessingDocument)clone1.Clone(); var body3 = clone3.MainDocumentPart.Document.Body; body3.GetFirstChild <Paragraph>() .InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 3")))); clone3.Close(); // Clone source again. var clone4 = (WordprocessingDocument)source.Clone(); var body4 = clone4.MainDocumentPart.Document.Body; body4.GetFirstChild <Paragraph>() .InsertBeforeSelf(new Paragraph(new Run(new Text("Clone 4")))); clone4.Close(); } }); } }