Sections in PowerPoint is a feature that allows you to organize slides into different groups/segments for easy management. Adding sections with unique names can help keep track of specific groups of slides, or can also help outline the topics of a PowerPoint presentation. In this article, you will learn how to programmatically add or remove sections in a PowerPoint document using Spire.Presentation for .NET.

Install Spire.Presentation for .NET

To begin with, you need to add the DLL files included in the Spire.Presentation 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.Presentation

Add a Section at the End of a PowerPoint Document in C# and VB.NET

Spire.Presentation for .NET provides the Presentation.SectionList.Append(string sectionName) method to append a section with section name at the end of a PowerPoint document. The detailed steps are as follows.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Append a section at the end of the document using Presentation.SectionList.Append(string sectionName) method.
  • Save the result document using Presentation.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Presentation;

namespace AppendSectionAtEnd
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();

            //Load a sample PowerPoint document
            ppt.LoadFromFile("Test.pptx");

            //Add a section at the end of the document
            Section section = ppt.SectionList.Append("End Section");

            //Save the result document
            ppt.SaveToFile("AddSectionAtEnd.pptx", FileFormat.Pptx2013);
        }
    }
}

C#/VB.NET: Add or Remove Sections in PowerPoint

Insert a Section Before a Specified Section in PowerPoint in C# and VB.NET

If you want to insert a section before an existing section to make the document more logical, Spire.Presentation for .NET provides the Presentation.SectionList.Insert(int sectionIndex, string sectionName) method. The following are the steps to insert a section at a specified position by section index.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Insert a new section before the specified section using Presentation.SectionList.Insert(int sectionIndex, string sectionName) method.
  • Save the result document using Presentation.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Presentation;

namespace InsertSectionAtSpecifiedPosition
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();

            //Load a sample PowerPoint document
            ppt.LoadFromFile("Test.pptx");

            //Insert a section before the second section
            Section section = ppt.SectionList.Insert(1, "New Section");

            //Save the result document
            ppt.SaveToFile("InsertSectionAtSpecifiedPosition.pptx", FileFormat.Pptx2013);
        }
    }
}

C#/VB.NET: Add or Remove Sections in PowerPoint

Add a Section Before a Specified Slide in PowerPoint in C# and VB.NET

To divided the existing PowerPoint slides into different sections, you can use the Presentation.SectionList.Add(string sectionName, ISlide slide) method to insert a section before a specified slide. The detailed steps are as follows.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get a specified slide using Presentation.Slides property.
  • Add a section before the specified slide using Presentation.SectionList.Add(string sectionName, ISlide slide) method.
  • Save the result document using Presentation.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Presentation;

namespace AddSectionBeforeSlide
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();

            //Load a sample PowerPoint document
            ppt.LoadFromFile("Test.pptx");

            //Get the second slide in the document
            ISlide slide = ppt.Slides[1];

            //Add a section before the second slide
            Section section = ppt.SectionList.Add("New Section", slide);

            //Save the result document
            ppt.SaveToFile("AddSectionBeforeSlide.pptx", FileFormat.Pptx2013);
        }
    }
}

C#/VB.NET: Add or Remove Sections in PowerPoint

Remove a Section from a PowerPoint Document in C# and VB.NET

If you do not need a particular section, you can simply remove it using Presentation.SectionList.RemoveAt(int index) method. Note that removing a section does not remove the slides in that section. The following are the steps to remove a specified section but keep the slides in it.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Remove a specified section using Presentation.SectionList.RemoveAt(int index) method. Or you can remove all the sections in the document using Presentation.SectionList.RemoveAll() method.
  • Save the result document using Presentation.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Presentation;

namespace RemoveSection
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Presentation instance
            Presentation ppt = new Presentation();

            //Load a sample PowerPoint document
            ppt.LoadFromFile("Test.pptx");

            //Remove the second section
            ppt.SectionList.RemoveAt(1);

            //Remove all the sections
            //ppt.SectionList.RemoveAll();

            //Save the result document
            ppt.SaveToFile("RemoveSection.pptx", FileFormat.Pptx2013);
        }
    }
}

C#/VB.NET: Add or Remove Sections 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.

