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

Added basic Elements/ElementsByType property and few other improvements #240

Merged
merged 20 commits into from
Sep 21, 2024
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 OfficeIMO.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static void Main(string[] args) {
BasicDocument.Example_BasicWordWithMarginsInCentimeters(folderPath, false);
BasicDocument.Example_BasicWordWithMarginsAndImage(folderPath, false);
BasicDocument.Example_BasicWordWithLineSpacing(folderPath, false);
BasicDocument.Example_BasicWordWithSomeParagraphs(folderPath, false);

AdvancedDocument.Example_AdvancedWord(folderPath, false);
AdvancedDocument.Example_AdvancedWord2(folderPath, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;
using Color = SixLabors.ImageSharp.Color;
Expand Down Expand Up @@ -30,6 +30,9 @@ public static void Example_AdvancedWord(string folderPath, bool openWord) {
// lets create a list that will be binded to TOC
var wordListToc = document.AddTableOfContentList(WordListStyle.Headings111);

var elements = document.Elements;
Console.WriteLine("Elements count: " + elements.Count);

wordListToc.AddItem("How to add a table to document?");

document.AddParagraph("In the first paragraph I would like to show you how to add a table to the document using one of the 105 built-in styles:");
Expand All @@ -47,10 +50,16 @@ public static void Example_AdvancedWord(string folderPath, bool openWord) {
var paragraph = document.AddParagraph("Adding lists is similar to ading a table. Just define a list and add list items to it. ").SetText("Remember that you can add anything between list items! ");
paragraph.SetColor(Color.Blue).SetText("For example TOC List is just another list, but defining a specific style.");

elements = document.Elements;
Console.WriteLine("Elements count before list: " + elements.Count);

var list = document.AddList(WordListStyle.Bulleted);
list.AddItem("First element of list", 0);
list.AddItem("Second element of list", 1);

elements = document.Elements;
Console.WriteLine("Elements count after list: " + elements.Count);

var paragraphWithHyperlink = document.AddHyperLink("Go to Evotec Blogs", new Uri("https://evotec.xyz"), true, "URL with tooltip");
// you can also change the hyperlink text, uri later on using properties
paragraphWithHyperlink.Hyperlink.Uri = new Uri("https://evotec.xyz/hub");
Expand Down Expand Up @@ -95,6 +104,12 @@ public static void Example_AdvancedWord(string folderPath, bool openWord) {
// add watermark
document.Sections[0].AddWatermark(WordWatermarkStyle.Text, "Draft");

elements = document.Elements;
Console.WriteLine("Elements count in the end: " + elements.Count);

var elementsByType = document.ElementsByType;
Console.WriteLine("ElementsByType count in the end: " + elementsByType.Count);

document.Save(openWord);
}
}
Expand Down
27 changes: 27 additions & 0 deletions OfficeIMO.Examples/Word/BasicDocument/BasicDocument.Create07.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;

namespace OfficeIMO.Examples.Word {
internal static partial class BasicDocument {
public static void Example_BasicWordWithSomeParagraphs(string folderPath, bool openWord) {
Console.WriteLine("[*] Creating standard document with margins");
string filePath = System.IO.Path.Combine(folderPath, "EmptyDocumentWithSomeParagraphs.docx");
using (WordDocument document = WordDocument.Create(filePath)) {
document.Settings.FontFamily = "Arial";
document.Settings.FontSize = 9;
document.AddParagraph("This should be Arial 9");

var par = document.AddParagraph("This should be Tahoma 20");
par.FontFamily = "Tahoma";
par.AddText("SuperScript").SetVerticalTextAlignment(VerticalPositionValues.Superscript);
par.AddText("Continue 1 ");
par.AddText("Baseline").SetVerticalTextAlignment(VerticalPositionValues.Baseline);
par.AddText("Continue 2 ");
par.AddText("SubScript").SetVerticalTextAlignment(VerticalPositionValues.Subscript);

document.Save(openWord);
}
}
}
}
Binary file added OfficeIMO.Tests/Documents/NestedTables.docx
Binary file not shown.
Binary file not shown.
15 changes: 14 additions & 1 deletion OfficeIMO.Tests/Word.BasicDocument.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.IO;
using OfficeIMO.Word;
using Xunit;

Expand Down Expand Up @@ -36,5 +35,19 @@ public void Test_OpeningWordAndParagraphCountMatches() {
//Assert.True(t0.Paragraphs.Count() == 12);
}
}

[Fact]
public void Test_AllElements() {
var docs = Directory.GetFiles(_directoryDocuments, "*.docx");
foreach (var doc in docs) {
using (WordDocument document = WordDocument.Load(doc)) {
var allElements = document.Elements;
Assert.True(allElements.Count > 0);
var allElementsByType = document.ElementsByType;
Assert.True(allElementsByType.Count > 0);
}

}
}
}
}
33 changes: 33 additions & 0 deletions OfficeIMO.Tests/Word.Paragraphs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -439,5 +439,38 @@ public void Test_OpeningDocumentWithCustomStyle() {
Assert.Equal(WordParagraphStyles.Custom, document.Paragraphs[1].Style);
}
}

