diff --git a/Build/ApproveVerifyTests.ps1 b/Build/ApproveVerifyTests.ps1 index 37956b19..9eb45ce7 100644 --- a/Build/ApproveVerifyTests.ps1 +++ b/Build/ApproveVerifyTests.ps1 @@ -3,7 +3,7 @@ param ( ) $ErrorActionPreference = 'Stop' -$DotnetVersions = @('6.0', '7.0', '8.0') +$DotnetVersions = @('6_0', '7_0', '8_0') $SolutionPath = [io.path]::Combine($SolutionRoot, 'OfficeImo.sln') if ($SolutionRoot -and (Test-Path -Path $SolutionPath)) { Write-Host "Solution found at $($SolutionPath). Processing files..." -ForegroundColor Green diff --git a/OfficeIMO.Examples/Word/Embed/EmbedFileHTML.cs b/OfficeIMO.Examples/Word/Embed/EmbedFileHTML.cs index 7a282498..3624df99 100644 --- a/OfficeIMO.Examples/Word/Embed/EmbedFileHTML.cs +++ b/OfficeIMO.Examples/Word/Embed/EmbedFileHTML.cs @@ -13,7 +13,7 @@ internal static partial class Embed { public static void Example_EmbedFileHTML(string folderPath, string templateFolder, bool openWord) { Console.WriteLine("[*] Creating standard document with embedded HTML file"); - string filePath = System.IO.Path.Combine(folderPath, "EmbeddedFileHTML.docx"); + string filePath = System.IO.Path.Combine(folderPath, "EmbeddedFileHTML3.docx"); string htmlFilePath = System.IO.Path.Combine(templateFolder, "SampleFileHTML.html"); using (WordDocument document = WordDocument.Create(filePath)) { Console.WriteLine("Embedded documents in word: " + document.EmbeddedDocuments.Count); @@ -21,14 +21,34 @@ public static void Example_EmbedFileHTML(string folderPath, string templateFolde document.AddParagraph("Add HTML document in DOCX"); + Console.WriteLine("Embedded documents in word: " + document.EmbeddedDocuments.Count); + + document.AddEmbeddedDocument(htmlFilePath); + + document.AddEmbeddedDocument(htmlFilePath); + + Console.WriteLine("Embedded documents in word: " + document.EmbeddedDocuments.Count); + document.AddSection(); + document.AddEmbeddedDocument(htmlFilePath); + + document.AddEmbeddedDocument(htmlFilePath); + + Console.WriteLine("Embedded documents in word: " + document.EmbeddedDocuments.Count); + Console.WriteLine("Embedded documents in Section 0: " + document.Sections[0].EmbeddedDocuments.Count); Console.WriteLine("Embedded documents in Section 1: " + document.Sections[1].EmbeddedDocuments.Count); document.AddEmbeddedDocument(htmlFilePath); + Console.WriteLine("Embedded documents in word: " + document.EmbeddedDocuments.Count); + Console.WriteLine("Embedded documents in Section 0: " + document.Sections[0].EmbeddedDocuments.Count); + Console.WriteLine("Embedded documents in Section 1: " + document.Sections[1].EmbeddedDocuments.Count); + document.EmbeddedDocuments[0].Save("C:\\TEMP\\EmbeddedFileHTML.html"); + document.AddEmbeddedDocument(htmlFilePath); + Console.WriteLine("Embedded documents in word: " + document.EmbeddedDocuments.Count); Console.WriteLine("Embedded documents in Section 0: " + document.Sections[0].EmbeddedDocuments.Count); Console.WriteLine("Embedded documents in Section 1: " + document.Sections[1].EmbeddedDocuments.Count); diff --git a/OfficeIMO.Examples/Word/Embed/EmbedFileMultiple.cs b/OfficeIMO.Examples/Word/Embed/EmbedFileMultiple.cs index 065578b8..005fa875 100644 --- a/OfficeIMO.Examples/Word/Embed/EmbedFileMultiple.cs +++ b/OfficeIMO.Examples/Word/Embed/EmbedFileMultiple.cs @@ -43,8 +43,7 @@ public static void Example_EmbedFileMultiple(string folderPath, string templateF document.AddParagraph("Add TEXT 2"); // but you can specify type as you want, if the extension would be .ext, and the content type would be text/plain - document.AddEmbeddedDocument(txtFilePath, AlternativeFormatImportPartType.TextPlain); - + document.AddEmbeddedDocument(txtFilePath, WordAlternativeFormatImportPartType.TextPlain); Console.WriteLine("Embedded documents in word: " + document.EmbeddedDocuments.Count); Console.WriteLine("Embedded documents in Section 0: " + document.Sections[0].EmbeddedDocuments.Count); diff --git a/OfficeIMO.Examples/Word/Embed/EmbedFragmentHTML.cs b/OfficeIMO.Examples/Word/Embed/EmbedFragmentHTML.cs index 02106fe7..0dba32df 100644 --- a/OfficeIMO.Examples/Word/Embed/EmbedFragmentHTML.cs +++ b/OfficeIMO.Examples/Word/Embed/EmbedFragmentHTML.cs @@ -37,7 +37,7 @@ public static void Example_EmbedHTMLFragment(string folderPath, bool openWord) { """; - document.AddEmbeddedFragment(htmlContent, AlternativeFormatImportPartType.Html); + document.AddEmbeddedFragment(htmlContent, WordAlternativeFormatImportPartType.Html); document.EmbeddedDocuments[0].Save("C:\\TEMP\\EmbeddedFragment.html"); diff --git a/OfficeIMO.Examples/Word/Images/ImageFixedPosition.cs b/OfficeIMO.Examples/Word/Images/ImageFixedPosition.cs index ccdfa252..8bc733c3 100644 --- a/OfficeIMO.Examples/Word/Images/ImageFixedPosition.cs +++ b/OfficeIMO.Examples/Word/Images/ImageFixedPosition.cs @@ -28,9 +28,9 @@ internal static void Example_AddingFixedImages(string folderPath, bool openWord) Console.WriteLine("PRE position edit."); // Before editing, we can assess the RelativeFrom and PositionOffset properties of the image. - DocumentFormat.OpenXml.EnumValue hRelativeFrom; - string hOffset, vOffset; - DocumentFormat.OpenXml.EnumValue vRelativeFrom; + //DocumentFormat.OpenXml.EnumValue hRelativeFrom; + //string hOffset, vOffset; + //DocumentFormat.OpenXml.EnumValue vRelativeFrom; checkImageProps(paragraph1); // Begin editing the fixed position properties of the image. You may edit both, however it @@ -68,7 +68,7 @@ internal static void Example_AddingFixedImages(string folderPath, bool openWord) document.Save(openWord); - static void checkImageProps(WordParagraph paragraph1) { + static void checkImageProps(WordParagraph paragraph1) { var hRelativeFrom = paragraph1.Image.horizontalPosition.RelativeFrom; var hOffset = paragraph1.Image.horizontalPosition.PositionOffset.Text; var vRelativeFrom = paragraph1.Image.verticalPosition.RelativeFrom; diff --git a/OfficeIMO.Examples/Word/Sections/Sections.Example1.cs b/OfficeIMO.Examples/Word/Sections/Sections.Example1.cs index d9ac96fa..85ccd30b 100644 --- a/OfficeIMO.Examples/Word/Sections/Sections.Example1.cs +++ b/OfficeIMO.Examples/Word/Sections/Sections.Example1.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -39,6 +39,8 @@ internal static void Example_BasicSections2(string folderPath, bool openWord) { Console.WriteLine("+ PageOrientation section 1: " + document.Sections[1].PageOrientation); Console.WriteLine("+ PageOrientation section 2: " + document.Sections[2].PageOrientation); + Console.WriteLine("+ Paragraphs section 0 Text: " + document.Sections[0].Paragraphs[0].Text); + Console.WriteLine("+ Paragraphs section 2 Text: " + document.Sections[2].Paragraphs[0].Text); document.Save(); } @@ -52,6 +54,8 @@ internal static void Example_BasicSections2(string folderPath, bool openWord) { Console.WriteLine("+ PageOrientation section 1: " + document.Sections[1].PageOrientation); Console.WriteLine("+ PageOrientation section 2: " + document.Sections[2].PageOrientation); + Console.WriteLine("+ Paragraphs section 0 Text: " + document.Sections[0].Paragraphs[0].Text); + Console.WriteLine("+ Paragraphs section 2 Text: " + document.Sections[2].Paragraphs[0].Text); var section1 = document.AddSection(); section1.AddParagraph("Test Section4"); diff --git a/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample1.cs b/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample1.cs index 3c54f71b..2ef15d4e 100644 --- a/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample1.cs +++ b/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample1.cs @@ -1,11 +1,7 @@ using System; - using DocumentFormat.OpenXml.Drawing.Wordprocessing; - using OfficeIMO.Word; - using Color = SixLabors.ImageSharp.Color; -using HorizontalAlignmentValues = DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues; namespace OfficeIMO.Examples.Word { internal static partial class WordTextBox { @@ -38,7 +34,7 @@ internal static void Example_AddingTextbox(string folderPath, bool openWord) { Console.WriteLine("Alignment: " + textBox.HorizontalAlignment); - textBox.HorizontalAlignment = HorizontalAlignmentValues.Right; + textBox.HorizontalAlignment = WordHorizontalAlignmentValues.Right; //textBox.HorizontalPositionOffset = 1901950; diff --git a/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample2.cs b/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample2.cs index 6c6f89d0..ccd75dcf 100644 --- a/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample2.cs +++ b/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample2.cs @@ -1,14 +1,10 @@ -using System; - using DocumentFormat.OpenXml.Drawing.Wordprocessing; -using DocumentFormat.OpenXml.Spreadsheet; using DocumentFormat.OpenXml.Wordprocessing; - using OfficeIMO.Word; - using Color = SixLabors.ImageSharp.Color; using HorizontalAlignmentValues = DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues; + namespace OfficeIMO.Examples.Word { internal static partial class WordTextBox { internal static void Example_AddingTextbox2(string folderPath, bool openWord) { @@ -23,14 +19,14 @@ internal static void Example_AddingTextbox2(string folderPath, bool openWord) { textBox.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page; // horizontal alignment overwrites the horizontal position offset so only one will work - textBox.HorizontalAlignment = HorizontalAlignmentValues.Left; + textBox.HorizontalAlignment = WordHorizontalAlignmentValues.Left; textBox.VerticalPositionOffsetCentimeters = 3; var textBox2 = document.AddTextBox("My textbox on the right"); textBox2.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page; textBox2.Paragraphs[0].ParagraphAlignment = JustificationValues.Right; // horizontal alignment overwrites the horizontal position offset so only one will work - textBox2.HorizontalAlignment = HorizontalAlignmentValues.Right; + textBox2.HorizontalAlignment = WordHorizontalAlignmentValues.Right; textBox2.VerticalPositionOffsetCentimeters = 3; Console.WriteLine(textBox.VerticalPositionOffsetCentimeters); @@ -41,7 +37,7 @@ internal static void Example_AddingTextbox2(string folderPath, bool openWord) { var textBox3 = document.AddTextBox("My textbox in the center with borders"); textBox3.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page; - textBox3.HorizontalAlignment = HorizontalAlignmentValues.Center; + textBox3.HorizontalAlignment = WordHorizontalAlignmentValues.Center; textBox3.VerticalPositionOffsetCentimeters = 10; textBox3.Paragraphs[0].Borders.BottomStyle = BorderValues.BasicWideOutline; textBox3.Paragraphs[0].Borders.BottomSize = 10; diff --git a/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample3.cs b/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample3.cs index 12279291..70549eb3 100644 --- a/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample3.cs +++ b/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample3.cs @@ -1,10 +1,5 @@ -using System; using DocumentFormat.OpenXml.Drawing.Wordprocessing; -using DocumentFormat.OpenXml.Spreadsheet; -using DocumentFormat.OpenXml.Wordprocessing; using OfficeIMO.Word; -using Color = SixLabors.ImageSharp.Color; -using HorizontalAlignmentValues = DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues; namespace OfficeIMO.Examples.Word { internal static partial class WordTextBox { @@ -22,14 +17,14 @@ internal static void Example_AddingTextbox3(string folderPath, bool openWord) { textBox.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page; // horizontal alignment overwrites the horizontal position offset so only one will work - textBox.HorizontalAlignment = HorizontalAlignmentValues.Left; + textBox.HorizontalAlignment = WordHorizontalAlignmentValues.Left; textBox.VerticalPositionOffsetCentimeters = 3; var textBox2 = document.AddTextBox("My textbox on the right"); textBox2.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page; // textBox2.WordParagraph.ParagraphAlignment = JustificationValues.Right; // horizontal alignment overwrites the horizontal position offset so only one will work - textBox2.HorizontalAlignment = HorizontalAlignmentValues.Right; + textBox2.HorizontalAlignment = WordHorizontalAlignmentValues.Right; textBox2.VerticalPositionOffsetCentimeters = 3; Console.WriteLine(textBox.VerticalPositionOffsetCentimeters); diff --git a/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample4.cs b/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample4.cs index fba3127b..caff7d4a 100644 --- a/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample4.cs +++ b/OfficeIMO.Examples/Word/WordTextBox/WordTextBox.Sample4.cs @@ -1,11 +1,5 @@ -using System; using DocumentFormat.OpenXml.Drawing.Wordprocessing; -using DocumentFormat.OpenXml.Spreadsheet; -using DocumentFormat.OpenXml.Wordprocessing; using OfficeIMO.Word; -using Color = SixLabors.ImageSharp.Color; -using HorizontalAlignmentValues = DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues; -using Text = DocumentFormat.OpenXml.Spreadsheet.Text; namespace OfficeIMO.Examples.Word { internal static partial class WordTextBox { @@ -27,51 +21,44 @@ internal static void Example_AddingTextbox4(string folderPath, bool openWord) { Console.WriteLine("Textbox (header) wraptext: " + textBox.WrapText); - //var textBox1 = document.AddTextBox("My textbox 1 left - InLineWithText", WrapTextImage.InLineWithText); - //textBox1.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page; - //textBox1.HorizontalAlignment = HorizontalAlignmentValues.Left; - //textBox1.VerticalPositionOffsetCentimeters = 6; - - //Console.WriteLine("Textbox1 (body) wraptext (InLineWithText): " + textBox1.WrapText); - var textBox2 = document.AddTextBox("My textbox 2 right - square", WrapTextImage.Square); textBox2.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page; - textBox2.HorizontalAlignment = HorizontalAlignmentValues.Right; + textBox2.HorizontalAlignment = WordHorizontalAlignmentValues.Right; textBox2.VerticalPositionOffsetCentimeters = 6; Console.WriteLine("Textbox2 (body) wraptext (Square): " + textBox2.WrapText); var textBox3 = document.AddTextBox("My textbox 3 center - tight", WrapTextImage.Tight); textBox3.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page; - textBox3.HorizontalAlignment = HorizontalAlignmentValues.Center; + textBox3.HorizontalAlignment = WordHorizontalAlignmentValues.Center; textBox3.VerticalPositionOffsetCentimeters = 6; Console.WriteLine("Textbox3 (body) wraptext (Tight): " + textBox3.WrapText); var textBox4 = document.AddTextBox("My textbox 4 left - behind text", WrapTextImage.BehindText); textBox4.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page; - textBox4.HorizontalAlignment = HorizontalAlignmentValues.Left; + textBox4.HorizontalAlignment = WordHorizontalAlignmentValues.Left; textBox4.VerticalPositionOffsetCentimeters = 9; Console.WriteLine("Textbox4 (body) wraptext (BehindText): " + textBox4.WrapText); var textBox5 = document.AddTextBox("My textbox 5 right - in front of text", WrapTextImage.InFrontOfText); textBox5.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page; - textBox5.HorizontalAlignment = HorizontalAlignmentValues.Right; + textBox5.HorizontalAlignment = WordHorizontalAlignmentValues.Right; textBox5.VerticalPositionOffsetCentimeters = 9; Console.WriteLine("Textbox5 (body) wraptext (InFrontOfText): " + textBox5.WrapText); var textBox6 = document.AddTextBox("My textbox 6 left - top and bottom", WrapTextImage.TopAndBottom); textBox6.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page; - textBox6.HorizontalAlignment = HorizontalAlignmentValues.Left; + textBox6.HorizontalAlignment = WordHorizontalAlignmentValues.Left; textBox6.VerticalPositionOffsetCentimeters = 12; Console.WriteLine("Textbox6 (body) wraptext (TopAndBottom): " + textBox6.WrapText); var textBox7 = document.AddTextBox("My textbox 7 right - through", WrapTextImage.Through); textBox7.HorizontalPositionRelativeFrom = HorizontalRelativePositionValues.Page; - textBox7.HorizontalAlignment = HorizontalAlignmentValues.Right; + textBox7.HorizontalAlignment = WordHorizontalAlignmentValues.Right; textBox7.VerticalPositionOffsetCentimeters = 12; Console.WriteLine("Textbox7 (body) wraptext (Through): " + textBox7.WrapText); diff --git a/OfficeIMO.Excel/OfficeIMO.Excel.csproj b/OfficeIMO.Excel/OfficeIMO.Excel.csproj index 22e61059..9f4d8b60 100644 --- a/OfficeIMO.Excel/OfficeIMO.Excel.csproj +++ b/OfficeIMO.Excel/OfficeIMO.Excel.csproj @@ -5,7 +5,7 @@ OfficeIMO.Excel OfficeIMO.Excel - 0.1.1 + 1.0.0 netstandard2.0;netstandard2.1;net472;net48;net6.0;net7.0;net8.0 net6.0;net7.0;net8.0 False @@ -14,15 +14,16 @@ Przemyslaw Klys OfficeIMO.Excel - word;office;openxml;net472;net48;net50;netstandard;netstandard2.0,netstandard2.1 - https://github.com/evotecit/OfficeIMO - https://github.com/evotecit/OfficeIMO/blob/master/License.md + docx;net60;excel;office;openxml;net472;net48;net50;netstandard;netstandard2.0,netstandard2.1;net70 + https://github.com/EvotecIT/OfficeIMO + https://github.com/EvotecIT/OfficeIMO/blob/master/LICENSE False True - (c) 2011 - 2022 Przemyslaw Klys @ Evotec. All rights reserved. + (c) 2011 - 2024 Przemyslaw Klys @ Evotec. All rights reserved. OfficeIMO.png - https://github.com/evotecit/OfficeIMO + https://github.com/EvotecIT/OfficeIMO portable + Latest /word/footer1.xml - + @@ -12118,7 +12118,7 @@ - + @@ -12156,7 +12156,7 @@ /docProps/custom.xml - + 9999-12-31T23:59:59Z diff --git a/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicEmptyWord.verified.txt b/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicEmptyWord.verified.txt index 8dcf9712..777be140 100644 --- a/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicEmptyWord.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicEmptyWord.verified.txt @@ -23,7 +23,7 @@ - + diff --git a/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWord.verified.txt b/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWord.verified.txt index dccf5fd1..0d560400 100644 --- a/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWord.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWord.verified.txt @@ -56,7 +56,7 @@ , and we still continue adding more text to existing paragraph. - + diff --git a/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWordWithBreaks.verified.txt b/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWordWithBreaks.verified.txt index 6a5c315a..b5919307 100644 --- a/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWordWithBreaks.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWordWithBreaks.verified.txt @@ -59,7 +59,7 @@ Adding paragraph4 with some text and pressing SHIFT+ENTER - + diff --git a/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWordWithDefaultFontChange.verified.txt b/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWordWithDefaultFontChange.verified.txt index ee9b128f..2d80b636 100644 --- a/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWordWithDefaultFontChange.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWordWithDefaultFontChange.verified.txt @@ -35,7 +35,7 @@ INSTRUMENTO PARTICULAR DE CONSTITUIÇÃO DE GARANTIA DE ALIENAÇÃO FIDUCIÁRIA DE IMÓVEL - + diff --git a/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWordWithDefaultStyleChange.verified.txt b/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWordWithDefaultStyleChange.verified.txt index ba4df08e..6a408c9f 100644 --- a/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWordWithDefaultStyleChange.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/BasicDocumentTests.BasicWordWithDefaultStyleChange.verified.txt @@ -39,7 +39,7 @@ Adding paragraph1 with some text and pressing ENTER - + diff --git a/OfficeIMO.VerifyTests/Word/verified/BookmarkTests.BasicWordWithBookmarks.verified.txt b/OfficeIMO.VerifyTests/Word/verified/BookmarkTests.BasicWordWithBookmarks.verified.txt index 899d6bbb..a15207e2 100644 --- a/OfficeIMO.VerifyTests/Word/verified/BookmarkTests.BasicWordWithBookmarks.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/BookmarkTests.BasicWordWithBookmarks.verified.txt @@ -126,7 +126,7 @@ Test 5 - + diff --git a/OfficeIMO.VerifyTests/Word/verified/BordersAndMarginsTests.BasicPageBorders.verified.txt b/OfficeIMO.VerifyTests/Word/verified/BordersAndMarginsTests.BasicPageBorders.verified.txt index 2b67a9ba..206974fe 100644 --- a/OfficeIMO.VerifyTests/Word/verified/BordersAndMarginsTests.BasicPageBorders.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/BordersAndMarginsTests.BasicPageBorders.verified.txt @@ -29,7 +29,7 @@ Section 0 - + diff --git a/OfficeIMO.VerifyTests/Word/verified/BordersAndMarginsTests.BasicPageBorders2.verified.txt b/OfficeIMO.VerifyTests/Word/verified/BordersAndMarginsTests.BasicPageBorders2.verified.txt index 93000e98..88342ed5 100644 --- a/OfficeIMO.VerifyTests/Word/verified/BordersAndMarginsTests.BasicPageBorders2.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/BordersAndMarginsTests.BasicPageBorders2.verified.txt @@ -32,7 +32,7 @@ - + @@ -42,7 +42,7 @@ - + diff --git a/OfficeIMO.VerifyTests/Word/verified/BordersAndMarginsTests.BasicWordMarginsSizes.verified.txt b/OfficeIMO.VerifyTests/Word/verified/BordersAndMarginsTests.BasicWordMarginsSizes.verified.txt index b30f4dc7..ac70e468 100644 --- a/OfficeIMO.VerifyTests/Word/verified/BordersAndMarginsTests.BasicWordMarginsSizes.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/BordersAndMarginsTests.BasicWordMarginsSizes.verified.txt @@ -31,7 +31,7 @@ - + @@ -44,7 +44,7 @@ - + @@ -57,7 +57,7 @@ - + @@ -70,7 +70,7 @@ - + @@ -81,7 +81,7 @@ Section 4 - + diff --git a/OfficeIMO.VerifyTests/Word/verified/ChartTests.AddingMultipleCharts.verified.txt b/OfficeIMO.VerifyTests/Word/verified/ChartTests.AddingMultipleCharts.verified.txt index e2fa4ccb..4b96d7e4 100644 --- a/OfficeIMO.VerifyTests/Word/verified/ChartTests.AddingMultipleCharts.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/ChartTests.AddingMultipleCharts.verified.txt @@ -138,7 +138,7 @@ - + diff --git a/OfficeIMO.VerifyTests/Word/verified/CommentTests.PlayingWithComments.verified.txt b/OfficeIMO.VerifyTests/Word/verified/CommentTests.PlayingWithComments.verified.txt index 84d3987b..1f455918 100644 --- a/OfficeIMO.VerifyTests/Word/verified/CommentTests.PlayingWithComments.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/CommentTests.PlayingWithComments.verified.txt @@ -45,7 +45,7 @@ Test Section - another line - + diff --git a/OfficeIMO.VerifyTests/Word/verified/CoverPageTests.AddingCoverPage.verified.txt b/OfficeIMO.VerifyTests/Word/verified/CoverPageTests.AddingCoverPage.verified.txt index e4ec8d09..486e7bbc 100644 --- a/OfficeIMO.VerifyTests/Word/verified/CoverPageTests.AddingCoverPage.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/CoverPageTests.AddingCoverPage.verified.txt @@ -452,7 +452,7 @@ More on the next page - + diff --git a/OfficeIMO.VerifyTests/Word/verified/CoverPageTests.AddingCoverPage2.verified.txt b/OfficeIMO.VerifyTests/Word/verified/CoverPageTests.AddingCoverPage2.verified.txt index 3db7bc20..eb8e5404 100644 --- a/OfficeIMO.VerifyTests/Word/verified/CoverPageTests.AddingCoverPage2.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/CoverPageTests.AddingCoverPage2.verified.txt @@ -371,7 +371,7 @@ More on the next page - + diff --git a/OfficeIMO.VerifyTests/Word/verified/CustomAndBuiltinPropertiesTests.BasicDocumentProperties.verified.txt b/OfficeIMO.VerifyTests/Word/verified/CustomAndBuiltinPropertiesTests.BasicDocumentProperties.verified.txt index 50860e88..5d5890af 100644 --- a/OfficeIMO.VerifyTests/Word/verified/CustomAndBuiltinPropertiesTests.BasicDocumentProperties.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/CustomAndBuiltinPropertiesTests.BasicDocumentProperties.verified.txt @@ -34,7 +34,7 @@ Basic paragraph - + diff --git a/OfficeIMO.VerifyTests/Word/verified/CustomAndBuiltinPropertiesTests.ValidateDocument.verified.txt b/OfficeIMO.VerifyTests/Word/verified/CustomAndBuiltinPropertiesTests.ValidateDocument.verified.txt index 68e9ce22..1c37f187 100644 --- a/OfficeIMO.VerifyTests/Word/verified/CustomAndBuiltinPropertiesTests.ValidateDocument.verified.txt +++ b/OfficeIMO.VerifyTests/Word/verified/CustomAndBuiltinPropertiesTests.ValidateDocument.verified.txt @@ -31,7 +31,7 @@ Basic paragraph - Page 4 - + @@ -10851,7 +10851,7 @@ /docProps/custom.xml - + 9999-12-31T23:59:59Z diff --git a/OfficeIMO.Word/CustomImagePartType.cs b/OfficeIMO.Word/CustomImagePartType.cs new file mode 100644 index 00000000..cd3904c1 --- /dev/null +++ b/OfficeIMO.Word/CustomImagePartType.cs @@ -0,0 +1,24 @@ +using DocumentFormat.OpenXml.Packaging; + +namespace OfficeIMO.Word; + +public enum CustomImagePartType { + Bmp, + Gif, + Jpeg, + Png, + Tiff +} + +public static class CustomImagePartTypeExtensions { + public static string ToOpenXmlImagePartType(this CustomImagePartType customType) { + return customType switch { + CustomImagePartType.Bmp => "image/bmp", + CustomImagePartType.Gif => "image/gif", + CustomImagePartType.Jpeg => "image/jpeg", + CustomImagePartType.Png => "image/png", + CustomImagePartType.Tiff => "image/tiff", + _ => throw new ArgumentOutOfRangeException(nameof(customType), customType, null) + }; + } +} diff --git a/OfficeIMO.Word/ExtensionsHeadersAndFooters.cs b/OfficeIMO.Word/ExtensionsHeadersAndFooters.cs index 691534fb..2c11ab29 100644 --- a/OfficeIMO.Word/ExtensionsHeadersAndFooters.cs +++ b/OfficeIMO.Word/ExtensionsHeadersAndFooters.cs @@ -161,9 +161,50 @@ internal static void AddFooterReference(WordDocument document, WordSection secti /// /// internal static SectionProperties AddSectionProperties(this WordprocessingDocument wordDocument) { - var sectionProperties = new SectionProperties(); + var sectionProperties = CreateSectionProperties(); wordDocument.MainDocumentPart.Document.Body.Append(sectionProperties); return sectionProperties; } + + /// + /// Some documents might not have section properties with proper rsidR. This method will fix that. + /// Due to the nature of the rsidR, it is important to have unique rsidR for each section. + /// Otherwise, comparison of sections will not work properly. + /// + /// + /// + internal static SectionProperties MakeSureSectionIsValid(this SectionProperties sectionProperties) { + if (sectionProperties.RsidR == null) { + sectionProperties.RsidR = GenerateRsid(); + } + + return sectionProperties; + } + + /// + /// Generate a unique rsid + /// + /// + internal static string GenerateRsid() { + // Generate a unique rsid using a GUID + return Guid.NewGuid().ToString("N").Substring(0, 8).ToUpper(); + } + + /// + /// Create a new section properties + /// + /// + internal static SectionProperties CreateSectionProperties() { + SectionProperties sectionProperties = new SectionProperties() { RsidR = GenerateRsid() }; + + // Set the page size and margins + //PageSize pageSize = new PageSize() { Width = 12240, Height = 15840 }; // A4 size + //PageMargin pageMargin = new PageMargin() { Top = 1440, Right = 1440, Bottom = 1440, Left = 1440 }; // 1 inch margins + + //sectionProperties.Append(pageSize); + //sectionProperties.Append(pageMargin); + + return sectionProperties; + } } } diff --git a/OfficeIMO.Word/Helpers.cs b/OfficeIMO.Word/Helpers.cs index b7328be0..68835fbb 100644 --- a/OfficeIMO.Word/Helpers.cs +++ b/OfficeIMO.Word/Helpers.cs @@ -66,20 +66,20 @@ public static bool IsFileLocked(this string fileName) { return IsFileLocked(new FileInfo(fileName)); } - internal static ImageСharacteristics GetImageСharacteristics(Stream imageStream) { + internal static ImageCharacteristics GetImageCharacteristics(Stream imageStream) { using var img = SixLabors.ImageSharp.Image.Load(imageStream, out var imageFormat); imageStream.Position = 0; var type = ConvertToImagePartType(imageFormat); - return new ImageСharacteristics(img.Width, img.Height, type); + return new ImageCharacteristics(img.Width, img.Height, type); } - private static ImagePartType ConvertToImagePartType(IImageFormat imageFormat) => + private static CustomImagePartType ConvertToImagePartType(IImageFormat imageFormat) => imageFormat.Name switch { - "BMP" => ImagePartType.Bmp, - "GIF" => ImagePartType.Gif, - "JPEG" => ImagePartType.Jpeg, - "PNG" => ImagePartType.Png, - "TIFF" => ImagePartType.Tiff, + "BMP" => CustomImagePartType.Bmp, + "GIF" => CustomImagePartType.Gif, + "JPEG" => CustomImagePartType.Jpeg, + "PNG" => CustomImagePartType.Png, + "TIFF" => CustomImagePartType.Tiff, _ => throw new ImageFormatNotSupportedException($"Image format not supported: {imageFormat.Name}.") }; @@ -164,5 +164,5 @@ internal static UInt32 ConvertCentimetersToTwipsUInt32(double cmValue) { } } - internal record ImageСharacteristics(double Width, double Height, ImagePartType Type); + internal record ImageCharacteristics(double Width, double Height, CustomImagePartType Type); } diff --git a/OfficeIMO.Word/OfficeIMO.Word.csproj b/OfficeIMO.Word/OfficeIMO.Word.csproj index eed6f8ec..adfe35f8 100644 --- a/OfficeIMO.Word/OfficeIMO.Word.csproj +++ b/OfficeIMO.Word/OfficeIMO.Word.csproj @@ -5,7 +5,7 @@ OfficeIMO.Word OfficeIMO.Word - 0.18.0 + 1.0.0 netstandard2.0;netstandard2.1;net472;net48;net6.0;net7.0;net8.0 net6.0;net7.0;net8.0 False @@ -14,13 +14,13 @@ OfficeIMO.Word docx;net60;word;office;openxml;net472;net48;net50;netstandard;netstandard2.0,netstandard2.1;net70 - https://github.com/evotecit/OfficeIMO - https://github.com/evotecit/OfficeIMO/blob/master/License.md + https://github.com/EvotecIT/OfficeIMO + https://github.com/EvotecIT/OfficeIMO/blob/master/LICENSE False True - (c) 2011 - 2023 Przemyslaw Klys @ Evotec. All rights reserved. + (c) 2011 - 2024 Przemyslaw Klys @ Evotec. All rights reserved. OfficeIMO.png - https://github.com/evotecit/OfficeIMO + https://github.com/EvotecIT/OfficeIMO portable False OfficeIMO.ico - README.MD + README.md git False True + Latest @@ -62,7 +63,7 @@ - + diff --git a/OfficeIMO.Word/Security.cs b/OfficeIMO.Word/Security.cs index 9b8dd411..6797c453 100644 --- a/OfficeIMO.Word/Security.cs +++ b/OfficeIMO.Word/Security.cs @@ -40,7 +40,8 @@ private static byte[] ConcatByteArrays(byte[] array1, byte[] array2) { return result; } - internal static void ProtectWordDoc(WordprocessingDocument wordDocument, string password, DocumentProtectionValues documentProtectionValue = DocumentProtectionValues.ReadOnly) { + internal static void ProtectWordDoc(WordprocessingDocument wordDocument, string password, DocumentProtectionValues? documentProtectionValue = null) { + documentProtectionValue ??= DocumentProtectionValues.ReadOnly; // Generate the Salt byte[] arrSalt = new byte[16]; RandomNumberGenerator rand = new RNGCryptoServiceProvider(); diff --git a/OfficeIMO.Word/WordAlternativeFormatImportPartType.cs b/OfficeIMO.Word/WordAlternativeFormatImportPartType.cs new file mode 100644 index 00000000..139a2db5 --- /dev/null +++ b/OfficeIMO.Word/WordAlternativeFormatImportPartType.cs @@ -0,0 +1,10 @@ +namespace OfficeIMO.Word; + +/// +/// Alternative format import part type +/// +public enum WordAlternativeFormatImportPartType { + Rtf, + Html, + TextPlain +} diff --git a/OfficeIMO.Word/WordChart.PrivateMethods.cs b/OfficeIMO.Word/WordChart.PrivateMethods.cs index 5be19df8..c97afea1 100644 --- a/OfficeIMO.Word/WordChart.PrivateMethods.cs +++ b/OfficeIMO.Word/WordChart.PrivateMethods.cs @@ -144,7 +144,8 @@ private Chart GenerateChartBar(Chart chart) { return chart; } - private BarChart CreateBarChart(BarDirectionValues barDirection = BarDirectionValues.Bar) { + private BarChart CreateBarChart(BarDirectionValues? barDirection = null) { + barDirection ??= BarDirectionValues.Bar; BarChart barChart1 = new BarChart(); barChart1.AddNamespaceDeclaration("c", "http://schemas.openxmlformats.org/drawingml/2006/chart"); diff --git a/OfficeIMO.Word/WordChart.cs b/OfficeIMO.Word/WordChart.cs index ba043f9c..840e9e1d 100644 --- a/OfficeIMO.Word/WordChart.cs +++ b/OfficeIMO.Word/WordChart.cs @@ -19,7 +19,7 @@ public WordChart(WordDocument document, WordParagraph paragraph, Drawing drawing _paragraph = paragraph; } - public WordChart(WordDocument document, WordParagraph paragraph, string title = null, bool roundedCorners = false, int width = 600, int height = 600) { + public WordChart(WordDocument document, WordParagraph paragraph, string title = "", bool roundedCorners = false, int width = 600, int height = 600) { _document = document; _paragraph = paragraph; SetTitle(title); @@ -186,7 +186,7 @@ private ChartPart CreateChartPart(WordDocument document, bool roundedCorners) { return part; } - private Chart GenerateChart(string title = null) { + private Chart GenerateChart(string title = "") { Chart chart1 = new Chart(); AutoTitleDeleted autoTitleDeleted1 = new AutoTitleDeleted() { Val = false }; PlotArea plotArea1 = new PlotArea() { Layout = new Layout() }; @@ -207,14 +207,22 @@ private Chart GenerateChart(string title = null) { return chart1; } + /// + /// Set the title of the chart + /// + /// + /// public WordChart SetTitle(string title) { PrivateTitle = title; UpdateTitle(); return this; } + /// + /// Update the title of the chart + /// internal void UpdateTitle() { - if (PrivateTitle != null) { + if (!string.IsNullOrEmpty(PrivateTitle)) { if (_chart != null) { if (_chart.Title == null) { _chart.Append(AddTitle(PrivateTitle)); diff --git a/OfficeIMO.Word/WordDocument.PublicMethods.cs b/OfficeIMO.Word/WordDocument.PublicMethods.cs index 2fd821d9..956176a1 100644 --- a/OfficeIMO.Word/WordDocument.PublicMethods.cs +++ b/OfficeIMO.Word/WordDocument.PublicMethods.cs @@ -34,7 +34,8 @@ public void AddHeadersAndFooters() { WordHeadersAndFooters.AddHeadersAndFooters(this); } - public WordParagraph AddBreak(BreakValues breakType = BreakValues.Page) { + public WordParagraph AddBreak(BreakValues? breakType = null) { + breakType ??= BreakValues.Page; WordParagraph newWordParagraph = new WordParagraph { _run = new Run(new Break() { Type = breakType }), _document = this @@ -68,7 +69,7 @@ public WordParagraph AddHyperLink(string text, string anchor, bool addStyle = fa /// The width. /// The height. /// WordChart - public WordChart AddChart(string title = null, bool roundedCorners = false, int width = 600, int height = 600) { + public WordChart AddChart(string title = "", bool roundedCorners = false, int width = 600, int height = 600) { var paragraph = this.AddParagraph(); var chartInstance = new WordChart(this, paragraph, title, roundedCorners, width, height); return chartInstance; @@ -106,18 +107,17 @@ public WordTextBox AddTextBox(string text, WrapTextImage wrapTextImage = WrapTex return wordTextBox; } - public WordParagraph AddHorizontalLine(BorderValues lineType = BorderValues.Single, SixLabors.ImageSharp.Color? color = null, uint size = 12, uint space = 1) { - return this.AddParagraph().AddHorizontalLine(lineType, color, size, space); + public WordParagraph AddHorizontalLine(BorderValues? lineType = null, SixLabors.ImageSharp.Color? color = null, uint size = 12, uint space = 1) { + lineType ??= BorderValues.Single; + return this.AddParagraph().AddHorizontalLine(lineType.Value, color, size, space); } public WordSection AddSection(SectionMarkValues? sectionMark = null) { - //Paragraph paragraph = new Paragraph() { RsidParagraphAddition = "fff0", RsidRunAdditionDefault = "fff0"}; Paragraph paragraph = new Paragraph(); ParagraphProperties paragraphProperties = new ParagraphProperties(); - SectionProperties sectionProperties = new SectionProperties(); - // SectionProperties sectionProperties = new SectionProperties() { RsidR = "fff0" }; + SectionProperties sectionProperties = WordHeadersAndFooters.CreateSectionProperties(); if (sectionMark != null) { SectionType sectionType = new SectionType() { Val = sectionMark }; @@ -144,14 +144,12 @@ public WordParagraph AddField(WordFieldType wordFieldType, WordFieldFormat? word return this.AddParagraph().AddField(wordFieldType, wordFieldFormat, advanced, parameters); } - public WordEmbeddedDocument AddEmbeddedDocument(string fileName, AlternativeFormatImportPartType? type = null) { - WordEmbeddedDocument embeddedDocument = new WordEmbeddedDocument(this, fileName, type, false); - return embeddedDocument; + public WordEmbeddedDocument AddEmbeddedDocument(string fileName, WordAlternativeFormatImportPartType? type = null) { + return new WordEmbeddedDocument(this, fileName, type, false); } - public WordEmbeddedDocument AddEmbeddedFragment(string htmlContent, AlternativeFormatImportPartType type) { - WordEmbeddedDocument embeddedDocument = new WordEmbeddedDocument(this, htmlContent, type, true); - return embeddedDocument; + public WordEmbeddedDocument AddEmbeddedFragment(string htmlContent, WordAlternativeFormatImportPartType type) { + return new WordEmbeddedDocument(this, htmlContent, type, true); } diff --git a/OfficeIMO.Word/WordDocument.cs b/OfficeIMO.Word/WordDocument.cs index de25d101..000faf38 100644 --- a/OfficeIMO.Word/WordDocument.cs +++ b/OfficeIMO.Word/WordDocument.cs @@ -750,7 +750,7 @@ public void Open(string filePath = "", bool openWord = true) { /// /// /// - private static void CopyPackageProperties(PackageProperties src, PackageProperties dest) { + private static void CopyPackageProperties(IPackageProperties src, IPackageProperties dest) { dest.Category = src.Category; dest.ContentStatus = src.ContentStatus; dest.ContentType = src.ContentType; @@ -899,7 +899,6 @@ public void Dispose() { } catch { // ignored } - this._wordprocessingDocument.Dispose(); } if (_fileStream != null) { diff --git a/OfficeIMO.Word/WordEmbeddedDocument.cs b/OfficeIMO.Word/WordEmbeddedDocument.cs index 801f47d8..2e03de7d 100644 --- a/OfficeIMO.Word/WordEmbeddedDocument.cs +++ b/OfficeIMO.Word/WordEmbeddedDocument.cs @@ -68,16 +68,16 @@ public WordEmbeddedDocument(WordDocument wordDocument, AltChunk altChunk) { } } - public WordEmbeddedDocument(WordDocument wordDocument, string fileNameOrContent, AlternativeFormatImportPartType? alternativeFormatImportPartType, bool htmlFragment) { - AlternativeFormatImportPartType partType; + public WordEmbeddedDocument(WordDocument wordDocument, string fileNameOrContent, WordAlternativeFormatImportPartType? alternativeFormatImportPartType, bool htmlFragment) { + WordAlternativeFormatImportPartType partType; if (alternativeFormatImportPartType == null) { FileInfo fileInfo = new FileInfo(fileNameOrContent); if (fileInfo.Extension == ".rtf") { - partType = AlternativeFormatImportPartType.Rtf; + partType = WordAlternativeFormatImportPartType.Rtf; } else if (fileInfo.Extension == ".html") { - partType = AlternativeFormatImportPartType.Html; + partType = WordAlternativeFormatImportPartType.Html; } else if (fileInfo.Extension == ".log" || fileInfo.Extension == ".txt") { - partType = AlternativeFormatImportPartType.TextPlain; + partType = WordAlternativeFormatImportPartType.TextPlain; } else { throw new Exception("Only RTF and HTML files are supported for now :-)"); } @@ -91,7 +91,14 @@ public WordEmbeddedDocument(WordDocument wordDocument, string fileNameOrContent, MainDocumentPart mainDocPart = wordDocument._document.MainDocumentPart; - AlternativeFormatImportPart chunk = mainDocPart.AddAlternativeFormatImportPart(partType, altChunk.Id); + PartTypeInfo partTypeInfo = partType switch { + WordAlternativeFormatImportPartType.Rtf => AlternativeFormatImportPartType.Rtf, + WordAlternativeFormatImportPartType.Html => AlternativeFormatImportPartType.Html, + WordAlternativeFormatImportPartType.TextPlain => AlternativeFormatImportPartType.TextPlain, + _ => throw new Exception("Unsupported format type") + }; + + AlternativeFormatImportPart chunk = mainDocPart.AddAlternativeFormatImportPart(partTypeInfo, altChunk.Id); // if it's a fragment, we don't need to read the file var documentContent = htmlFragment ? fileNameOrContent : File.ReadAllText(fileNameOrContent, Encoding.ASCII); diff --git a/OfficeIMO.Word/WordEquation.cs b/OfficeIMO.Word/WordEquation.cs index 8b5ebe5c..cd92bfb1 100644 --- a/OfficeIMO.Word/WordEquation.cs +++ b/OfficeIMO.Word/WordEquation.cs @@ -9,7 +9,7 @@ namespace OfficeIMO.Word { public class WordEquation : WordElement { private WordDocument _document; private Paragraph _paragraph; - private DocumentFormat.OpenXml.Math.Paragraph mathParagraph; + //private DocumentFormat.OpenXml.Math.Paragraph mathParagraph; private readonly DocumentFormat.OpenXml.Math.OfficeMath _officeMath; private readonly DocumentFormat.OpenXml.Math.Paragraph _mathParagraph; diff --git a/OfficeIMO.Word/WordField.PrivateMethods.cs b/OfficeIMO.Word/WordField.PrivateMethods.cs index ee3667b6..8aac7530 100644 --- a/OfficeIMO.Word/WordField.PrivateMethods.cs +++ b/OfficeIMO.Word/WordField.PrivateMethods.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -117,4 +117,4 @@ private static Run AddFieldEnd() { } } -} \ No newline at end of file +} diff --git a/OfficeIMO.Word/WordField.PublicMethods.cs b/OfficeIMO.Word/WordField.PublicMethods.cs index 4d926549..9b53f074 100644 --- a/OfficeIMO.Word/WordField.PublicMethods.cs +++ b/OfficeIMO.Word/WordField.PublicMethods.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using DocumentFormat.OpenXml.Wordprocessing; diff --git a/OfficeIMO.Word/WordHeaderFooter.Methods.cs b/OfficeIMO.Word/WordHeaderFooter.Methods.cs index 8efb0cfd..0b48c202 100644 --- a/OfficeIMO.Word/WordHeaderFooter.Methods.cs +++ b/OfficeIMO.Word/WordHeaderFooter.Methods.cs @@ -31,8 +31,9 @@ public WordParagraph AddHyperLink(string text, string anchor, bool addStyle = fa return this.AddParagraph().AddHyperLink(text, anchor, addStyle, tooltip, history); } - public WordParagraph AddHorizontalLine(BorderValues lineType = BorderValues.Single, SixLabors.ImageSharp.Color? color = null, uint size = 12, uint space = 1) { - return this.AddParagraph().AddHorizontalLine(lineType, color, size, space); + public WordParagraph AddHorizontalLine(BorderValues? lineType = null, SixLabors.ImageSharp.Color? color = null, uint size = 12, uint space = 1) { + lineType ??= BorderValues.Single; + return this.AddParagraph().AddHorizontalLine(lineType.Value, color, size, space); } public WordParagraph AddBookmark(string bookmarkName) { diff --git a/OfficeIMO.Word/WordHorizontalAlignmentValues.cs b/OfficeIMO.Word/WordHorizontalAlignmentValues.cs new file mode 100644 index 00000000..8efb0062 --- /dev/null +++ b/OfficeIMO.Word/WordHorizontalAlignmentValues.cs @@ -0,0 +1,48 @@ +namespace OfficeIMO.Word; + +/// +/// Word horizontal alignment values +/// +public enum WordHorizontalAlignmentValues { + Left, + Center, + Right, + Outside +} + +/// +/// Class to help with horizontal alignment values +/// +public static class HorizontalAlignmentHelper { + /// + /// Convert alignment to string + /// + /// + /// + /// + public static string ToString(WordHorizontalAlignmentValues alignment) { + return alignment switch { + WordHorizontalAlignmentValues.Left => "left", + WordHorizontalAlignmentValues.Center => "center", + WordHorizontalAlignmentValues.Right => "right", + WordHorizontalAlignmentValues.Outside => "outside", + _ => throw new ArgumentException($"Invalid alignment value: {alignment}") + }; + } + + /// + /// Convert string to alignment + /// + /// + /// + /// + public static WordHorizontalAlignmentValues FromString(string value) { + return value.ToLowerInvariant() switch { + "left" => WordHorizontalAlignmentValues.Left, + "center" => WordHorizontalAlignmentValues.Center, + "right" => WordHorizontalAlignmentValues.Right, + "outside" => WordHorizontalAlignmentValues.Outside, + _ => throw new ArgumentException($"Invalid alignment value: {value}") + }; + } +} diff --git a/OfficeIMO.Word/WordImage.cs b/OfficeIMO.Word/WordImage.cs index 8045c805..95182e65 100644 --- a/OfficeIMO.Word/WordImage.cs +++ b/OfficeIMO.Word/WordImage.cs @@ -482,39 +482,23 @@ public WrapTextImage? WrapText { set => WordWrapTextImage.SetWrapTextImage(_Image, _Image.Anchor, _Image.Inline, value); } - public WordImage( - WordDocument document, - WordParagraph paragraph, - string filePath, - double? width, - double? height, - WrapTextImage wrapImage = WrapTextImage.InLineWithText, - string description = "", - ShapeTypeValues shape = ShapeTypeValues.Rectangle, - BlipCompressionValues compressionQuality = BlipCompressionValues.Print) { + public WordImage(WordDocument document, WordParagraph paragraph, string filePath, double? width, double? height, WrapTextImage wrapImage = WrapTextImage.InLineWithText, string description = "", ShapeTypeValues? shape = null, BlipCompressionValues? compressionQuality = null) { FilePath = filePath; var fileName = System.IO.Path.GetFileName(filePath); using var imageStream = new FileStream(filePath, FileMode.Open); - AddImage(document, paragraph, imageStream, fileName, width, height, shape, compressionQuality, description, wrapImage); + shape ??= ShapeTypeValues.Rectangle; // Set default value if not provided + compressionQuality ??= BlipCompressionValues.Print; // Set default value if not provided + AddImage(document, paragraph, imageStream, fileName, width, height, shape.Value, compressionQuality.Value, description, wrapImage); } - public WordImage( - WordDocument document, - WordParagraph paragraph, - Stream imageStream, - string fileName, - double? width, - double? height, - WrapTextImage wrapImage = WrapTextImage.InLineWithText, - string description = "", - ShapeTypeValues shape = ShapeTypeValues.Rectangle, - BlipCompressionValues compressionQuality = BlipCompressionValues.Print) { + public WordImage(WordDocument document, WordParagraph paragraph, Stream imageStream, string fileName, double? width, double? height, WrapTextImage wrapImage = WrapTextImage.InLineWithText, string description = "", ShapeTypeValues? shape = null, BlipCompressionValues? compressionQuality = null) { FilePath = fileName; - AddImage(document, paragraph, imageStream, fileName, width, height, shape, compressionQuality, description, wrapImage); + shape ??= ShapeTypeValues.Rectangle; // Set default value if not provided + compressionQuality ??= BlipCompressionValues.Print; // Set default value if not provided + AddImage(document, paragraph, imageStream, fileName, width, height, shape.Value, compressionQuality.Value, description, wrapImage); } private Graphic GetGraphic(double emuWidth, double emuHeight, string fileName, string relationshipId, ShapeTypeValues shape, BlipCompressionValues compressionQuality, string description = "") { - var shapeProperties = new ShapeProperties(); var transform2D = new Transform2D(); var newOffset = new Offset() { X = 0L, Y = 0L }; @@ -699,18 +683,7 @@ public void Remove() { } } - private void AddImage( - WordDocument document, - WordParagraph paragraph, - Stream imageStream, - string fileName, - double? width, - double? height, - ShapeTypeValues shape, - BlipCompressionValues compressionQuality, - string description, - WrapTextImage wrapImage - ) { + private void AddImage(WordDocument document, WordParagraph paragraph, Stream imageStream, string fileName, double? width, double? height, ShapeTypeValues shape, BlipCompressionValues compressionQuality, string description, WrapTextImage wrapImage) { _document = document; var imageLocation = AddImageToLocation(document, paragraph, imageStream, fileName, width, height); @@ -745,12 +718,14 @@ internal static WordImageLocation AddImageToLocation( // Size - https://stackoverflow.com/questions/8082980/inserting-image-into-docx-using-openxml-and-setting-the-size // if widht/height are not set we check ourselves // but probably will need better way - var imageСharacteristics = Helpers.GetImageСharacteristics(imageStream); + var imageCharacteristics = Helpers.GetImageCharacteristics(imageStream); if (width == null || height == null) { - width = imageСharacteristics.Width; - height = imageСharacteristics.Height; + width = imageCharacteristics.Width; + height = imageCharacteristics.Height; } + + var imagePartType = imageCharacteristics.Type; var imageName = System.IO.Path.GetFileNameWithoutExtension(fileName); ImagePart imagePart; @@ -758,15 +733,15 @@ internal static WordImageLocation AddImageToLocation( var location = paragraph.Location(); if (location.GetType() == typeof(Header)) { var part = ((Header)location).HeaderPart; - imagePart = part.AddImagePart(imageСharacteristics.Type); + imagePart = part.AddImagePart(imagePartType.ToOpenXmlImagePartType()); relationshipId = part.GetIdOfPart(imagePart); } else if (location.GetType() == typeof(Footer)) { var part = ((Footer)location).FooterPart; - imagePart = part.AddImagePart(imageСharacteristics.Type); + imagePart = part.AddImagePart(imagePartType.ToOpenXmlImagePartType()); relationshipId = part.GetIdOfPart(imagePart); } else if (location.GetType() == typeof(Document)) { var part = document._wordprocessingDocument.MainDocumentPart; - imagePart = part.AddImagePart(imageСharacteristics.Type); + imagePart = part.AddImagePart(imagePartType.ToOpenXmlImagePartType()); relationshipId = part.GetIdOfPart(imagePart); } else { throw new Exception("Paragraph is not in document or header or footer. This is weird. Probably a bug."); diff --git a/OfficeIMO.Word/WordParagraph.PublicMethods.cs b/OfficeIMO.Word/WordParagraph.PublicMethods.cs index 7aee0900..05fe342a 100644 --- a/OfficeIMO.Word/WordParagraph.PublicMethods.cs +++ b/OfficeIMO.Word/WordParagraph.PublicMethods.cs @@ -229,13 +229,15 @@ public void AddComment(string author, string initials, string comment) { /// The size of the line. /// The space the line takes up. /// The new Paragraph after the line. - public WordParagraph AddHorizontalLine(BorderValues lineType = BorderValues.Single, SixLabors.ImageSharp.Color? color = null, uint size = 12, uint space = 1) { - this._paragraphProperties.ParagraphBorders = new ParagraphBorders(); - this._paragraphProperties.ParagraphBorders.BottomBorder = new BottomBorder() { - Val = lineType, - Size = size, - Space = space, - Color = color != null ? color.Value.ToHexColor() : "auto" + public WordParagraph AddHorizontalLine(BorderValues? lineType = null, SixLabors.ImageSharp.Color? color = null, uint size = 12, uint space = 1) { + lineType ??= BorderValues.Single; + this._paragraphProperties.ParagraphBorders = new ParagraphBorders { + BottomBorder = new BottomBorder() { + Val = lineType.Value, + Size = size, + Space = space, + Color = color != null ? color.Value.ToHexColor() : "auto" + } }; return this; } @@ -325,7 +327,9 @@ public WordTable AddTableBefore(int rows, int columns, WordTableStyle tableStyle /// The optional alignment for the tabs. /// The optional rune to use before the tabs. /// The added tabs. - public WordTabStop AddTabStop(int position, TabStopValues alignment = TabStopValues.Left, TabStopLeaderCharValues leader = TabStopLeaderCharValues.None) { + public WordTabStop AddTabStop(int position, TabStopValues? alignment = null, TabStopLeaderCharValues? leader = null) { + alignment ??= TabStopValues.Left; + leader ??= TabStopLeaderCharValues.None; var wordTab = new WordTabStop(this); wordTab.AddTab(position, alignment, leader); return wordTab; @@ -365,7 +369,7 @@ public WordList AddList(WordListStyle style) { /// The width. /// The height. /// WordChart - public WordChart AddChart(string title = null, bool roundedCorners = false, int width = 600, int height = 600) { + public WordChart AddChart(string title = "", bool roundedCorners = false, int width = 600, int height = 600) { var paragraph = this.AddParagraph(); var chartInstance = new WordChart(this._document, paragraph, title, roundedCorners, width, height); return chartInstance; @@ -377,8 +381,9 @@ public WordChart AddChart(string title = null, bool roundedCorners = false, int /// The text of the note. /// The footnote. public WordParagraph AddFootNote(string text) { - var footerWordParagraph = new WordParagraph(this._document, true, true); - footerWordParagraph.Text = text; + var footerWordParagraph = new WordParagraph(this._document, true, true) { + Text = text + }; var wordFootNote = WordFootNote.AddFootNote(this._document, this, footerWordParagraph); return wordFootNote; diff --git a/OfficeIMO.Word/WordSection.PrivateMethods.cs b/OfficeIMO.Word/WordSection.PrivateMethods.cs index c725cc9a..71330719 100644 --- a/OfficeIMO.Word/WordSection.PrivateMethods.cs +++ b/OfficeIMO.Word/WordSection.PrivateMethods.cs @@ -254,7 +254,7 @@ private List GetParagraphsList() { if (element is Paragraph) { Paragraph paragraph = (Paragraph)element; if (paragraph.ParagraphProperties != null && paragraph.ParagraphProperties.SectionProperties != null) { - if (paragraph.ParagraphProperties.SectionProperties == _sectionProperties) { + if (AreSectionPropertiesEqual(paragraph.ParagraphProperties.SectionProperties, _sectionProperties)) { foundCount = count; } @@ -268,7 +268,7 @@ private List GetParagraphsList() { if (foundCount < 0) { var sectionProperties = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType().FirstOrDefault(); - if (sectionProperties == _sectionProperties) { + if (AreSectionPropertiesEqual(sectionProperties, _sectionProperties)) { foundCount = count; } } @@ -329,7 +329,7 @@ private List GetTablesList() { if (element is Paragraph) { Paragraph paragraph = (Paragraph)element; if (paragraph.ParagraphProperties != null && paragraph.ParagraphProperties.SectionProperties != null) { - if (paragraph.ParagraphProperties.SectionProperties == _sectionProperties) { + if (AreSectionPropertiesEqual(paragraph.ParagraphProperties.SectionProperties, _sectionProperties)) { foundCount = count; } @@ -342,9 +342,11 @@ private List GetTablesList() { } } - var sectionProperties = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType().FirstOrDefault(); - if (sectionProperties == _sectionProperties) { - foundCount = count; + if (foundCount < 0) { + var sectionProperties = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType().FirstOrDefault(); + if (AreSectionPropertiesEqual(sectionProperties, _sectionProperties)) { + foundCount = count; + } } } } @@ -369,10 +371,9 @@ private List GetEmbeddedDocumentsList() { if (element is Paragraph) { Paragraph paragraph = (Paragraph)element; if (paragraph.ParagraphProperties != null && paragraph.ParagraphProperties.SectionProperties != null) { - if (paragraph.ParagraphProperties.SectionProperties == _sectionProperties) { + if (AreSectionPropertiesEqual(paragraph.ParagraphProperties.SectionProperties, _sectionProperties)) { foundCount = count; } - count++; dataSections[count] = new List(); } @@ -382,16 +383,32 @@ private List GetEmbeddedDocumentsList() { } } - var sectionProperties = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType().FirstOrDefault(); - if (sectionProperties == _sectionProperties) { - foundCount = count; + if (foundCount < 0) { + var sectionProperties = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType().FirstOrDefault(); + if (AreSectionPropertiesEqual(sectionProperties, _sectionProperties)) { + foundCount = count; + } } } } - return dataSections[foundCount]; } + /// + /// Checks if two SectionProperties are equal + /// + /// + /// + /// + private bool AreSectionPropertiesEqual(SectionProperties sp1, SectionProperties sp2) { + if (sp1 == null || sp2 == null) { + return sp1 == sp2; + } + + // Compare the XML representation of the SectionProperties + return sp1.OuterXml == sp2.OuterXml; + } + /// /// Gets list of word elements in given section /// @@ -410,7 +427,7 @@ private List GetWordElements() { // converts Paragraph to WordParagraph Paragraph paragraph = (Paragraph)element; if (paragraph.ParagraphProperties != null && paragraph.ParagraphProperties.SectionProperties != null) { - if (paragraph.ParagraphProperties.SectionProperties == _sectionProperties) { + if (AreSectionPropertiesEqual(paragraph.ParagraphProperties.SectionProperties, _sectionProperties)) { foundCount = count; } @@ -430,12 +447,17 @@ private List GetWordElements() { // converts Table to WordTable WordTable wordTable = new WordTable(_document, (Table)element); dataSections[count].Add(wordTable); + } else { + // TODO: Implement other types + Debug.WriteLine("Missing type? " + element.GetType().Name + ". Maybe lets add it?"); } } - var sectionProperties = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType().FirstOrDefault(); - if (sectionProperties == _sectionProperties) { - foundCount = count; + if (foundCount < 0) { + var sectionProperties = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType().FirstOrDefault(); + if (AreSectionPropertiesEqual(sectionProperties, _sectionProperties)) { + foundCount = count; + } } } } @@ -508,7 +530,7 @@ private List GetSdtBlockList() { if (element is Paragraph) { Paragraph paragraph = (Paragraph)element; if (paragraph.ParagraphProperties != null && paragraph.ParagraphProperties.SectionProperties != null) { - if (paragraph.ParagraphProperties.SectionProperties == _sectionProperties) { + if (AreSectionPropertiesEqual(paragraph.ParagraphProperties.SectionProperties, _sectionProperties)) { foundCount = count; } @@ -520,9 +542,11 @@ private List GetSdtBlockList() { } } - var sectionProperties = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType().FirstOrDefault(); - if (sectionProperties == _sectionProperties) { - foundCount = count; + if (foundCount < 0) { + var sectionProperties = _wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType().FirstOrDefault(); + if (AreSectionPropertiesEqual(sectionProperties, _sectionProperties)) { + foundCount = count; + } } } } diff --git a/OfficeIMO.Word/WordSection.PublicMethods.cs b/OfficeIMO.Word/WordSection.PublicMethods.cs index 1d682433..1aaf856c 100644 --- a/OfficeIMO.Word/WordSection.PublicMethods.cs +++ b/OfficeIMO.Word/WordSection.PublicMethods.cs @@ -57,8 +57,9 @@ public WordSection SetBorders(WordBorder wordBorder) { return this; } - public WordParagraph AddHorizontalLine(BorderValues lineType = BorderValues.Single, SixLabors.ImageSharp.Color? color = null, uint size = 12, uint space = 1) { - return this.AddParagraph("").AddHorizontalLine(lineType, color, size, space); + public WordParagraph AddHorizontalLine(BorderValues? lineType = null, SixLabors.ImageSharp.Color? color = null, uint size = 12, uint space = 1) { + lineType ??= BorderValues.Single; + return this.AddParagraph("").AddHorizontalLine(lineType.Value, color, size, space); } public WordParagraph AddHyperLink(string text, Uri uri, bool addStyle = false, string tooltip = "", bool history = true) { diff --git a/OfficeIMO.Word/WordSection.cs b/OfficeIMO.Word/WordSection.cs index e1c9a945..042d3e26 100644 --- a/OfficeIMO.Word/WordSection.cs +++ b/OfficeIMO.Word/WordSection.cs @@ -340,7 +340,7 @@ internal WordSection(WordDocument wordDocument, SectionProperties sectionPropert this._wordprocessingDocument = wordDocument._wordprocessingDocument; this._paragraph = paragraph; if (sectionProperties != null) { - this._sectionProperties = sectionProperties; + this._sectionProperties = sectionProperties.MakeSureSectionIsValid(); } else { sectionProperties = wordDocument._wordprocessingDocument.MainDocumentPart.Document.Body.ChildElements.OfType().FirstOrDefault(); if (sectionProperties == null) { diff --git a/OfficeIMO.Word/WordTabStop.cs b/OfficeIMO.Word/WordTabStop.cs index 2504b7d0..b151a481 100644 --- a/OfficeIMO.Word/WordTabStop.cs +++ b/OfficeIMO.Word/WordTabStop.cs @@ -54,7 +54,9 @@ public WordTabStop(WordParagraph wordParagraph, TabStop tab) { _tabStop = tab; } - internal WordTabStop AddTab(int position, TabStopValues alignment = TabStopValues.Left, TabStopLeaderCharValues leader = TabStopLeaderCharValues.None) { + internal WordTabStop AddTab(int position, TabStopValues? alignment = null, TabStopLeaderCharValues? leader = null) { + alignment ??= TabStopValues.Left; + leader ??= TabStopLeaderCharValues.None; TabStop tabStop1 = new TabStop() { Val = alignment, Leader = leader, Position = position }; _tabs.Append(tabStop1); _tabStop = tabStop1; diff --git a/OfficeIMO.Word/WordTextBox.cs b/OfficeIMO.Word/WordTextBox.cs index e3ec86ca..528f5662 100644 --- a/OfficeIMO.Word/WordTextBox.cs +++ b/OfficeIMO.Word/WordTextBox.cs @@ -124,16 +124,16 @@ public WrapTextImage? WrapText { /// /// Gets or sets the horizontal alignment of the text box /// - public DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues HorizontalAlignment { + public WordHorizontalAlignmentValues HorizontalAlignment { get { var anchor = _anchor; if (anchor != null) { var horizontalPosition = anchor.HorizontalPosition; if (horizontalPosition != null && horizontalPosition.HorizontalAlignment != null) { - return GetHorizontalAlignmentFromText(horizontalPosition.HorizontalAlignment.Text); + return HorizontalAlignmentHelper.FromString(horizontalPosition.HorizontalAlignment.Text); } } - return DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues.Center; + return WordHorizontalAlignmentValues.Center; } set { var anchor = _anchor; @@ -144,10 +144,10 @@ public DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues H } if (horizontalPosition.HorizontalAlignment == null) { horizontalPosition.HorizontalAlignment = new HorizontalAlignment() { - Text = value.ToString().ToLower() + Text = HorizontalAlignmentHelper.ToString(value) }; } else { - horizontalPosition.HorizontalAlignment.Text = value.ToString().ToLower(); + horizontalPosition.HorizontalAlignment.Text = HorizontalAlignmentHelper.ToString(value); } } } @@ -232,7 +232,6 @@ public int? HorizontalPositionOffset { /// /// Allows to set horizontally position of the text box in centimeters - /// Please /// public double? HorizontalPositionOffsetCentimeters { get { @@ -947,27 +946,6 @@ public TextBoxContent GenerateTextBoxContent(string text) { return textBoxContent1; } - /// - /// Helps to translate text to HorizontalAlignment - /// - /// - /// - private DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues GetHorizontalAlignmentFromText(string text) { - switch (text.ToLower()) { - case "left": - return DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues.Left; - case "right": - return DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues.Right; - case "center": - return DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues.Center; - case "outside": - return DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues.Outside; - default: - return DocumentFormat.OpenXml.Drawing.Wordprocessing.HorizontalAlignmentValues.Center; - } - } - - private ShapeProperties GenerateShapeProperties() { ShapeProperties shapeProperties1 = new ShapeProperties() { BlackWhiteMode = DocumentFormat.OpenXml.Drawing.BlackWhiteModeValues.Auto }; diff --git a/OfficeIMO.Word/WordWatermark.cs b/OfficeIMO.Word/WordWatermark.cs index 7cc103cf..e47ad95b 100644 --- a/OfficeIMO.Word/WordWatermark.cs +++ b/OfficeIMO.Word/WordWatermark.cs @@ -27,7 +27,7 @@ public class WordWatermark : WordElement { private SdtBlock _sdtBlock; private WordHeader _wordHeader; private WordSection _section; - private WordParagraph _wordParagraph; + //private WordParagraph _wordParagraph; public string Text { get { diff --git a/OfficeImo.sln.DotSettings b/OfficeImo.sln.DotSettings index dd60199c..47d1a421 100644 --- a/OfficeImo.sln.DotSettings +++ b/OfficeImo.sln.DotSettings @@ -1,2 +1,3 @@  + True True \ No newline at end of file