
Document Operation (23)
Get the differences by comparing two Word documents in C#/VB.NET
Written by support iceblueWe have introduced how to compare two Word documents in C# and VB.NET. From Spire.Doc V8.12.14, it supports to get the differences between two Word documents in a structure list. This article will show you how to use Spire.Doc to get the differences by comparing two Word documents.
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using Spire.Doc.Formatting.Revisions; using System; namespace GetWordDifferences { class Program { static void Main(string[] args) { //Load the first Word document Document doc1 = new Document(); doc1.LoadFromFile("Sample1.docx"); //Load the second Word document Document doc2 = new Document(); doc2.LoadFromFile("Sample2.docx"); //Compare the two Word documents doc1.Compare(doc2, "Author"); foreach (Section sec in doc1.Sections) { foreach (DocumentObject docItem in sec.Body.ChildObjects) { if (docItem is Paragraph) { Paragraph para = docItem as Paragraph; if (para.IsInsertRevision) { EditRevision insRevison = para.InsertRevision; EditRevisionType insType = insRevison.Type; string insAuthor = insRevison.Author; DateTime insDateTime = insRevison.DateTime; } else if (para.IsDeleteRevision) { EditRevision delRevison = para.DeleteRevision; EditRevisionType delType = delRevison.Type; string delAuthor = delRevison.Author; DateTime delDateTime = delRevison.DateTime; } foreach (ParagraphBase paraItem in para.ChildObjects) { if (paraItem.IsInsertRevision) { EditRevision insRevison = paraItem.InsertRevision; EditRevisionType insType = insRevison.Type; string insAuthor = insRevison.Author; DateTime insDateTime = insRevison.DateTime; } else if (paraItem.IsDeleteRevision) { EditRevision delRevison = paraItem.DeleteRevision; EditRevisionType delType = delRevison.Type; string delAuthor = delRevison.Author; DateTime delDateTime = delRevison.DateTime; } } } } } //Get the difference about revisions DifferRevisions differRevisions = new DifferRevisions(doc1); var insetRevisionsList = differRevisions.InsertRevisions; var deletRevisionsList = differRevisions.DeleteRevisions; } } }
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports Spire.Doc.Formatting.Revisions Imports System Namespace GetWordDifferences Class Program Private Shared Sub Main(ByVal args() As String) 'Load the first Word document Dim doc1 As Document = New Document doc1.LoadFromFile("Sample1.docx") 'Load the second Word document Dim doc2 As Document = New Document doc2.LoadFromFile("Sample2.docx") 'Compare the two Word documents doc1.Compare(doc2, "Author") For Each sec As Section In doc1.Sections For Each docItem As DocumentObject In sec.Body.ChildObjects If (TypeOf docItem Is Paragraph) Then Dim para As Paragraph = CType(docItem,Paragraph) If para.IsInsertRevision Then Dim insRevison As EditRevision = para.InsertRevision Dim insType As EditRevisionType = insRevison.Type Dim insAuthor As String = insRevison.Author Dim insDateTime As DateTime = insRevison.DateTime ElseIf para.IsDeleteRevision Then Dim delRevison As EditRevision = para.DeleteRevision Dim delType As EditRevisionType = delRevison.Type Dim delAuthor As String = delRevison.Author Dim delDateTime As DateTime = delRevison.DateTime End If For Each paraItem As ParagraphBase In para.ChildObjects If paraItem.IsInsertRevision Then Dim insRevison As EditRevision = paraItem.InsertRevision Dim insType As EditRevisionType = insRevison.Type Dim insAuthor As String = insRevison.Author Dim insDateTime As DateTime = insRevison.DateTime ElseIf paraItem.IsDeleteRevision Then Dim delRevison As EditRevision = paraItem.DeleteRevision Dim delType As EditRevisionType = delRevison.Type Dim delAuthor As String = delRevison.Author Dim delDateTime As DateTime = delRevison.DateTime End If Next End If Next Next 'Get the difference about revisions Dim differRevisions As DifferRevisions = New DifferRevisions(doc1) Dim insetRevisionsList = differRevisions.InsertRevisions Dim deletRevisionsList = differRevisions.DeleteRevisions End Sub End Class End Namespace
Spire.Doc for .NET supports comparing two Word documents and find the difference between them. In this article, you will learn how to use Spire.Doc for .NET to compare two Word documents in C# and VB.NET.
The input Word documents:
using Spire.Doc; namespace CompareWordDocuments { class Program { static void Main(string[] args) { //Create a Document instance Document doc1 = new Document(); //Load the first Word document doc1.LoadFromFile("Doc1.docx"); //Create a Document instance Document doc2 = new Document(); //Load the second Word document doc2.LoadFromFile("Doc2.docx"); //Compare the two Word documents doc1.Compare(doc2, "Shawn"); //Save the result to file doc1.SaveToFile("Result.docx"); doc1.Dispose(); } } }
Imports Spire.Doc Namespace CompareWordDocuments Class Program Private Shared Sub Main(ByVal args As String()) ‘Create a Document instance Dim doc1 As Document = New Document() ‘Load the first Word document doc1.LoadFromFile("Doc1.docx") ‘Create a Document instance Dim doc2 As Document = New Document() ‘Load the second Word document doc2.LoadFromFile("Doc2.docx") ‘Compare the two Word documents doc1.Compare(doc2, "Shawn") ‘Save the result to file doc1.SaveToFile("Result.docx") doc1.Dispose() End Sub End Class End Namespace
Output:
C# insert math equation and symbol to Word document
Written by support iceblueStarts from version 7.6.5, Spire.Doc supports to add Latex math code to Word document in C#. This article will show you how to add Latex Math Symbols and equation to word document.
using Spire.Doc; using Spire.Doc.Documents; using System; namespace InsertMath; class Program { static void Main(string[] args) { //create a word document Document doc = new Document(); //add a section Section section = doc.AddSection(); //add a paragraph to the section Paragraph paragraph = section.AddParagraph(); //add a LatexMathcode to the first paragraph OfficeMath officeMath = new OfficeMath(doc); paragraph.Items.Add(officeMath); officeMath.FromLatexMathCode("x^{2}+\\sqrt{x^{2}+1}=2"); //add equation to the second paragraph Paragraph paragraph2 = section.AddParagraph(); OfficeMath officeMath1 = new OfficeMath(doc); paragraph2.Items.Add(officeMath1); officeMath1.FromLatexMathCode("\\forall x \\in X, \\quad \\exists y \\leq \\epsilon"); //add symbols to the third paragraph Paragraph paragraph3 = section.AddParagraph(); OfficeMath officeMath2 = new OfficeMath(doc); paragraph3.Items.Add(officeMath2); officeMath2.FromLatexMathCode(" \\alpha,\\beta, \\gamma, \\Gamma, \\pi, \\Pi, \\phi, \\varphi, \\mu, \\Phi"); //save the document to file doc.SaveToFile("Equation.docx", FileFormat.Docx); System.Diagnostics.Process.Start("Equation.docx"); } } }
Effective screenshot after adding latex math code and symbols to the word document:
Add checkbox and picture content control to word document in C#
Written by support iceblueBesides the Combo Box, Text, Date Picker and Drop-Down List content controls, Checkbox and picture content control also are the mostly used content control in word document. Spire.Doc supports to add many kinds of content controls to the word document. This article will show you how to add checkbox and picture content control to word document by Spire.Doc for .NET.
Code snippets of how to add checkbox and picture content control:
using System; using System.Drawing; namespace AddCheckbox { class Program { static void Main(string[] args) { //Create a new word document Document document = new Document(); //Add a section to the document Section section = document.AddSection(); //Add a document to the section Paragraph paragraph = section.AddParagraph(); //Add checkbox content control StructureDocumentTagInline sdt = new StructureDocumentTagInline(document); paragraph = section.AddParagraph(); sdt = new StructureDocumentTagInline(document); sdt.CharacterFormat.FontSize = 20; paragraph.ChildObjects.Add(sdt); sdt.SDTProperties.SDTType = SdtType.CheckBox; SdtCheckBox scb = new SdtCheckBox(); sdt.SDTProperties.ControlProperties = scb; TextRange tr = new TextRange(document); tr.CharacterFormat.FontName = "MS Gothic"; tr.CharacterFormat.FontSize = 20; sdt.ChildObjects.Add(tr); scb.Checked = true; sdt.SDTProperties.Alias = "CheckoBox"; sdt.SDTProperties.Tag = "Checkbox"; //Add picture content control paragraph = section.AddParagraph(); sdt = new StructureDocumentTagInline(document); paragraph.ChildObjects.Add(sdt); sdt.SDTProperties.ControlProperties = new SdtPicture(); sdt.SDTProperties.Alias = "Picture"; sdt.SDTProperties.Tag = "Picture"; DocPicture pic = new DocPicture(document) { Width = 10, Height = 10 }; pic.LoadImage(Image.FromFile("Logo.jpg")); sdt.SDTContent.ChildObjects.Add(pic); document.SaveToFile("Sample.docx", FileFormat.Docx2013); } } }
Effective screenshot after adding checkbox and picture content control to word document:
Detect and Remove VBA Macros from Word Document in C#
Written by support iceblueWith Spire.Doc, developers can quickly detect if a Word document contains VBA macros and remove all of the VBA macros from a word document. This article is going to show you the detail steps of how to detect and remove VBA macros from word document using Spire.Doc.
Detail steps:
Step 1: Initialize a Document object and load the Word document.
Document document = new Document(); document.LoadFromFile("Input.docm");
Step 2: If the document contains Macros, remove them from the document.
if (document.IsContainMacro) { document.ClearMacros(); }
Step 3: Save the document.
document.SaveToFile("Output.docm", FileFormat.Docm);
Full code:
using Spire.Doc; namespace Remove_Macros_from_Word { class Program { static void Main(string[] args) { //Initialize a Document object Document document = new Document(); //Load the Word document document.LoadFromFile("Input.docm"); //If the document contains macros, remove them from the document if (document.IsContainMacro) { document.ClearMacros(); } //Save the document document.SaveToFile("Output.docm", FileFormat.Docm); } } }
With Spire.Doc, we can copy the content from one word document to another word document in C#. When we need to generate a large amount of documents from a single document, clone the document will be much easier. The clone method speeds up the generation of the word documents and developers only need one single line of code to get the copy of the word document.
Now we will show the code snippet of how to clone a word document in C#.
Step 1: Create a new instance of Document and load the document from file.
Document doc = new Document(); doc.LoadFromFile("Sample.docx",FileFormat.Docx2010);
Step 2: Clone the word document.
doc.Clone();
Step 3: Save the document to file.
doc.SaveToFile("Cloneword.docx", FileFormat.Docx2010);
Effective screenshot of clone the word document:
Full codes of clone a word document:
using Spire.Doc; namespace CloneWord { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile("Sample.docx", FileFormat.Docx2010); doc.Clone(); doc.SaveToFile("Cloneword.docx", FileFormat.Docx2010); } } }
Add, Count, Retrieve and Remove Variables in a Word document Using C#
Written by support iceblueDocument variables are used to preserve macro settings in between macro sessions. Spire.Doc allows adding variables, counting the number of variables, retrieving the name and value of variables, and removing specific variables in a Word document.
Add a Variable
Use the Add method to add a variable to a document. The following example adds a document variable named "A1" with a value of 12 to the document.
using Spire.Doc; using Spire.Doc.Documents; namespace ADDVARIABLE { class Program { static void Main(string[] args) { //Instantiate a document object Document document = new Document(); //Add a section Section section = document.AddSection(); //Add a paragraph Paragraph paragraph = section.AddParagraph(); //Add a DocVariable Filed paragraph.AppendField("A1", FieldType.FieldDocVariable); //Add a document variable to the DocVariable Filed document.Variables.Add("A1", "12"); //Update fields document.IsUpdateFields = true; //Save and close the document object document.SaveToFile("AddVariable.docx", FileFormat.Docx2013); document.Close(); } } }
Count the number of Variables
Use the Count property to return the number of variables in a document.
//Load the document Document document = new Document("AddVariable.docx"); //Get the number of variables in the document int number = document.Variables.Count; Console.WriteLine(number);
Retrieve Name and Value of a Variable
Use the GetNameByIndex and GetValueByIndex methods to retrieve the name and value of the variable by index, and the Variables[String Name] to retrieve or set the value of the variable by name.
using Spire.Doc; using Spire.Doc.Documents; using System; namespace COUNTVARIABLE { class Program { static void Main(string[] args) { //Load the document Document document = new Document("AddVariable.docx"); //Get the number of variables in the document int number = document.Variables.Count; Console.WriteLine(number); } } }
Remove a specific Variable
Use the Remove method to remove the variable from the document.
using Spire.Doc; using System; namespace RETRIEVEVARIABLE { class Program { static void Main(string[] args) { //Load the document Document document = new Document("AddVariable.docx"); // Retrieve name of the variable by index string s1 = document.Variables.GetNameByIndex(0); // Retrieve value of the variable by index string s2 = document.Variables.GetValueByIndex(0); // Retrieve or set value of the variable by name string s3 = document.Variables["A1"]; Console.WriteLine("{0} {1} {2}", s1, s2, s3); } } }
How to enable track changes of the word document
Written by support iceblueThe track changes has been used to keep track of the every changes that made to the Word document. It helps to record every edit, insertion, deletion, or modification in a word document. We have demonstrated how to accept/reject the tracked changes on word document in C#. This article will show you how to enable track changes of the document.
Step 1: Create a new word document and load the document from file.
Document document = new Document(); document.LoadFromFile("Sample.docx", FileFormat.Docx2010);
Step 2: Enable the track changes.
document.TrackChanges = true;
Step 3: Save the document to file.
document.SaveToFile("Enable Trackchanges.docx", FileFormat.Docx2010);
Effective screenshot:
Full codes:
using Spire.Doc; namespace EnableTrack { class Program { static void Main(string[] args) { Document document = new Document(); document.LoadFromFile("Sample.docx", FileFormat.Docx2010); document.TrackChanges = true; document.SaveToFile("Enable Trackchanges.docx", FileFormat.Docx2010); } } }
How to accept/reject the tracked changes on word document in C#
Written by support iceblueWhen we operate the word documents by edit the wording, and its format on the word documents, we usually use Microsoft Word's Tracking Changes feature to mark our every changes. Later if we want to share the final version of our document to others, we need to remove the tracked changes by accepting them or rejecting them. Spire.Doc offers a property Document.TrackChanges to turn on/off track changes and the method of Document.AcceptChanges();/Document.RejectChanges(); to accept/reject the track changes. This article will show you how to accept/reject the tracked changes on word document in C# with the help of Spire.Doc.
Firstly view the sample word document with tracked changes:
Here comes to the steps of how to accept or reject the tracked changes:
Step 1: Create a new word document and load the document from file.
Document doc = new Document(); doc.LoadFromFile("sample.docx");
Step 2: Get the first section and the paragraph we want to accept/reject the changes.
Section sec = doc.Sections[0]; Paragraph para = sec.Paragraphs[0];
Step 3: Accept the changes or reject the changes.
para.Document.AcceptChanges(); //para.Document.RejectChanges();
Step 4: Save the document to file.
doc.SaveToFile("result.docx", FileFormat.Docx);
Effective screenshot after accept the tracked changes:
Full codes:
using Spire.Doc; namespace ACCEPTCHANGE { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile("sample.docx"); Section sec = doc.Sections[0]; Spire.Doc.Documents.Paragraph para = sec.Paragraphs[0]; para.Document.AcceptChanges(); //para.Document.RejectChanges(); doc.SaveToFile("result.docx", FileFormat.Docx); } } }
How to split a word document by page break in C#
Written by support iceblueWith the help of Spire.Doc for .NET, not only can we split a word document by section, but also can split it by page break. We've already introduced how to split a Word document into multiple documents by section break. In this article, we'll learn how to split a word document by page break with Spire.Doc for .NET.
Please view the following screenshot of the original word document which has two page breaks at the end of the first and the second page.
Now refer to the following detail steps to split it into 3 separate documents by page breaks.
Step 1: Create a word document and load the original word document.
Document original = new Document(); original.LoadFromFile("New Zealand.docx");
Step 2: Create a new word document and add a section to it.
Document newWord = new Document(); Section section = newWord.AddSection();
Step 3: Split the original word document into separate documents according to page break.
int index = 0; //traverse through all sections of original document foreach (Section sec in original.Sections) { //traverse through all body child objects of each section foreach (DocumentObject obj in sec.Body.ChildObjects) { if (obj is Paragraph) { Paragraph para = obj as Paragraph; //add paragraph object in original section into section of new document section.Body.ChildObjects.Add(para.Clone()); foreach (DocumentObject parobj in para.ChildObjects) { if (parobj is Break && (parobj as Break).BreakType == BreakType.PageBreak) { //get the index of page break in paragraph int i = para.ChildObjects.IndexOf(parobj); //remove the page break from its paragraph section.Body.LastParagraph.ChildObjects.RemoveAt(i); //save the new document to a .docx file. newWord.SaveToFile(String.Format("result/out-{0}.docx", index), FileFormat.Docx); index++; //create a new document newWord = new Document(); //add a section for document section = newWord.AddSection(); //add paragraph object in original section into section of new document section.Body.ChildObjects.Add(para.Clone()); if (section.Paragraphs[0].ChildObjects.Count == 0) { //remove the first blank paragraph section.Body.ChildObjects.RemoveAt(0); } else { //remove the child objects before the page break while (i >= 0) { section.Paragraphs[0].ChildObjects.RemoveAt(i); i--; } } } } } if (obj is Table) { //add table object in original section into section of new document section.Body.ChildObjects.Add(obj.Clone()); } } } //save to a .docx file newWord.SaveToFile(String.Format("result/out-{0}.docx", index), FileFormat.Docx);
Output:
Full codes:
using System; using Spire.Doc; using Spire.Doc.Documents; namespace Split_Word_Document_by_Page_Break { class Program { static void Main(string[] args) { Document original = new Document(); original.LoadFromFile("New Zealand.docx"); Document newWord = new Document(); Section section = newWord.AddSection(); int index = 0; foreach (Section sec in original.Sections) { foreach (DocumentObject obj in sec.Body.ChildObjects) { if (obj is Paragraph) { Paragraph para = obj as Paragraph; section.Body.ChildObjects.Add(para.Clone()); foreach (DocumentObject parobj in para.ChildObjects) { if (parobj is Break && (parobj as Break).BreakType == BreakType.PageBreak) { int i = para.ChildObjects.IndexOf(parobj); section.Body.LastParagraph.ChildObjects.RemoveAt(i); newWord.SaveToFile(String.Format("result/out-{0}.docx", index), FileFormat.Docx); index++; newWord = new Document(); section = newWord.AddSection(); section.Body.ChildObjects.Add(para.Clone()); if (section.Paragraphs[0].ChildObjects.Count == 0) { section.Body.ChildObjects.RemoveAt(0); } else { while (i >= 0) { section.Paragraphs[0].ChildObjects.RemoveAt(i); i--; } } } } } if (obj is Table) { section.Body.ChildObjects.Add(obj.Clone()); } } } newWord.SaveToFile(String.Format("result/out-{0}.docx", index), FileFormat.Docx); } } }