Java: Compare Two Word Documents

2022-07-27 06:51:00 Written by Koohji

Document comparison is the process of checking new versions of a document against previous copies in order to identify changes made by different contributors. These differences may include additions or omissions of words, sentences or paragraphs, and formatting adjustments. This article demonstrates how to compare two Word documents in Java using Spire.Doc for Java.

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

Java: Compare Two Word Documents

Install Spire.Doc for Java

First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>14.4.0</version>
    </dependency>
</dependencies>

Compare Two Documents and Save Result in a Third Word Document

Saving the comparison result in a separate Word document allows users 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 Java.

  • 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.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class CompareDocuments {

    public 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.Docx_2013);
        doc1.dispose();
    }
}

Java: Compare Two Word Documents

Compare Two Documents and Return Insertions and Deletions in Lists

Sometimes, we may only care about 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 using DifferRevisions.getInsertRevisions() method.
  • Get a list of deletions using DifferRevisions.getDeleteRevisions() method.
  • Loop through the elements in the two lists to get the specific insertion and deletion.
  • Java
import com.spire.doc.DifferRevisions;
import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.fields.TextRange;
import com.spire.ms.System.Collections.Generic.List;

public class CompareReturnResultsInLists {

    public 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
        List insertRevisionsList = differRevisions.getInsertRevisions();

        //Return the deletion revisions in a list
        List  deleteRevisionsList = differRevisions.getDeleteRevisions();

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

        //Loop through the insertion revision list
        for (int i = 0; i < insertRevisionsList.size(); i++)
        {
            if (insertRevisionsList.get(i) instanceof TextRange)
            {
                m += 1;
                //Get the specific revision and get its content
                TextRange textRange = (TextRange)insertRevisionsList.get(i) ;
                System.out.println("Insertion #" + m + ":" + textRange.getText());
            }
        }
        System.out.println("============================================");
        //Loop through the deletion revision list
        for (int i = 0; i < deleteRevisionsList.size() ; i++)
        {
            if (deleteRevisionsList.get(i) instanceof TextRange)
            {
                n += 1;
                //Get the specific revision and get its content
                TextRange textRange = (TextRange) deleteRevisionsList.get(i) ;
                System.out.println("Deletion #" + n + ":" + textRange.getText());
            }
        }
    }
}

Java: 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.

Java: Convert PDF to Excel

2022-06-09 07:27:00 Written by Koohji

For security reasons, many financial documents such as invoices are usually saved in PDF format. If you want to perform data analysis and calculation on these documents, you may need to convert them to Excel. In this article, we will introduce how to convert PDF to Excel in Java using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>12.4.4</version>
    </dependency>
</dependencies>

Convert PDF to Excel in Java

The following are the steps to convert a PDF document to Excel:

  • Initialize an instance of PdfDocument class.
  • Load the PDF document using PdfDocument.loadFromFile(String) method.
  • Save the document to Excel using PdfDocument.saveToFile(String, FileFormat) method.
  • Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class ConvertPdfToExcel {
    public static void main(String[] args) {
        //Initialize an instance of PdfDocument class
        PdfDocument pdf = new PdfDocument();
        //Load the PDF document
        pdf.loadFromFile("Sample.pdf");

        //Save the PDF document to XLSX
        pdf.saveToFile("PdfToExcel.xlsx", FileFormat.XLSX);
    }
}

Java: Convert PDF to Excel

This example converts multiple PDF pages to multiple Excel worksheets. If you want to convert a multi-page PDF to a single Excel sheet, please refer to this article: Java: Convert a Multi-Page PDF to One Excel Worksheet.

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.

Java: Highlight Text in PowerPoint

2022-09-29 06:44:00 Written by Koohji

When you want to emphasize a particular point in a PowerPoint presentation, you can highlight it with a bright color to help the audience catch it at first glance. In this article, we will explain how to highlight text in a PowerPoint presentation in Java using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>11.3.5</version>
    </dependency>
</dependencies>

Highlight Text in PowerPoint in Java

The following are the steps to highlight specific text in a PowerPoint document:

  • Initialize an instance of Presentation class.
  • Load a PowerPoint presentation using Presentation.loadFromFile() method.
  • Loop through the slides in the presentation and the shapes on each slide.
  • Check if the current shape is of IAutoShape type.
  • If the result is true, typecast it to IAutoShape.
  • Initialize an instance of TextHighLightingOptions class, and set the text highlighting options such as whole words only and case sensitive using TextHighLightingOptions.setWholeWordsOnly() and TextHighLightingOptions.setCaseSensitive() methods.
  • Highlight a specific text in the shape using IAutoShape.getTextFrame().highLightText() method.
  • Save the result file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

import java.awt.*;

public class HighlightTextInPPT {
    public static void main(String []args) throws Exception {
        //Create an instance of Presentation class
        Presentation presentation = new Presentation();
        //Load a PowerPoint file
        presentation.loadFromFile("Input.pptx");

        //Loop through all slides
        for (int i = 0; i < presentation.getSlides().getCount(); i++)
        {
            //Get the current slide
            ISlide slide = presentation.getSlides().get(i);
            //Loop through the shapes on the slide
            for (int j = 0; j < slide.getShapes().getCount(); j++)
            {
                //Check if the current shape is of IAutoShape type
                if (slide.getShapes().get(j) instanceof IAutoShape)
                {
                    //Typecast the shape to IAutoShape
                    IAutoShape shape = (IAutoShape)slide.getShapes().get(j);

                    //Create an instance of TextHighLightingOptions class
                    TextHighLightingOptions options = new TextHighLightingOptions();
                    //Set text highlighting options
                    options.setCaseSensitive(true);
                    options.setWholeWordsOnly(true);

                    //Highlight specific text within the shape with color
                    shape.getTextFrame().highLightText("Spire", Color.YELLOW, options);
                }
            }
        }

        //Save the result file
        presentation.saveToFile("HighlightText.pptx", FileFormat.PPTX_2013);

    }
}

Java: Highlight Text in PowerPoint

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 40