[Fact]
public void Test_SubscriptAndSuperscript() {
string filePath = Path.Combine(_directoryDocuments, "Normal Subscript Superscript.docx");
using (WordDocument document = WordDocument.Load(filePath)) {
Assert.Equal(3, document.Paragraphs.Count);
Assert.Equal(null, document.Paragraphs[0].VerticalTextAlignment);
Assert.Equal(VerticalPositionValues.Subscript, document.Paragraphs[1].VerticalTextAlignment);
Assert.Equal(VerticalPositionValues.Superscript, document.Paragraphs[2].VerticalTextAlignment);

Assert.True(document.Paragraphs.Count == 3);

var wordParagraph1 = document.AddParagraph("Subscript").SetVerticalTextAlignment(VerticalPositionValues.Subscript);
var wordParagraph2 = document.AddParagraph("Baseline").SetVerticalTextAlignment(VerticalPositionValues.Baseline);
var wordParagraph3 = document.AddParagraph("Superscript").SetVerticalTextAlignment(VerticalPositionValues.Superscript);
var wordParagraph4 = document.AddParagraph("Normal");

Assert.True(document.Paragraphs.Count == 7);
Assert.Equal(document.Paragraphs[3].VerticalTextAlignment, VerticalPositionValues.Subscript);
Assert.Equal(document.Paragraphs[4].VerticalTextAlignment, VerticalPositionValues.Baseline);
Assert.Equal(document.Paragraphs[5].VerticalTextAlignment, VerticalPositionValues.Superscript);
Assert.Equal(document.Paragraphs[6].VerticalTextAlignment, null);

document.Paragraphs[3].VerticalTextAlignment = null;
Assert.Equal(document.Paragraphs[3].VerticalTextAlignment, null);
document.Paragraphs[4].SetSubScript();
Assert.Equal(document.Paragraphs[4].VerticalTextAlignment, VerticalPositionValues.Subscript);
document.Paragraphs[5].SetSubScript();
Assert.Equal(document.Paragraphs[5].VerticalTextAlignment, VerticalPositionValues.Subscript);
document.Paragraphs[6].SetSuperScript();
Assert.Equal(document.Paragraphs[6].VerticalTextAlignment, VerticalPositionValues.Superscript);
}
}
}
}
23 changes: 21 additions & 2 deletions OfficeIMO.Tests/Word.TablesNested.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.Wordprocessing;
using OfficeIMO.Word;
Expand Down Expand Up @@ -106,5 +106,24 @@ public void Test_CreatingWordDocumentWithNestedTables() {
document.Save();
}
}

[Fact]
public void Test_ReadingWordDocumentWithNestedTables() {
string filePath = Path.Combine(_directoryDocuments, "NestedTables.docx");
using (WordDocument document = WordDocument.Load(filePath)) {
Assert.Single(document.Tables);

var table = document.Tables[0];
Assert.True(table.HasNestedTables);
Assert.Equal(2, table.NestedTables.Count);
Assert.Equal(9, table.Cells.Count);
Assert.False(table.Cells[1].HasNestedTables);
Assert.True(table.Cells[8].HasNestedTables);

var cell = table.Cells[0];
Assert.True(cell.HasNestedTables);
Assert.Single(cell.NestedTables);
}
}
}
}
}
2 changes: 1 addition & 1 deletion OfficeIMO.Word/WordBookmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;

