News Category

Spire.Doc for .NET

This article shows you how to create a hyperlink to a bookmark within the same Word document by using Spire.Doc with C# and VB.NET.

C#
using Spire.Doc;
using Spire.Doc.Documents;

namespace LinkToBookmark
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Add two sections
            Section section1 = doc.AddSection();
            Section section2 = doc.AddSection();

            //Insert a paragraph in section 2 and add a bookmark named "myBookmark" to it
            Paragraph bookmarkParagrapg = section2.AddParagraph();
            bookmarkParagrapg.AppendText("Here is a bookmark");
            BookmarkStart start = bookmarkParagrapg.AppendBookmarkStart("myBookmark");
            bookmarkParagrapg.Items.Insert(0, start);
            bookmarkParagrapg.AppendBookmarkEnd("myBookmark");

            //Link to the bookmark
            Paragraph paragraph = section1.AddParagraph();
            paragraph.AppendText("Link to a bookmark: ");
            paragraph.AppendHyperlink("myBookmark", "Jump to a location in this document", HyperlinkType.Bookmark);

            //Save to file
            doc.SaveToFile("LinkToBookmark.docx", FileFormat.Docx2013);
        }
    }
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace LinkToBookmark
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Create a Document object
            Document doc = New Document()
 
            'Add two sections
            Dim section1 As Section = doc.AddSection()
            Dim section2 As Section = doc.AddSection()
 
            'Insert a paragraph in section 2 and add a bookmark named "myBookmark" to it
            Dim bookmarkParagrapg As Paragraph = section2.AddParagraph()
            bookmarkParagrapg.AppendText("Here is a bookmark")
            Dim start As BookmarkStart = bookmarkParagrapg.AppendBookmarkStart("myBookmark")
            bookmarkParagrapg.Items.Insert(0, start)
            bookmarkParagrapg.AppendBookmarkEnd("myBookmark")
 
            'Link to the bookmark
            Dim paragraph As Paragraph = section1.AddParagraph()
            paragraph.AppendText("Link to a bookmark: ")
            paragraph.AppendHyperlink("myBookmark", "Jump to a location in this document", HyperlinkType.Bookmark)
 
            'Save to file
            doc.SaveToFile("LinkToBookmark.docx", FileFormat.Docx2013)
        End Sub
    End Class
End Namespace

Link to a Bookmark in a Word Document in C#/VB.NET

We 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.

C#
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;      
        }
    }
 }
VB.NET
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

It is not uncommon at work that we may receive two versions of a Word document and face the need to find the differences between them. Document comparison is particularly important and popular in the fields of laws, regulations and education. In this article, you will learn how to compare two Word documents in C# and VB.NET by using Spire.Doc for .NET.

Below is a screenshot of the two Word documents that’ll be compared.

C#/VB.NET: Compare Two Word Documents

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Compare Two Documents and Save Result in a Third Word Document

Saving the comparison result in a separate Word document allows us to see all the changes made to the original document, including insertions, deletions as well as modifications on formatting. The following are the steps to compare two documents and save the result in a third Word document using Spire.Doc for .NET.

  • Load two Word documents separately while initialing the Document objects.
  • Compare these two documents using Document.Compare() method.
  • Save the result in a third Word document using ;Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace CompareDocuments
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load one Word document
            Document doc1 = new Document("C:\\Users\\Administrator\\Desktop\\original.docx");

            //Load the other Word document
            Document doc2 = new Document("C:\\Users\\Administrator\\Desktop\\revised.docx");

            //Compare two documents
            doc1.Compare(doc2, "John");

            //Save the differences in a third document
            doc1.SaveToFile("Differences.docx", FileFormat.Docx2013);
            doc1.Dispose();
        }
    }
}

C#/VB.NET: Compare Two Word Documents

Compare Two Documents and Return Insertions and Deletions in Lists

Developers may only want to obtain the insertions and deletions instead of the whole differences. The following are the steps to get insertions and deletions in two separate lists.

  • Load two Word documents separately while initialing the Document objects.
  • Compare two documents using Document.Compare() method.
  • Get the revisions using the constructor function of the DifferRevisions ;class.
  • Get a list of insertions through DifferRevisions.InsertRevisions property.
  • Get a list of deletions through DifferRevisions.DeleteRevisions property.
  • Loop through the elements in the two lists to get the specific insertion and deletion.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Fields;
using System;

namespace GetDifferencesInList
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load one Word document
            Document doc1 = new Document("C:\\Users\\Administrator\\Desktop\\original.docx");

            //Load the other Word document
            Document doc2 = new Document("C:\\Users\\Administrator\\Desktop\\revised.docx");

            //Compare the two Word documents
            doc1.Compare(doc2, "Author");

            //Get the revisions
            DifferRevisions differRevisions = new DifferRevisions(doc1);

            //Return the insertion revisions in a list
            var insetRevisionsList = differRevisions.InsertRevisions;

            //Return the deletion revisions in a list
            var deletRevisionsList = differRevisions.DeleteRevisions;

            //Create two int variables
            int m = 0;
            int n = 0;

            //Loop through the insertion revision list 
            for (int i = 0; i < insetRevisionsList.Count; i++)
            {
                if (insetRevisionsList[i] is TextRange)
                {
                    m += 1;
                    //Get the specific revision and get its content
                    TextRange textRange = insetRevisionsList[i] as TextRange;
                    Console.WriteLine("Insertion #" + m + ":" + textRange.Text);
                }
            }
            Console.WriteLine("=====================");

            //Loop through the deletion revision list 
            for (int i = 0; i < deletRevisionsList.Count; i++)
            {
                if (deletRevisionsList[i] is TextRange)
                {
                    n += 1;
                    //Get the specific revision and get its content
                    TextRange textRange = deletRevisionsList[i] as TextRange;
                    Console.WriteLine("Deletion #" + n + ":" + textRange.Text);
                }
            }
            Console.ReadKey();
        }
    }
}

C#/VB.NET: Compare Two Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Page 4 of 56