Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue with read only documents crashing #44

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 2 additions & 7 deletions OfficeIMO.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion OfficeIMO.Tests/Word.Images.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using DocumentFormat.OpenXml.Drawing;
using OfficeIMO.Word;
Expand Down
50 changes: 23 additions & 27 deletions OfficeIMO.Word/WordDocument.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Packaging;
Expand Down Expand Up @@ -29,8 +29,7 @@ public WordTableOfContent TableOfContent {
get {
var sdtBlocks = _document.Body?.ChildElements.OfType<SdtBlock>() ?? Enumerable.Empty<SdtBlock>();

foreach (var sdtBlock in sdtBlocks)
{
foreach (var sdtBlock in sdtBlocks) {
var sdtProperties = sdtBlock?.ChildElements.OfType<SdtProperties>().FirstOrDefault();
var docPartObject = sdtProperties?.ChildElements.OfType<SdtContentDocPartObject>().FirstOrDefault();
var docPartGallery = docPartObject?.ChildElements.OfType<DocPartGallery>().FirstOrDefault();
Expand All @@ -48,8 +47,7 @@ public WordCoverPage CoverPage {
get {
var sdtBlocks = _document.Body?.ChildElements.OfType<SdtBlock>() ?? Enumerable.Empty<SdtBlock>();

foreach (var sdtBlock in sdtBlocks)
{
foreach (var sdtBlock in sdtBlocks) {
var sdtProperties = sdtBlock?.ChildElements.OfType<SdtProperties>().FirstOrDefault();
var docPartObject = sdtProperties?.ChildElements.OfType<SdtContentDocPartObject>().FirstOrDefault();
var docPartGallery = docPartObject?.ChildElements.OfType<DocPartGallery>().FirstOrDefault();
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -678,17 +672,19 @@ public void Dispose() {
}
}

private static void InitialiseStyleDefinitions(WordprocessingDocument wordDocument)
{
var styleDefinitionsPart = wordDocument.MainDocumentPart.GetPartsOfType<StyleDefinitionsPart>().FirstOrDefault();
if (styleDefinitionsPart != null)
{
AddStyleDefinitions(styleDefinitionsPart);
}
else
{
var styleDefinitionsPart1 = wordDocument.MainDocumentPart.AddNewPart<StyleDefinitionsPart>("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<StyleDefinitionsPart>()
.FirstOrDefault();
if (styleDefinitionsPart != null) {
AddStyleDefinitions(styleDefinitionsPart);
} else {

var styleDefinitionsPart1 = wordDocument.MainDocumentPart.AddNewPart<StyleDefinitionsPart>("rId1");
GenerateStyleDefinitionsPart1Content(styleDefinitionsPart1);

}
}
}

Expand Down Expand Up @@ -719,4 +715,4 @@ public List<ValidationErrorInfo> DocumentValidationErrors {
}
}
}
}
}