Fast and easy to use cross-platform .NET library that creates or modifies Microsoft Word (DocX) and later also Excel (XLSX) files without installing any software. Library is based on Open XML SDK
MIT License
279
stars
49
forks
source link
Adds CleanupDocument method to merge same formatted runs #106
This PR adds CleanupDocument() method so one can apply the cleaning up of existing or new documents. Microsoft Word, when you add bold, change color or similar will create separate runs to keep text. This is expected and something that keeps documents nicely formatted. However Microsoft Word for unknown reasons also sometimes adds new Runs that have very same formatting. I guess it could be user changed their mind when it comes to formatting and what was bold now isn't bold anymore.
For example: This is very long lineoftext - that would most likely translate into 7 runs in Microsoft Word OpenXml. So even tho the text line of text is BOLD all the way, it would treat it as 3 separate Runs. While this is ok, most of the time, it becomes really complicated when trying to fine existence of "line of text" within document. Because you are not searching for a single Text, but text split over multiple WordParagraphs. This becomes even more problematic if you add bold, then remove bold and it still stays 7 Runs, while in the Word document it looks like a single line of text.
public static void CleanupDocuments_Sample01(bool openWord) {
Console.WriteLine("[*] Load external Word Document - Sample 1");
string fullPath = "C:\PathToFile";
using (WordDocument document = WordDocument.Load(fullPath, false)) {
Console.WriteLine(fullPath);
Console.WriteLine("Sections count: " + document.Sections.Count);
Console.WriteLine("Tables count: " + document.Tables.Count);
Console.WriteLine("Paragraphs count: " + document.Paragraphs.Count);
foreach (var paragraph in document.Paragraphs) {
if (paragraph.Text.StartsWith("You can do")) {
paragraph.Text = "Maybe you can't!";
}
}
var cleanupCount = document.CleanupDocument();
Console.WriteLine("Removed " + cleanupCount + " runs because of identical formatting.");
document.Save(@"C:\SaveAsDifferent123file.docx", openWord);
}
}
This PR adds CleanupDocument() method so one can apply the cleaning up of existing or new documents. Microsoft Word, when you add bold, change color or similar will create separate runs to keep text. This is expected and something that keeps documents nicely formatted. However Microsoft Word for unknown reasons also sometimes adds new Runs that have very same formatting. I guess it could be user changed their mind when it comes to formatting and what was bold now isn't bold anymore.
For example: This is very long line of text - that would most likely translate into 7 runs in Microsoft Word OpenXml. So even tho the text line of text is BOLD all the way, it would treat it as 3 separate Runs. While this is ok, most of the time, it becomes really complicated when trying to fine existence of "line of text" within document. Because you are not searching for a single Text, but text split over multiple WordParagraphs. This becomes even more problematic if you add bold, then remove bold and it still stays 7 Runs, while in the Word document it looks like a single line of text.