From 5380aee6516becc230f9f8c7cf281517b2fe324f Mon Sep 17 00:00:00 2001 From: Przemyslaw Klys Date: Fri, 14 Oct 2022 12:37:24 +0200 Subject: [PATCH] Fixes issue with read only documents crashing --- CHANGELOG.MD | 1 + OfficeIMO.Examples/Program.cs | 9 ++---- OfficeIMO.Tests/Word.Images.cs | 2 +- OfficeIMO.Word/WordDocument.cs | 50 ++++++++++++++++------------------ 4 files changed, 27 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index b0f31305..37bd3c1c 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -1,5 +1,6 @@ #### 0.3.1 - 2022.10.14 - Adds BMP, GIF, PNG, TIFF image formats support [#42](https://github.com/EvotecIT/OfficeIMO/pull/42) by rstm-sf +- Fixes issue with read only documents crashing #### 0.3.0 - 2022.10.11 - Update DocumentFormat.OpenXml from 2.16.0 to 2.18.0 diff --git a/OfficeIMO.Examples/Program.cs b/OfficeIMO.Examples/Program.cs index 8e0bd4a8..ef3e3a7e 100644 --- a/OfficeIMO.Examples/Program.cs +++ b/OfficeIMO.Examples/Program.cs @@ -95,13 +95,8 @@ static void Main(string[] args) { //filePath = System.IO.Path.Combine(folderPath, "AdvancedParagraphs.docx"); //Example_MultipleParagraphsViaDifferentWays(filePath, false); - var openWordWithImages = false; - Images.Example_AddingImages(folderPath, openWordWithImages); - if (openWordWithImages != true) { - // this example is based on the example above, so opening word would break the example - // This requires additional fixes to work properly, different PR will be needed - //Images.Example_ReadWordWithImages(); - } + Images.Example_AddingImages(folderPath, true); + Images.Example_ReadWordWithImages(); Images.Example_AddingImagesMultipleTypes(folderPath, true); //Console.WriteLine("[*] Creating standard document with page breaks and removing them"); //filePath = System.IO.Path.Combine(folderPath, "Basic Document with some page breaks.docx"); diff --git a/OfficeIMO.Tests/Word.Images.cs b/OfficeIMO.Tests/Word.Images.cs index 7f19e355..507a8ce9 100644 --- a/OfficeIMO.Tests/Word.Images.cs +++ b/OfficeIMO.Tests/Word.Images.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using DocumentFormat.OpenXml.Drawing; using OfficeIMO.Word; diff --git a/OfficeIMO.Word/WordDocument.cs b/OfficeIMO.Word/WordDocument.cs index 901f9956..3e35f014 100644 --- a/OfficeIMO.Word/WordDocument.cs +++ b/OfficeIMO.Word/WordDocument.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.IO.Packaging; @@ -29,8 +29,7 @@ public WordTableOfContent TableOfContent { get { var sdtBlocks = _document.Body?.ChildElements.OfType() ?? Enumerable.Empty(); - foreach (var sdtBlock in sdtBlocks) - { + foreach (var sdtBlock in sdtBlocks) { var sdtProperties = sdtBlock?.ChildElements.OfType().FirstOrDefault(); var docPartObject = sdtProperties?.ChildElements.OfType().FirstOrDefault(); var docPartGallery = docPartObject?.ChildElements.OfType().FirstOrDefault(); @@ -48,8 +47,7 @@ public WordCoverPage CoverPage { get { var sdtBlocks = _document.Body?.ChildElements.OfType() ?? Enumerable.Empty(); - foreach (var sdtBlock in sdtBlocks) - { + foreach (var sdtBlock in sdtBlocks) { var sdtProperties = sdtBlock?.ChildElements.OfType().FirstOrDefault(); var docPartObject = sdtProperties?.ChildElements.OfType().FirstOrDefault(); var docPartGallery = docPartObject?.ChildElements.OfType().FirstOrDefault(); @@ -487,7 +485,7 @@ public static WordDocument Load(string filePath, bool readOnly = false, bool aut var wordDocument = WordprocessingDocument.Open(memoryStream, !readOnly, openSettings); - InitialiseStyleDefinitions(wordDocument); + InitialiseStyleDefinitions(wordDocument, readOnly); word.FilePath = filePath; word._wordprocessingDocument = wordDocument; @@ -496,17 +494,15 @@ public static WordDocument Load(string filePath, bool readOnly = false, bool aut return word; } - public static WordDocument Load(Stream stream, bool readOnly = false, bool autoSave = false) - { + public static WordDocument Load(Stream stream, bool readOnly = false, bool autoSave = false) { var document = new WordDocument(); - var openSettings = new OpenSettings - { + var openSettings = new OpenSettings { AutoSave = autoSave }; var wordDocument = WordprocessingDocument.Open(stream, !readOnly, openSettings); - InitialiseStyleDefinitions(wordDocument); + InitialiseStyleDefinitions(wordDocument, readOnly); document._wordprocessingDocument = wordDocument; document._document = wordDocument.MainDocumentPart.Document; @@ -638,12 +634,10 @@ public void Save(bool openWord) { this.Save("", openWord); } - public void Save(Stream outputStream) - { + public void Save(Stream outputStream) { this._wordprocessingDocument.Clone(outputStream); - if (outputStream.CanSeek) - { + if (outputStream.CanSeek) { outputStream.Seek(0, SeekOrigin.Begin); } } @@ -678,17 +672,19 @@ public void Dispose() { } } - private static void InitialiseStyleDefinitions(WordprocessingDocument wordDocument) - { - var styleDefinitionsPart = wordDocument.MainDocumentPart.GetPartsOfType().FirstOrDefault(); - if (styleDefinitionsPart != null) - { - AddStyleDefinitions(styleDefinitionsPart); - } - else - { - var styleDefinitionsPart1 = wordDocument.MainDocumentPart.AddNewPart("rId1"); - GenerateStyleDefinitionsPart1Content(styleDefinitionsPart1); + private static void InitialiseStyleDefinitions(WordprocessingDocument wordDocument, bool readOnly) { + // if document is read only we shouldn't be doing any new styles, hopefully it doesn't break anything + if (readOnly == false) { + var styleDefinitionsPart = wordDocument.MainDocumentPart.GetPartsOfType() + .FirstOrDefault(); + if (styleDefinitionsPart != null) { + AddStyleDefinitions(styleDefinitionsPart); + } else { + + var styleDefinitionsPart1 = wordDocument.MainDocumentPart.AddNewPart("rId1"); + GenerateStyleDefinitionsPart1Content(styleDefinitionsPart1); + + } } } @@ -719,4 +715,4 @@ public List DocumentValidationErrors { } } } -} \ No newline at end of file +}