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

Monday, 31 August 2020 06:51

Compare Two Word Documents in Java

In this article, we'll introduce how to compare two Word documents and find the differences between them by using Spire.Doc for Java.

The input Word documents look as follows:

Compare Two Word Documents in Java

import com.spire.doc.Document;

public class CompareWordDocuments {
    public static void main(String[] args) throws Exception {
        //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, "E-iceblue");

        //Save the result to file
        doc1.saveToFile("Result.docx");
    }
}

Output:

Compare Two Word Documents in Java

Thursday, 27 August 2020 07:27

Convert PDF to Excel in Java

This article will show you how to convert PDF to Excel in Java by using Spire.PDF for Java.

Below is the screenshot of the input PDF file:

Convert PDF to Excel in Java

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class ToXLS {
    public static void main(String[] args) {
        //Create a PdfDocument object
        PdfDocument pdf = new PdfDocument();
        //Load a sample PDF file
        pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pdf");
        //Save to Excel
        pdf.saveToFile("output/ToXLS.xlsx", FileFormat.XLSX);
    }
}

Output

Convert PDF to Excel in Java

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

This article demonstrates how to search and highlight specific text in a PowerPoint document using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.IAutoShape;
import com.spire.presentation.Presentation;
import com.spire.presentation.TextHighLightingOptions;

import java.awt.*;

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

        //Get the first shape on the first slide
        IAutoShape shape = (IAutoShape)presentation.getSlides().get(0).getShapes().get(0);

        //Set highlight options
        TextHighLightingOptions options = new TextHighLightingOptions();
        options.setWholeWordsOnly(true);
        options.setCaseSensitive(true);

        //Highlight text
        shape.getTextFrame().highLightText("Spire", Color.yellow, options);

        //Save the result document
        presentation.saveToFile("HighlightSpecifiedText.pptx", FileFormat.PPTX_2013);
    }
}

Output:

Search and Highlight Specific Text in PowerPoint in Java

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);
    }
}

This article will demonstrate how to add an image to PDF file by using Spire.PDF for Java in Java applications. When we use page.getCanvas().drawImage() method to draw image to the PDF file, we could set the size and position of the image.

import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfImage;


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

        //Create a Pdf document
        PdfDocument pdf = new PdfDocument();

        //Add one page to the pdf document
        PdfPageBase page = pdf.getPages().add();

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

        //Set the width and height of image
        float width = image.getWidth() * 0.15f;
        float height = image.getHeight() * 0.15f;

        //Define a position to draw image
        double x = (page.getCanvas().getClientSize().getWidth() - width) / 2;
        float y = 60f;

        //Draw image on page canvas
        page.getCanvas().drawImage(image, x, y, width, height);

        String result = "output/DrawImage.pdf";

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

Effective screenshot after adding image to PDF:

How to add an image to PDF in Java applications

Monday, 17 August 2020 07:46

Compare Two Word Documents in C#, VB.NET

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:

Compare Two Word Documents in C#, VB.NET

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

Compare Two Word Documents in C#, VB.NET

Thursday, 13 August 2020 07:49

Change Hyperlink Appearance in Word in Java

This article demonstrates how to change the appearance of hyperlinks in a Word document using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.HyperlinkType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.UnderlineStyle;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class ChangeHyperlinkAppearance {

    public static void main(String[] args) {

        //Create a Document object
        Document document = new Document();

        //Add a section
        Section section = document.addSection();

        //Add a paragraph
        Paragraph para= section.addParagraph();
        para.appendText("Regular Link: ");

        //Insert a hyperlink
        TextRange txtRange = para.appendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.Web_Link);
        txtRange.getCharacterFormat().setFontName("Times New Roman");
        txtRange.getCharacterFormat().setFontSize(12f);
        para.appendBreak(BreakType.Line_Break);

        //Add a paragraph
        para = section.addParagraph();
        para.appendText("Change Color: ");

        //Insert a hyperlink and set its color to red
        txtRange = para.appendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.Web_Link);
        txtRange.getCharacterFormat().setFontName("Times New Roman");
        txtRange.getCharacterFormat().setFontSize(12f);
        txtRange.getCharacterFormat().setTextColor(Color.RED);
        para.appendBreak(BreakType.Line_Break);

        //Add a paragraph
        para = section.addParagraph();
        para.appendText("Remove Underline: ");

        //Insert a hyperlink and remove its underline
        txtRange = para.appendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.Web_Link);
        txtRange.getCharacterFormat().setFontName("Times New Roman");
        txtRange.getCharacterFormat().setFontSize(12f);
        txtRange.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None);

        //Save the document
        document.saveToFile("output/ChangeHyperlinkAppearance.docx", FileFormat.Docx_2013);
    }
}

Change Hyperlink Appearance in Word in Java