namespace OfficeIMO.Word {
public class WordBookmark {
public class WordBookmark : WordElement {
private WordDocument _document;
private Paragraph _paragraph;
private BookmarkStart _bookmarkStart;
Expand Down
4 changes: 2 additions & 2 deletions OfficeIMO.Word/WordBreak.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,7 +9,7 @@ namespace OfficeIMO.Word {
/// Represents a break in the text.
/// Be it page break, soft break, column or text wrapping
/// </summary>
public class WordBreak {
public class WordBreak : WordElement {
private WordDocument _document;
private readonly Paragraph _paragraph;
private readonly Run _run;
Expand Down
2 changes: 1 addition & 1 deletion OfficeIMO.Word/WordChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using PlotArea = DocumentFormat.OpenXml.Drawing.Charts.PlotArea;

namespace OfficeIMO.Word {
public partial class WordChart {
public partial class WordChart : WordElement {
public WordChart(WordDocument document, WordParagraph paragraph, Drawing drawing) {
_document = document;
_drawing = drawing;
Expand Down
4 changes: 2 additions & 2 deletions OfficeIMO.Word/WordComment.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace OfficeIMO.Word {
public partial class WordComment {
public partial class WordComment : WordElement {
private WordParagraph _paragraph;
private readonly WordDocument _document;
private readonly Comment _comment;
Expand Down
5 changes: 1 addition & 4 deletions OfficeIMO.Word/WordCoverPage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using DocumentFormat.OpenXml.Wordprocessing;

namespace OfficeIMO.Word {
Expand All @@ -19,7 +18,7 @@ public enum CoverPageTemplate {
Retrospect
}

public partial class WordCoverPage {
public partial class WordCoverPage : WordElement {
private readonly WordDocument _document;
private readonly SdtBlock _sdtBlock;

Expand All @@ -32,8 +31,6 @@ public WordCoverPage(WordDocument wordDocument, CoverPageTemplate coverPageTempl
_document = wordDocument;
_sdtBlock = GetStyle(coverPageTemplate);
this._document._wordprocessingDocument.MainDocumentPart.Document.Body.Append(_sdtBlock);

//this._document._wordprocessingDocument.MainDocumentPart.Document.Body.InsertAt(_sdtBlock, 0);
}

private SdtBlock GetStyle(CoverPageTemplate template) {
Expand Down
67 changes: 32 additions & 35 deletions OfficeIMO.Word/WordDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,14 @@ internal int BookmarkId {
public WordTableOfContent TableOfContent {
get {
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);
}
}

return null;
return WordSection.ConvertStdBlockToTableOfContent(this, sdtBlocks);
}
}

public WordCoverPage CoverPage {
get {
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);
}
}

return null;
return WordSection.ConvertStdBlockToCoverPage(this, sdtBlocks);
}
}

Expand Down Expand Up @@ -213,6 +191,35 @@ public List<WordParagraph> ParagraphsFootNotes {
}
}

/// <summary>
/// List of all elements in the document from all the sections
/// </summary>
public List<WordElement> Elements {
get {
List<WordElement> list = new List<WordElement>();
foreach (var section in this.Sections) {
list.AddRange(section.Elements);
}
return list;
}
}

/// <summary>
/// List of all elements in the document from all the sections by their subtype
/// </summary>
public List<WordElement> ElementsByType {
get {
List<WordElement> list = new List<WordElement>();
foreach (var section in this.Sections) {
list.AddRange(section.ElementsByType);
}
return list;
}
}

/// <summary>
/// List of all PageBreaks in the document from all the sections
/// </summary>
public List<WordBreak> PageBreaks {
get {
List<WordBreak> list = new List<WordBreak>();
Expand Down Expand Up @@ -259,17 +266,7 @@ public List<WordComment> Comments {
get { return WordComment.GetAllComments(this); }
}

public List<WordList> Lists {
get {
return WordSection.GetAllDocumentsLists(this);
//List<WordList> list = new List<WordList>();
//foreach (var section in this.Sections) {
// list.AddRange(section.Lists);
//}

//return list;
}
}
public List<WordList> Lists => WordSection.GetAllDocumentsLists(this);

/// <summary>
/// Provides a list of Bookmarks in the document from all the sections
Expand Down
11 changes: 11 additions & 0 deletions OfficeIMO.Word/WordElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace OfficeIMO.Word;

/// <summary>
/// Word element used as base for all Word elements
/// </summary>
public class WordElement {
/// <summary>
/// Constructor
/// </summary>
public WordElement() { }
}
2 changes: 1 addition & 1 deletion OfficeIMO.Word/WordEmbeddedDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using DocumentFormat.OpenXml.Wordprocessing;

namespace OfficeIMO.Word {
public class WordEmbeddedDocument {
public class WordEmbeddedDocument : WordElement {
private string _id;
private AltChunk _altChunk;
private readonly AlternativeFormatImportPart _altContent;
Expand Down
2 changes: 1 addition & 1 deletion OfficeIMO.Word/WordEndNote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace OfficeIMO.Word {

public partial class WordEndNote {
public partial class WordEndNote : WordElement {
private readonly WordDocument _document;
private readonly Paragraph _paragraph;
private readonly Run _run;
Expand Down
4 changes: 2 additions & 2 deletions OfficeIMO.Word/WordEquation.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Wordprocessing;

namespace OfficeIMO.Word {

public class WordEquation {
public class WordEquation : WordElement {
private WordDocument _document;
private Paragraph _paragraph;
private DocumentFormat.OpenXml.Math.Paragraph mathParagraph;
Expand Down
2 changes: 1 addition & 1 deletion OfficeIMO.Word/WordField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public enum WordFieldFormat {
Arabic,
}

public partial class WordField {
public partial class WordField : WordElement {
private readonly WordDocument _document;
private readonly Paragraph _paragraph;
private readonly List<Run> _runs = new List<Run>();
Expand Down
Loading
Loading