Wednesday, 02 September 2020 06:44

Set an Expiration Date for PDF Document in Java

This article demonstrates how to set an expiration date for a PDF document using Spire.PDF for Java.

import com.spire.pdf.actions.PdfJavaScriptAction;

public class ExpiryDate {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        //Set expiration date and warning information,and close the document through JavaScript
        String javaScript = "var rightNow = new Date();"
                + "var endDate = new Date('June 20, 2020 23:59:59');"
                + "if(rightNow.getTime() > endDate)"
                + "app.alert('This document is no longer valid, please contact us for a updated one.',1);"
                + "this.closeDoc();";

        //Create a PdfJavaScriptAction object based on the javascript
        PdfJavaScriptAction js = new PdfJavaScriptAction(javaScript);

        //Set PdfJavaScriptAction as the AfterOpenAction
        doc.setAfterOpenAction(js);

        //Save to file
        doc.saveToFile("ExpirationDate.pdf", FileFormat.PDF);
    }
}

Set an Expiration Date for PDF Document in Java

Wednesday, 27 July 2022 06:51

Java: Compare Two Word Documents

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>12.4.1</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.

Thursday, 09 June 2022 07:27

Java: Convert PDF to Excel

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

Tuesday, 25 August 2020 09:15

Verify digital signature in PDF in Java

This article will demonstrate how to use Spire.PDF for Java to verify the digital signature in PDF in Java applications.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfSignature;
import com.spire.pdf.widget.*;

public class VerifySignature {
    public static void main(String[] args) {

        //Load a pdf document
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("Sample.pdf");

        //Get the collection of PDF fields
        PdfFormWidget pdfFormWidget = (PdfFormWidget) doc.getForm();
        PdfFormFieldWidgetCollection pdfFormFieldWidgetCollection = pdfFormWidget.getFieldsWidget();

        //Traverse all the PDF form field
        for (int i = 0; i < pdfFormFieldWidgetCollection.getCount(); i++) {
            //check whether it is PdfSignatureField
            if (pdfFormFieldWidgetCollection.get(i) instanceof PdfSignatureFieldWidget) {
                //get the signature field
                PdfSignatureFieldWidget signatureFieldWidget = (PdfSignatureFieldWidget) pdfFormFieldWidgetCollection.get(i);
                //get the PDF signature
                PdfSignature signature = signatureFieldWidget.getSignature();

                //Verify the signature
                boolean result = signature.verifySignature();
                if (result) {
                    System.out.println("Valid signature");
                } else {
                    System.out.println("Invalid signature");
                }
            }
        }
    }
}

After run the project, we will get the verify signature results:

Verify digital signature in PDF in Java

Thursday, 29 September 2022 06:44

Java: Highlight Text in PowerPoint

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

This article demonstrates how to find the text that matches a specific regular expression in a PDF document using Spire.PDF for Java.

import com.spire.pdf.general.find.PdfTextFind;
import java.awt.*;

public class FindByRegularExpression {

    public static void main(String[] args) throws Exception {

        //Load a PDF document
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.pdf");

        //Create a object of PdfTextFind collection
        PdfTextFind[] results;

        //Loop through the pages
        for (Object page : (Iterable) pdf.getPages()) {
            PdfPageBase pageBase = (PdfPageBase) page;

            //Define a regular expression
            String pattern = "\\#\\w+\\b";

            //Find all results that match the pattern
            results = pageBase.findText(pattern).getFinds();

            //Highlight the search results with yellow
            for (PdfTextFind find : results) {
                find.applyHighLight(Color.yellow);
            }
        }

        //Save to file
        pdf.saveToFile("FindByPattern.pdf");
    }
}

Find Text in PDF by Regular Expression in Java

Friday, 21 August 2020 03:18

Delete Images in Excel in Java

This article demonstrates how to remove a specific image or all images from an Excel worksheet using Spire.XLS for Java.

