diff --git a/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document.sln b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document.sln new file mode 100644 index 00000000..bd3a084a --- /dev/null +++ b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36930.0 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Set-Proofing-for-Word-document", "Set-Proofing-for-Word-document\Set-Proofing-for-Word-document.csproj", "{A299A71F-FF30-D899-576A-78B13CF37E0C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A299A71F-FF30-D899-576A-78B13CF37E0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A299A71F-FF30-D899-576A-78B13CF37E0C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A299A71F-FF30-D899-576A-78B13CF37E0C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A299A71F-FF30-D899-576A-78B13CF37E0C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F9409610-9407-455F-8D68-5B34EB50D1E0} + EndGlobalSection +EndGlobal diff --git a/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Data/Input.docx b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Data/Input.docx new file mode 100644 index 00000000..5c191704 Binary files /dev/null and b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Data/Input.docx differ diff --git a/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Output/.gitkeep b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Output/.gitkeep new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Output/Result.docx b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Output/Result.docx new file mode 100644 index 00000000..de4c0805 Binary files /dev/null and b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Output/Result.docx differ diff --git a/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Program.cs b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Program.cs new file mode 100644 index 00000000..acb223ff --- /dev/null +++ b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Program.cs @@ -0,0 +1,134 @@ +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; +using Syncfusion.Office; + +namespace Set_Proofing_for_Word_document +{ + class Program + { + static void Main(string[] args) + { + using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + //Opens an existing document from file system through constructor of WordDocument class. + using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic)) + { + foreach (WSection section in document.Sections) + { + //Accesses the Body of section where all the contents in document are apart. + WTextBody sectionBody = section.Body; + IterateTextBody(sectionBody); + WHeadersFooters headersFooters = section.HeadersFooters; + //Consider that OddHeader and OddFooter are applied to this document. + //Iterates through the TextBody of OddHeader and OddFooter. + IterateTextBody(headersFooters.OddHeader); + IterateTextBody(headersFooters.OddFooter); + } + //Creates file stream. + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) + { + //Saves the Word document to file stream. + document.Save(outputFileStream, FormatType.Docx); + } + } + } + } + + /// + /// Iterates textbody child elements. + /// + private static void IterateTextBody(WTextBody textBody) + { + //Iterates through each of the child items of WTextBody. + for (int i = 0; i < textBody.ChildEntities.Count; i++) + { + //IEntity is the basic unit in DocIO DOM. + //Accesses the body items (should be either paragraph, table or block content control) as IEntity. + IEntity bodyItemEntity = textBody.ChildEntities[i]; + //A Text body has 3 types of elements - Paragraph, Table and Block Content Control + //Decides the element type by using EntityType. + switch (bodyItemEntity.EntityType) + { + case EntityType.Paragraph: + WParagraph paragraph = bodyItemEntity as WParagraph; + //Processes the paragraph contents. + //Iterates through the paragraph's DOM. + IterateParagraph(paragraph.Items); + break; + case EntityType.Table: + //Table is a collection of rows and cells. + //Iterates through table's DOM. + IterateTable(bodyItemEntity as WTable); + break; + case EntityType.BlockContentControl: + BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl; + //Iterates to the body items of Block Content Control. + IterateTextBody(blockContentControl.TextBody); + break; + } + } + } + + /// + /// Iterates table child elements. + /// + private static void IterateTable(WTable table) + { + //Iterates the row collection in a table. + foreach (WTableRow row in table.Rows) + { + //Iterates the cell collection in a table row. + foreach (WTableCell cell in row.Cells) + { + //Table cell is derived from (also a) TextBody. + //Reusing the code meant for iterating TextBody. + IterateTextBody(cell); + } + } + } + + /// + /// Iterates paragraph child elements. + /// + private static void IterateParagraph(ParagraphItemCollection paraItems) + { + for (int i = 0; i < paraItems.Count; i++) + { + Entity entity = paraItems[i]; + //A paragraph can have child elements such as text, image, hyperlink, symbols, etc., + //Decides the element type by using EntityType. + switch (entity.EntityType) + { + case EntityType.TextRange: + //Replaces the text with another. + WTextRange textRange = entity as WTextRange; + textRange.CharacterFormat.LocaleIdASCII = (short)LocaleIDs.fr_FR; + break; + case EntityType.Field: + WField field = entity as WField; + field.CharacterFormat.LocaleIdASCII = (short)LocaleIDs.fr_FR; + break; + case EntityType.Picture: + WPicture picture = entity as WPicture; + picture.CharacterFormat.LocaleIdASCII = (short)LocaleIDs.fr_FR; + break; + case EntityType.TextBox: + //Iterates to the body items of textbox. + WTextBox textBox = entity as WTextBox; + IterateTextBody(textBox.TextBoxBody); + break; + case EntityType.Shape: + //Iterates to the body items of shape. + Shape shape = entity as Shape; + IterateTextBody(shape.TextBody); + break; + case EntityType.InlineContentControl: + //Iterates to the paragraph items of inline content control. + InlineContentControl inlineContentControl = entity as InlineContentControl; + IterateParagraph(inlineContentControl.ParagraphItems); + break; + } + } + } + } +} diff --git a/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Set-Proofing-for-Word-document.csproj b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Set-Proofing-for-Word-document.csproj new file mode 100644 index 00000000..8a54e3cf --- /dev/null +++ b/Paragraphs/Set-Proofing-for-Word-document/.NET/Set-Proofing-for-Word-document/Set-Proofing-for-Word-document.csproj @@ -0,0 +1,22 @@ + + + + Exe + net8.0 + Set-Proofing-for-Word-document + enable + enable + + + + + + + + Always + + + Always + + +