Skip to content

Commit

Permalink
Merge branch 'master' of /~https://github.com/EvotecIT/OfficeIMO
Browse files Browse the repository at this point in the history
  • Loading branch information
PrzemyslawKlys committed Oct 11, 2022
2 parents 2e51c11 + e7092ec commit 76ba7e3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 49 deletions.
38 changes: 27 additions & 11 deletions OfficeIMO.Tests/Word.Save.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using OfficeIMO.Word;
using Xunit;

namespace OfficeIMO.Tests {
public partial class Word {

[Fact]
public void Test_Save() {
string filePath1 = System.IO.Path.Combine(_directoryWithFiles, "FirstDocument11.docx");
string filePath2 = System.IO.Path.Combine(_directoryWithFiles, "FirstDocument12.docx");
string filePath3 = System.IO.Path.Combine(_directoryWithFiles, "FirstDocument13.docx");
string filePath4 = System.IO.Path.Combine(_directoryWithFiles, "FirstDocument14.docx");
string filePath1 = Path.Combine(_directoryWithFiles, "FirstDocument11.docx");
string filePath2 = Path.Combine(_directoryWithFiles, "FirstDocument12.docx");
string filePath3 = Path.Combine(_directoryWithFiles, "FirstDocument13.docx");
string filePath4 = Path.Combine(_directoryWithFiles, "FirstDocument14.docx");

File.Delete(filePath1);
File.Delete(filePath2);
Expand Down Expand Up @@ -100,7 +96,7 @@ public void Test_Save() {

[Fact]
public void Test_Dispose() {
string filePath1 = System.IO.Path.Combine(_directoryWithFiles, "DisposeTesting.docx");
string filePath1 = Path.Combine(_directoryWithFiles, "DisposeTesting.docx");
File.Delete(filePath1);

Assert.True(File.Exists(filePath1) == false);
Expand All @@ -124,5 +120,25 @@ public void Test_Dispose() {

Assert.True(File.Exists(filePath1) == true);
}

[Fact]
public void Test_SaveToStream()
{
var document = WordDocument.Create();
document.AddParagraph("Hello world!");

using (var outputStream = new MemoryStream())
{
document.Save(outputStream);

var resultDoc = WordDocument.Load(outputStream);

var expectedCount = 1;
var expectedText = "Hello world!";

Assert.Equal(resultDoc.Paragraphs.Count, expectedCount);
Assert.Equal(resultDoc.Paragraphs[0].Text, expectedText);
}
}
}
}
103 changes: 65 additions & 38 deletions OfficeIMO.Word/WordDocument.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;

namespace OfficeIMO.Word {
public partial class WordDocument : IDisposable {
Expand All @@ -30,19 +27,16 @@ internal int BookmarkId {

public WordTableOfContent TableOfContent {
get {
var sdtBlocks = _document.Body.ChildElements.OfType<SdtBlock>();
foreach (var sdtBlock in sdtBlocks) {
if (sdtBlock != null) {
var sdtProperties = sdtBlock.ChildElements.OfType<SdtProperties>().FirstOrDefault();
if (sdtProperties != null) {
var docPartObject = sdtProperties.ChildElements.OfType<SdtContentDocPartObject>().FirstOrDefault();
if (docPartObject != null) {
var docPartGallery = docPartObject.ChildElements.OfType<DocPartGallery>().FirstOrDefault();
if (docPartGallery != null && docPartGallery.Val == "Table of Contents") {
return new WordTableOfContent(this, sdtBlock);
}
}
}
var sdtBlocks = _document.Body?.ChildElements.OfType<SdtBlock>() ?? Enumerable.Empty<SdtBlock>();

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();

if (docPartGallery != null && docPartGallery.Val == "Table of Contents") {
return new WordTableOfContent(this, sdtBlock);
}
}

Expand All @@ -52,19 +46,16 @@ public WordTableOfContent TableOfContent {

public WordCoverPage CoverPage {
get {
var sdtBlocks = _document.Body.ChildElements.OfType<SdtBlock>();
foreach (var sdtBlock in sdtBlocks) {
if (sdtBlock != null) {
var sdtProperties = sdtBlock.ChildElements.OfType<SdtProperties>().FirstOrDefault();
if (sdtProperties != null) {
var docPartObject = sdtProperties.ChildElements.OfType<SdtContentDocPartObject>().FirstOrDefault();
if (docPartObject != null) {
var docPartGallery = docPartObject.ChildElements.OfType<DocPartGallery>().FirstOrDefault();
if (docPartGallery != null && docPartGallery.Val == "Cover Pages") {
return new WordCoverPage(this, sdtBlock);
}
}
}
var sdtBlocks = _document.Body?.ChildElements.OfType<SdtBlock>() ?? Enumerable.Empty<SdtBlock>();

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();

if (docPartGallery != null && docPartGallery.Val == "Cover Pages") {
return new WordCoverPage(this, sdtBlock);
}
}

Expand Down Expand Up @@ -483,7 +474,7 @@ public static WordDocument Load(string filePath, bool readOnly = false, bool aut
}
}

WordDocument word = new WordDocument();
var word = new WordDocument();

var openSettings = new OpenSettings {
AutoSave = autoSave
Expand All @@ -494,15 +485,9 @@ public static WordDocument Load(string filePath, bool readOnly = false, bool aut
word._fileStream.CopyTo(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);

WordprocessingDocument wordDocument = WordprocessingDocument.Open(memoryStream, !readOnly, openSettings);
var wordDocument = WordprocessingDocument.Open(memoryStream, !readOnly, openSettings);

StyleDefinitionsPart styleDefinitionsPart = wordDocument.MainDocumentPart.GetPartsOfType<StyleDefinitionsPart>().FirstOrDefault();
if (styleDefinitionsPart != null) {
AddStyleDefinitions(styleDefinitionsPart);
} else {
StyleDefinitionsPart styleDefinitionsPart1 = wordDocument.MainDocumentPart.AddNewPart<StyleDefinitionsPart>("rId1");
GenerateStyleDefinitionsPart1Content(styleDefinitionsPart1);
}
InitialiseStyleDefinitions(wordDocument);

word.FilePath = filePath;
word._wordprocessingDocument = wordDocument;
Expand All @@ -511,6 +496,24 @@ 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)
{
var document = new WordDocument();

var openSettings = new OpenSettings
{
AutoSave = autoSave
};

var wordDocument = WordprocessingDocument.Open(stream, !readOnly, openSettings);
InitialiseStyleDefinitions(wordDocument);

document._wordprocessingDocument = wordDocument;
document._document = wordDocument.MainDocumentPart.Document;
document.LoadDocument();
return document;
}

public void Open(bool openWord = true) {
this.Open("", openWord);
}
Expand Down Expand Up @@ -635,6 +638,16 @@ public void Save(bool openWord) {
this.Save("", openWord);
}

public void Save(Stream outputStream)
{
this._wordprocessingDocument.Clone(outputStream);

if (outputStream.CanSeek)
{
outputStream.Seek(0, SeekOrigin.Begin);
}
}

/// <summary>
/// This moves section within body from top to bottom to allow footers/headers to move
/// Needs more work, but this is what Word does all the time
Expand Down Expand Up @@ -665,6 +678,20 @@ 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);
}
}

internal WordSection _currentSection => this.Sections.Last();


Expand Down

0 comments on commit 76ba7e3

Please sign in to comment.