Delete specific image

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class DeleteSpecificImage {

    public static void main(String[] args) {

        //Create a Workbook object
        Workbook workbook = new Workbook();
        
        //Load an Excel file
        workbook.loadFromFile("Input.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Delete a specific image by its index
        sheet.getPictures().get(1).remove();

        //Save the document
        workbook.saveToFile("DeleteSpecificImage.xlsx", ExcelVersion.Version2013);
    }

Delete all images

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class DeleteAllImages {

    public static void main(String[] args) {

        //Create a Workbook object
        Workbook workbook = new Workbook();

        //Load an Excel file
        workbook.loadFromFile("Input.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Loop through the images inside the worksheet
        for (int i = sheet.getPictures().getCount() - 1; i >= 0; i--) {
            
            //Delete an image by its index
            sheet.getPictures().get(i).remove();
        }

        //Save the document
        workbook.saveToFile("DeleteAllImages.xlsx", ExcelVersion.Version2013);
    }
}

Images play a vital role in various documents. They are helpful in conveying complex information that is difficult to present in plain text and making documents more visually appealing. In this article, we will focus on how to insert, replace or delete images in PDF documents 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>10.4.4</version>
    </dependency>
</dependencies>
    

Insert an Image into a PDF Document in Java

The following steps demonstrate how to insert an image into an existing PDF document:

  • Initialize an instance of the PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Get the desired page in the PDF document using PdfDocument.getPages().get() method.
  • Load an image using PdfImage.fromFile() method.
  • Specify the width and height of the image area on the page.
  • Specify the X and Y coordinates to start drawing the image.
  • Draw the image on the page using PdfPageBase.getCanvas().drawImage() method.
  • Save the result document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;

public class AddImage {
    public static void main(String []args){
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Input.pdf");
        
        //Get the first page in the PDF document
        PdfPageBase page = pdf.getPages().get(0);

        //Load an image
        PdfImage image = PdfImage.fromFile("image.jpg");

        //Specify the width and height of the image area on the page
        float width = image.getWidth() * 0.50f;
        float height = image.getHeight() * 0.50f;

        //Specify the X and Y coordinates to start drawing the image
        float x = 100f;
        float y = 60f;

        //Draw the image at a specified location on the page
        page.getCanvas().drawImage(image, x, y, width, height);

        //Save the result document
        pdf.saveToFile("AddImage.pdf", FileFormat.PDF);
    }
}

Java: Insert, Replace or Delete Images in PDF

Replace an Image with Another Image in a PDF Document in Java

The following steps demonstrate how to replace an image with another image in a PDF document:

  • Initialize an instance of the PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Get the desired page in the PDF document using PdfDocument.getPages().get() method.
  • Load an image using PdfImage.fromFile() method.
  • Replace a specific image on the page with the loaded image using PdfPageBase.replaceImage() method.
  • Save the result document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfImage;

public class ReplaceImage {
    public static void main(String []args){
        //Create a PdfDocument instance
        PdfDocument doc = new PdfDocument();
        //Load a PDF document
        doc.loadFromFile("AddImage.pdf");

        //Get the first page
        PdfPageBase page = doc.getPages().get(0);

        //Load an image
        PdfImage image = PdfImage.fromFile("image1.jpg");

        //Replace the first image on the page with the loaded image
        page.replaceImage(0, image);

        //Save the result document
        doc.saveToFile("ReplaceImage.pdf", FileFormat.PDF);
    }
}

Java: Insert, Replace or Delete Images in PDF

Delete a Specific Image in a PDF Document in Java

The following steps demonstrate how to delete an image from a PDF document:

  • Initialize an instance of the PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Get the desired page in the PDF document using PdfDocument.getPages().get() method.
  • Delete a specific image on the page using PdfPageBase.deleteImage() method.
  • Save the result document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

public class DeleteImage {
    public static void main(String []args){
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load a PDF document
        pdf.loadFromFile("AddImage.pdf");

        //Get the first page
        PdfPageBase page = pdf.getPages().get(0);

        //Delete the first image on the page
        page.deleteImage(0);

        //Save the result document
        pdf.saveToFile("DeleteImage.pdf", FileFormat.PDF);
    }
}

Java: Insert, Replace or Delete Images in PDF

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.

Friday, 22 July 2022 07:46

C#/VB.NET: Compare Two Word Documents

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.