Java: Digitally Sign a Word Document

2022-07-07 06:01:00 Written by Koohji

A digital signature is a specific type of signature that is backed by a digital certificate, providing proof of your identity. Digital signatures are used to protect documents in a wide range of fields such as life insurance, invoices, rental agreements, and employment contracts and so on. This article will show you how to digitally sign a Word document in Java using Spire.Doc for Java.

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>

Add a Digital Signature to a Word Document in Java

The follows are the steps to digitally sign a Word document using Spire.Doc for Java.

  • Create a Document object.
  • Load a Word document using Document.loadFromFile() method.
  • Specify the path and the password of a PFX certificate.
  • Digitally sign the document while saving the document using Document.saveToFile(string fileName, FileFormat fileFormat, string certificatePath, string securePassword) method. There are some other methods that you can use to digitally sign a Word document.
    • public void saveToFile(string fileName, FileFormat fileFormat, byte[] certificateData, string securePassword);
    • public void saveToStream(Stream stream, FileFormat fileFormat, byte[] certificateData, string securePassword);
    • public void saveToStream(Stream stream, FileFormat fileFormat, string certificatePath, string securePassword);
    • public static byte[] Document.sign(Stream sourceStream, byte[] certificateData, string securePassword);
    • public static byte[] Document.sign(Stream sourceStream, string certificatePath, string securePassword);
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class DigitallySignWordDocument {

    public static void main(String[] args) {

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

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

        //Specify the certificate path
        String certificatePath = "C:\\Users\\Administrator\\Desktop\\certificate.pfx";

        //Specify the password of the certificate
        String password = "e-iceblue";

        //Digitally sign the document while saving it to a .docx file
        doc.saveToFile("AddDigitalSignature.docx", FileFormat.Docx_2013, certificatePath, password);
    }
}

Java: Digitally Sign a Word Document

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 create multi-level category chart in Excel using Spire.XLS for Java.

import com.spire.xls.*;
import com.spire.xls.charts.*;

public class CreateMultiLevelChart {
    public static void main(String []args) throws Exception {
        //create a workbook
        Workbook workbook = new Workbook();
        Worksheet sheet = workbook.getWorksheets().get(0);

        //write data to cells
        sheet.getCellRange("A1").setText( "Main Category");
        sheet.getCellRange("A2").setText("Fruit");
        sheet.getCellRange("A6").setText("Vegies");
        sheet.getCellRange("B1").setText("Sub Category");
        sheet.getCellRange("B2").setText( "Bananas");
        sheet.getCellRange("B3").setText( "Oranges");
        sheet.getCellRange("B4").setText( "Pears");
        sheet.getCellRange("B5").setText("Grapes");
        sheet.getCellRange("B6").setText( "Carrots");
        sheet.getCellRange("B7").setText( "Potatoes");
        sheet.getCellRange("B8").setText( "Celery");
        sheet.getCellRange("B9").setText( "Onions");
        sheet.getCellRange("C1").setText("Value");
        sheet.getCellRange("C2").setValue("52");
        sheet.getCellRange("C3").setValue( "65");
        sheet.getCellRange("C4").setValue( "50");
        sheet.getCellRange("C5").setValue( "45");
        sheet.getCellRange("C6").setValue( "64");
        sheet.getCellRange("C7").setValue( "62");
        sheet.getCellRange("C8").setValue( "89");
        sheet.getCellRange("C9").setValue( "57");

        //vertically merge cells from A2 to A5, A6 to A9
        sheet.getCellRange("A2:A5").merge();
        sheet.getCellRange("A6:A9").merge();
        sheet.autoFitColumn(1);
        sheet.autoFitColumn(2);

        //add a clustered bar chart to worksheet
        Chart chart = sheet.getCharts().add(ExcelChartType.BarClustered);
        chart.setChartTitle( "Value");
        chart.getPlotArea().getFill().setFillType( ShapeFillType.NoFill);
        chart.getLegend().delete();
        chart.setLeftColumn(5);
        chart.setTopRow(1);
        chart.setRightColumn(14);

        //set the data source of series data
        chart.setDataRange(sheet.getCellRange("C2:C9"));
        chart.setSeriesDataFromRange(false);

        //set the data source of category labels
        ChartSerie serie = chart.getSeries().get(0);
        serie.setCategoryLabels( sheet.getCellRange("A2:B9"));

        //show multi-level category labels
        chart.getPrimaryCategoryAxis().setMultiLevelLable( true);

        //save the document
        workbook.saveToFile("output/createMultiLevelChart.xlsx", ExcelVersion.Version2013);
    }
}

Output:

Create Multi-Level Category Chart in Excel in Java

Java convert image to PDF

2021-06-01 07:45:14 Written by Koohji

We have demonstrated how to use Spire.PDF for java to convert PDF to image. This article will show you how to convert image to PDF in Java applications.

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

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

        //Create a PDF document
        PdfDocument pdf = new PdfDocument();
        //Add a new page
        PdfPageBase page = pdf.getPages().add();

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

        double widthFitRate = image.getPhysicalDimension().getWidth() / page.getCanvas().getClientSize().getWidth();
        double heightFitRate = image.getPhysicalDimension().getHeight() / page.getCanvas().getClientSize().getHeight();
        double fitRate = Math.max(widthFitRate, heightFitRate);

        //get the picture width and height
        double fitWidth = image.getPhysicalDimension().getWidth() / fitRate;
        double fitHeight = image.getPhysicalDimension().getHeight() / fitRate;

        //Draw image
        page.getCanvas().drawImage(image, 0, 30, fitWidth, fitHeight);

        // Save document to file
        pdf.saveToFile("output/ToPDF.pdf");
        pdf.close();

    }
}

Output:

Java convert image to PDF

page 30