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

Add ability to add custom lists (simplified logic) #208

Draft
wants to merge 26 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7df26f6
Add Numbering property to WordList class
PrzemyslawKlys Feb 18, 2024
4b99f21
Add WordListNumbering class to OfficeIMO.Word
PrzemyslawKlys Feb 18, 2024
0527f16
Add WordListLevel class to OfficeIMO.Word
PrzemyslawKlys Feb 18, 2024
cb1a9e0
add an example
PrzemyslawKlys Feb 18, 2024
889265d
program list
PrzemyslawKlys Feb 18, 2024
a9bcaf9
Merge branch 'master' into AddCustomLists
PrzemyslawKlys Feb 20, 2024
9f74dc5
update list of examples
PrzemyslawKlys Feb 20, 2024
2cb2ce8
Initialize abstract numbers making them unique
PrzemyslawKlys Feb 20, 2024
cf48bf7
Change example
PrzemyslawKlys Feb 20, 2024
4a0ad5e
Add word
PrzemyslawKlys Feb 20, 2024
8cb257d
Add documentation
PrzemyslawKlys Feb 20, 2024
edfea98
Initialize abstract num for loading and new documents
PrzemyslawKlys Feb 20, 2024
001de13
Add helpers with conversion
PrzemyslawKlys Mar 8, 2024
40410b3
Improvements
PrzemyslawKlys Mar 8, 2024
ab2db06
Add docs
PrzemyslawKlys Mar 8, 2024
aa5ec92
Removed unneeded code
PrzemyslawKlys Mar 8, 2024
16f9462
add docs
PrzemyslawKlys Mar 8, 2024
9a6ca6e
Update examples
PrzemyslawKlys Mar 8, 2024
45bb633
Merge branch 'master' into AddCustomLists
PrzemyslawKlys Sep 23, 2024
31b34ad
Merge branch 'master' of https://github.com/EvotecIT/OfficeIMO into A…
PrzemyslawKlys Dec 11, 2024
0917c00
Merge branch 'AddCustomLists' of https://github.com/EvotecIT/OfficeIM…
PrzemyslawKlys Dec 11, 2024
5c1cde8
omg
PrzemyslawKlys Dec 11, 2024
8ef6422
Enhance WordList class for improved numbering support
PrzemyslawKlys Dec 11, 2024
7a55d60
Add Example_BasicLists10 method for custom Word lists
PrzemyslawKlys Dec 11, 2024
5a60ea3
Refactor WordList class to enhance documentation and clarify properti…
PrzemyslawKlys Dec 11, 2024
57c742d
Enhance WordList class with detailed XML documentation for private me…
PrzemyslawKlys Dec 11, 2024
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
4 changes: 4 additions & 0 deletions OfficeIMO.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ static void Main(string[] args) {
Lists.Example_BasicLists2Load(folderPath, false);
Lists.Example_BasicLists7(folderPath, false);
Lists.Example_BasicLists8(folderPath, false);

Lists.Example_CustomList1(folderPath, true);
Tables.Example_BasicTables1(folderPath, true);

Tables.Example_BasicTables1(folderPath, false);
Tables.Example_BasicTablesLoad1(folderPath, false);
Tables.Example_BasicTablesLoad2(templatesPath, folderPath, false);
Expand Down
153 changes: 153 additions & 0 deletions OfficeIMO.Examples/Word/Lists/Lists.CreateCustom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using System;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;

namespace OfficeIMO.Examples.Word {
internal static partial class Lists {
internal static void Example_CustomList1(string folderPath, bool openWord) {
string filePath = System.IO.Path.Combine(folderPath, "Document with Custom Lists 2.docx");
using (WordDocument document = WordDocument.Create(filePath)) {
var paragraph = document.AddParagraph("This is 1st list");
paragraph.ParagraphAlignment = JustificationValues.Center;

// create list with bulleted style
// we could have used CustomStyle, but we are using Bulleted to show removal of levels
WordList wordList1 = document.AddList(WordListStyle.Bulleted);
wordList1.AddItem("Text 1 - List1");
wordList1.AddItem("Text 2 - List1", 1);
wordList1.AddItem("Text 3 - List1", 2);
wordList1.AddItem("Text 4 - List1", 3);
wordList1.AddItem("Text 5 - List1", 4);
wordList1.AddItem("Text 6 - List1", 5);
wordList1.AddItem("Text 7 - List1", 6);
wordList1.AddItem("Text 8 - List1", 7);
wordList1.AddItem("Text 9 - List1", 8);

// let's display some properties of the list
Console.WriteLine(wordList1.Numbering.Levels[0]._level.LevelIndex.ToString());
Console.WriteLine(wordList1.Numbering.Levels[1]._level.LevelIndex.ToString());
Console.WriteLine(wordList1.Numbering.Levels[2]._level.LevelIndex.ToString());
Console.WriteLine(wordList1.Numbering.Levels[0].IndentationHanging);
Console.WriteLine(wordList1.Numbering.Levels[0].IndentationLeft);
Console.WriteLine(wordList1.Numbering.Levels[0].IndentationLeftCentimeters);
Console.WriteLine(wordList1.Numbering.Levels[1].IndentationLeftCentimeters);
Console.WriteLine(wordList1.Numbering.Levels[2].IndentationLeftCentimeters);
Console.WriteLine(wordList1.Numbering.Levels[3].IndentationLeftCentimeters);
Console.WriteLine(wordList1.Numbering.Levels[1].LevelJustification);
Console.WriteLine(wordList1.Numbering.Levels[1].StartNumberingValue);

Console.WriteLine("Current levels count: " + wordList1.Numbering.Levels.Count);

// remove single level
wordList1.Numbering.Levels[0].Remove();
// remove all levels
wordList1.Numbering.RemoveAllLevels();

Console.WriteLine("Current levels count: " + wordList1.Numbering.Levels.Count);

// add custom levels
var level = new WordListLevel(SimplifiedListNumbers.BulletOpenCircle);
wordList1.Numbering.AddLevel(level);

var level1 = new WordListLevel(SimplifiedListNumbers.BulletSolidRound);
wordList1.Numbering.AddLevel(level1);

var level2 = new WordListLevel(SimplifiedListNumbers.BulletSquare);
wordList1.Numbering.AddLevel(level2);

var level3 = new WordListLevel(SimplifiedListNumbers.BulletSquare2);
wordList1.Numbering.AddLevel(level3);

var level4 = new WordListLevel(SimplifiedListNumbers.BulletClubs);
wordList1.Numbering.AddLevel(level4);

var level5 = new WordListLevel(SimplifiedListNumbers.BulletDiamond);
wordList1.Numbering.AddLevel(level5);

var level6 = new WordListLevel(SimplifiedListNumbers.BulletCheckmark);
wordList1.Numbering.AddLevel(level6);

var level7 = new WordListLevel(SimplifiedListNumbers.BulletArrow);
wordList1.Numbering.AddLevel(level7);

Console.WriteLine("Current levels count: " + wordList1.Numbering.Levels.Count);

document.AddParagraph("This is 2nd list");

// define list
WordList wordList2 = document.AddList(WordListStyle.Custom);
// add levels
var level21 = new WordListLevel(SimplifiedListNumbers.Decimal);
wordList2.Numbering.AddLevel(level21);
var level22 = new WordListLevel(SimplifiedListNumbers.DecimalBracket);
wordList2.Numbering.AddLevel(level22);
var level23 = new WordListLevel(SimplifiedListNumbers.DecimalDot);
wordList2.Numbering.AddLevel(level23);
var level24 = new WordListLevel(SimplifiedListNumbers.LowerLetter);
wordList2.Numbering.AddLevel(level24);
var level25 = new WordListLevel(SimplifiedListNumbers.LowerLetterBracket);
wordList2.Numbering.AddLevel(level25);
var level26 = new WordListLevel(SimplifiedListNumbers.LowerLetterDot);
wordList2.Numbering.AddLevel(level26);
var level27 = new WordListLevel(SimplifiedListNumbers.UpperLetter);
wordList2.Numbering.AddLevel(level27);
var level28 = new WordListLevel(SimplifiedListNumbers.UpperLetterBracket);
wordList2.Numbering.AddLevel(level28);
var level29 = new WordListLevel(SimplifiedListNumbers.UpperLetterDot);
wordList2.Numbering.AddLevel(level29);

// add items to the list
wordList2.AddItem("Text 1 - Decimal");
wordList2.AddItem("Text 2 - DecimalBracket", 1);
wordList2.AddItem("Text 3 - DecimalDot", 2);
wordList2.AddItem("Text 4 - LowerLetter", 3);
wordList2.AddItem("Text 4 - LowerLetter", 3);
wordList2.AddItem("Text 4 - LowerLetter", 3);
wordList2.AddItem("Text 5.1 - LowerLetterBracket", 4);
wordList2.AddItem("Text 5.2 - LowerLetterBracket", 4);
wordList2.AddItem("Text 5.3 - LowerLetterBracket", 4);
wordList2.AddItem("Text 6 - LowerLetterDot", 5);
wordList2.AddItem("Text 7 - UpperLetter", 6);
wordList2.AddItem("Text 8 - UpperLetterBracket", 7);
wordList2.AddItem("Text 9 - UpperLetterDot", 8);

document.AddParagraph("This is 3rd list");

WordList wordList3 = document.AddList(WordListStyle.Custom);
var level31 = new WordListLevel(SimplifiedListNumbers.UpperRoman);
wordList3.Numbering.AddLevel(level31);
var level32 = new WordListLevel(SimplifiedListNumbers.UpperRomanBracket);
wordList3.Numbering.AddLevel(level32);
var level33 = new WordListLevel(SimplifiedListNumbers.UpperRomanDot);
wordList3.Numbering.AddLevel(level33);
var level34 = new WordListLevel(SimplifiedListNumbers.LowerRoman);
level34.StartNumberingValue = 4;
level34.LevelJustification = LevelJustificationValues.Right;
wordList3.Numbering.AddLevel(level34);
var level35 = new WordListLevel(SimplifiedListNumbers.LowerRomanBracket);
wordList3.Numbering.AddLevel(level35);
var level36 = new WordListLevel(SimplifiedListNumbers.LowerRomanDot);
wordList3.Numbering.AddLevel(level36);
var level37 = new WordListLevel(SimplifiedListNumbers.DecimalBracket);
wordList3.Numbering.AddLevel(level37);
var level38 = new WordListLevel(SimplifiedListNumbers.DecimalDot);
wordList3.Numbering.AddLevel(level38);
var level39 = new WordListLevel(SimplifiedListNumbers.Decimal);
wordList3.Numbering.AddLevel(level39);


wordList3.AddItem("Text 1 - UpperRoman");
wordList3.AddItem("Text 2 - UpperRomanBracket", 1);
wordList3.AddItem("Text 3 - UpperRomanDot", 2);
wordList3.AddItem("Text 4 - LowerRoman", 3);
wordList3.AddItem("Text 5 - LowerRomanBracket", 4);
wordList3.AddItem("Text 6 - LowerRomanDot", 5);
wordList3.AddItem("Text 7 - DecimalBracket", 6);
wordList3.AddItem("Text 8 - DecimalDot", 7);
wordList3.AddItem("Text 9 - Decimal", 8);

document.Save(openWord);
}
}
}
}
41 changes: 41 additions & 0 deletions OfficeIMO.Examples/Word/Lists/Lists.CreateWithChanges01.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;
using Color = SixLabors.ImageSharp.Color;

namespace OfficeIMO.Examples.Word {
internal static partial class Lists {
internal static void Example_BasicLists10(string folderPath, bool openWord) {
string filePath = System.IO.Path.Combine(folderPath, "Document with Lists with custom lists numbering.docx");
using (WordDocument document = WordDocument.Create(filePath)) {
var paragraph = document.AddParagraph("This is 1st list - LowerLetterWithBracket");
paragraph.ParagraphAlignment = JustificationValues.Center;

WordList wordList1 = document.AddList(WordListStyle.LowerLetterWithBracket);
wordList1.Bold = true;
wordList1.FontSize = 16;
wordList1.Color = Color.DarkRed;

var listItem1 = wordList1.AddItem("Text 1");
listItem1.Bold = true;
listItem1.FontSize = 16;
listItem1.Color = Color.DarkRed;

wordList1.AddItem("Text 2", 1);
wordList1.AddItem("Text 3", 2);

paragraph = document.AddParagraph("This is 2nd list - LowerLetterWithDot");
paragraph.ParagraphAlignment = JustificationValues.Center;
paragraph.Bold = true;
paragraph.FontSize = 16;
paragraph.Color = Color.AliceBlue;

document.Save(openWord);
}
}
}
}
7 changes: 7 additions & 0 deletions OfficeIMO.Examples/Word/Tables/Tables.Create1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ internal static void Example_BasicTables1(string folderPath, bool openWord) {

wordTable.Rows[2].Cells[0].Paragraphs[0].AddParagraphAfterSelf().AddParagraphAfterSelf().AddParagraphAfterSelf().Text = "Works differently";

int paragraphCount = wordTable.Rows[1].Cells[0].Paragraphs.Count;
Console.WriteLine(wordTable.Rows[1].Cells[0].Paragraphs.Count); // should be 5
for (int i = 1; i < paragraphCount; i++) {
Console.WriteLine("Removing paragraph");
wordTable.Rows[1].Cells[0].Paragraphs[0].Remove();
}
Console.WriteLine(wordTable.Rows[1].Cells[0].Paragraphs.Count); // should be 1


Console.WriteLine(wordTable.Style);
Expand Down
39 changes: 39 additions & 0 deletions OfficeIMO.Word/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,45 @@ internal static UInt32 ConvertCentimetersToTwipsUInt32(double cmValue) {
UInt32 twips = (UInt32)(cmValue * 567.0);
return twips;
}

/// <summary>
/// Converts centimeters to twentieths of a point
/// </summary>
/// <param name="cm"></param>
/// <returns></returns>
internal static double ConvertCentimetersToTwentiethsOfPoint(double cm) {
double inches = cm / 2.54;
double points = inches * 72;
double twentiethsOfPoint = points * 20;
return twentiethsOfPoint;
}

internal static double ConvertTwentiethsOfPointToCentimeters(double twentiethsOfPoint) {
double points = twentiethsOfPoint / 20;
double centimeters = (points / 72) * 2.54;
return centimeters;
}

/// <summary>
/// Converts centimeters to points
/// </summary>
/// <param name="cm"></param>
/// <returns></returns>
internal static double ConvertCentimetersToPoints(double cm) {
double inches = cm / 2.54;
double points = inches * 72;
return points;
}

/// <summary>
/// Converts the points to centimeters.
/// </summary>
/// <param name="points">The points.</param>
/// <returns></returns>
internal static double ConvertPointsToCentimeters(double points) {
double centimeters = (points / 72) * 2.54;
return centimeters;
}
}

internal record ImageCharacteristics(double Width, double Height, CustomImagePartType Type);
Expand Down
18 changes: 18 additions & 0 deletions OfficeIMO.Word/WordDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ public List<WordComment> Comments {
get { return WordComment.GetAllComments(this); }
}

/// <summary>
/// Gets the lists in the document
/// </summary>
/// <value>
/// The lists.
/// </value>
public List<WordList> Lists => WordSection.GetAllDocumentsLists(this);

/// <summary>
Expand Down Expand Up @@ -579,6 +585,12 @@ public static WordDocument Create(string filePath = "", bool autoSave = false) {
WordSection wordSection = new WordSection(word, null);
WordBackground wordBackground = new WordBackground(word);

// initialize abstract number id for lists to make sure those are unique
WordListStyles.InitializeAbstractNumberId(word._wordprocessingDocument);

// initialize abstract number id for lists to make sure those are unique
WordListStyles.InitializeAbstractNumberId(word._wordprocessingDocument);

//word.Save();
return word;
}
Expand Down Expand Up @@ -691,6 +703,9 @@ public static WordDocument Load(string filePath, bool readOnly = false, bool aut
word._wordprocessingDocument = wordDocument;
word._document = wordDocument.MainDocumentPart.Document;
word.LoadDocument();

// initialize abstract number id for lists to make sure those are unique
WordListStyles.InitializeAbstractNumberId(word._wordprocessingDocument);
return word;
}

Expand All @@ -714,6 +729,9 @@ public static WordDocument Load(Stream stream, bool readOnly = false, bool autoS
document._wordprocessingDocument = wordDocument;
document._document = wordDocument.MainDocumentPart.Document;
document.LoadDocument();

// initialize abstract number id for lists to make sure those are unique
WordListStyles.InitializeAbstractNumberId(document._wordprocessingDocument);
return document;
}

Expand Down
Loading
Loading