Wednesday, 07 August 2019 09:25

Create a Multi-Column Word Document in Java

We can format Word document in a multi-column newsletter layout by adding columns. This article demonstrates how to add multiple columns to a Word document and specify the column width and the spacing between columns using Spire.Doc for Java.

import com.spire.doc.*;
import com.spire.doc.documents.*;

public class CreateMutiColumnWordDocument {
    public static void main(String[] args){
        //create a Document object
        Document document = new Document();
        //add a section 
        Section section = document.addSection();

        //add 3 columns to the section
        section.addColumn(100, 20);
        section.addColumn(100, 20);
        section.addColumn(100, 20);

        //add a paragraph to the section
        Paragraph paragraph = section.addParagraph();
        //add a paragraph to the section
        paragraph = section.addParagraph();
        String text = "Spire.Doc for Java is a professional Java Word API that enables Java applications "
        +"to create, convert, manipulate and print Word documents without using Microsoft Office.";
        //add text to the paragraph
        paragraph.appendText(text);
        //add column break to the paragraph
        paragraph.appendBreak(BreakType.Column_Break);

        //add a paragraph to the section
        paragraph = section.addParagraph();
        //add text to the paragraph
        paragraph.appendText(text);
        //add column break to the paragraph
        paragraph.appendBreak(BreakType.Column_Break);

        //add a paragraph to the section
        paragraph = section.addParagraph();
        //add text to the paragraph
        paragraph.appendText(text);

        //add line between columns
        section.getPageSetup().setColumnsLineBetween(true);

        //save the resultant document
        document.saveToFile("Muti-Column Document.docx", FileFormat.Docx_2013);

    }
}

Output:

Create a Multi-Column Word Document in Java

Wednesday, 07 August 2019 07:28

Java make a booklet from a PDF document

When we print a huge PDF document, print as a booklet is a great way to save the paper and make the pages tidy. This article we will introduce how to create a booklet from a PDF document in Java applications.

import com.spire.pdf.*;


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

        String inputPath = "Sample.pdf";

        PdfDocument doc = new PdfDocument();
        doc.loadFromFile(inputPath);
        PdfPageBase page = doc.getPages().get(0);

        float width = (float) page.getSize().getWidth()*2;
        float height = (float) page.getSize().getHeight();

        doc.createBooklet(inputPath, width, height,true);

        doc.saveToFile("Output/Booklet.pdf");

    }
}

Effective screenshot after creating PDF booklet:

Java make a booklet from a PDF document

This article demonstrates how to detect the required form fields in an existing PDF document using Spire.PDF for Java.

import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.PdfFormWidget;

public class DetectRequiredFields {

    public static void main(String[] args) {

        //load a PDF file
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Fields.pdf");

        //get form widget from the PDF document.
        PdfFormWidget formWidget = (PdfFormWidget)doc.getForm();

        //loop through the fields widget
        for (int i = 0; i < formWidget.getFieldsWidget().getList().getCapacity(); i++) {

            //get the specific field
            PdfField field = (PdfField) formWidget.getFieldsWidget().getList().get_Item(i);

            //get the field name
            String fieldName = field.getName();

            //determine if the field is required
            boolean isRequired = field.getRequired();
            if (isRequired){

                //print the required field
                System.out.println(fieldName + " is required");
            }
        }
    }
}

Detect Required Fields in a PDF Document in Java

This article demonstrates how to add combo box, check box, text, picture, date picker and drop-down list content controls to a Word document using Spire.Doc for Java.

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

import java.util.Date;

public class ContentControls {
    public static void main(String[] args){
        //create a new Word document
        Document document = new Document();
        Section section = document.addSection();
        Paragraph paragraph = section.addParagraph();
        TextRange txtRange = paragraph.appendText("The following example shows how to add content controls in a Word document.");
        section.addParagraph();

        //add combo box content control
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("Add Combo Box Content Control:  ");
        txtRange.getCharacterFormat().setItalic(true);
        StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Combo_Box);
        sd.getSDTProperties().setAlias("ComboBox");
        sd.getSDTProperties().setTag("ComboBox");
        SdtComboBox cb = new SdtComboBox();
        cb.getListItems().add(new SdtListItem("Spire.Doc"));
        cb.getListItems().add(new SdtListItem("Spire.XLS"));
        cb.getListItems().add(new SdtListItem("Spire.PDF"));
        sd.getSDTProperties().setControlProperties(cb);
        TextRange rt = new TextRange(document);
        rt.setText(cb.getListItems().get(0).getDisplayText());
        sd.getSDTContent().getChildObjects().add(rt);
        section.addParagraph();

        //Add checkbox content control
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("Add Check Box Content Control:  ");
        txtRange.getCharacterFormat().setItalic(true);
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Check_Box);
        sd.getSDTProperties().setAlias("CheckBox");
        sd.getSDTProperties().setTag("CheckBox");
        SdtCheckBox scb = new SdtCheckBox();
        sd.getSDTProperties().setControlProperties(scb);
        rt = new TextRange(document);
        sd.getChildObjects().add(rt);
        scb.setChecked(true);
        section.addParagraph();

        //add text content control
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("Add Text Content Control:  ");
        txtRange.getCharacterFormat().setItalic(true);
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Text);
        sd.getSDTProperties().setAlias("Text");
        sd.getSDTProperties().setTag("Text");
        SdtText text = new SdtText(true);
        text.isMultiline(true);
        sd.getSDTProperties().setControlProperties(text);
        rt = new TextRange(document);
        rt.setText("Text");
        sd.getSDTContent().getChildObjects().add(rt);
        section.addParagraph();

        //add picture content control
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("Add Picture Content Control:  ");
        txtRange.getCharacterFormat().setItalic(true);
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setControlProperties(new SdtPicture());
        sd.getSDTProperties().setAlias("Picture");
        sd.getSDTProperties().setTag("Picture");
        DocPicture pic = new DocPicture(document);
        pic.setWidth(10f);
        pic.setHeight(10f);
        pic.loadImage("logo.png");
        sd.getSDTContent().getChildObjects().add(pic);
        section.addParagraph();

        //add date picker content control
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("Add Date Picker Content Control:  ");
        txtRange.getCharacterFormat().setItalic(true);
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Date_Picker);
        sd.getSDTProperties().setAlias("Date");
        sd.getSDTProperties().setTag("Date");
        SdtDate date = new SdtDate();
        date.setCalendarType(CalendarType.Default);
        date.setDateFormat("yyyy.MM.dd");
        date.setFullDate(new Date());
        sd.getSDTProperties().setControlProperties(date);
        rt = new TextRange(document);
        rt.setText("2018.12.25");
        sd.getSDTContent().getChildObjects().add(rt);
        section.addParagraph();

        //add drop-down list content control
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("Add Drop-Down List Content Control:  ");
        txtRange.getCharacterFormat().setItalic(true);
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Drop_Down_List);
        sd.getSDTProperties().setAlias("DropDownList");
        sd.getSDTProperties().setTag("DropDownList");
        SdtDropDownList sddl = new SdtDropDownList();
        sddl.getListItems().add(new SdtListItem("Harry"));
        sddl.getListItems().add(new SdtListItem("Jerry"));
        sd.getSDTProperties().setControlProperties(sddl);
        rt = new TextRange(document);
        rt.setText(sddl.getListItems().get(0).getDisplayText());
        sd.getSDTContent().getChildObjects().add(rt);

        //save the resultant document
        document.saveToFile("addContentControls.docx", FileFormat.Docx_2013);
    }
}

Output:

Add Content Controls to Word Document in Java

This article will show you how to set the wrapping style to adjust the position of the image in Java applications with the help of Spire.Doc for Java.

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.documents.TextWrappingType;
import com.spire.doc.fields.DocPicture;

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

        Document doc = new Document();
        doc.loadFromFile("Sample.docx");

        Section sec = doc.getSections().get(0);

        Paragraph para = sec.getParagraphs().get(0);
        DocPicture picture = para.appendPicture("logo.png");

        //Set image width and height
        picture.setWidth(150f);
        picture.setHeight(125f);

        //Set text wrapping style to Behind
        picture.setTextWrappingStyle(TextWrappingStyle.Behind);
        picture.setTextWrappingType(TextWrappingType.Both);

        //Save the document to file
        doc.saveToFile("Output/WrapStyle.docx");
        doc.close();

    }
}

Effective screenshot after setting the wrapping style for image:

How to wrap text around image on Word document in Java

In document creation, divisions of content are frequently required to fulfill specific layout requirements and establish logical structures. The insertion of section breaks and page breaks is the most commonly employed method for dividing content, as it enables flexible control over page and section organization. Moreover, page breaks and section breaks are quite helpful in formatting purposes and the establishment of distinct document styles. This article aims to demonstrate how to use Spire.Doc for Java to insert page breaks and section breaks into Word documents through Java programs.

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.6</version>
    </dependency>
</dependencies>
    

Insert Page Breaks into Word Documents

Spire.Doc for Java provides the Paragraph.appendBreak(BreakType.PageBreak) method to insert a page break at the end of a paragraph. The detailed steps are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Get the first section using Document.getSections().get() method.
  • Get the eighth paragraph of the section using Section.getParagraphs().get() method.
  • Add a page break to the end of the paragraph using Paragraph.appendBreak(BreakType.PageBreak) method.
  • Save the document using Document.saveToFile() method.
  • 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.Paragraph;

public class InsertPageBreak {
    public static void main(String[] args) {
        //Create an object of Document class
        Document doc = new Document();

        //Load a Word document
        doc.loadFromFile("Sample.docx");

        //Get the first section
        Section section = doc.getSections().get(0);

        //Get the eighth paragraph
        Paragraph paragraph = section.getParagraphs().get(7);

        //Add a page break to the end of the paragraph
        paragraph.appendBreak(BreakType.Page_Break);

        //Save the document
        doc.saveToFile("PageBreak.docx", FileFormat.Auto);
    }
}

Java: Insert Page Breaks and Section Breaks to Word

Insert Section Breaks into Word Documents

Inserting section breaks involves using the Paragraph.insertSectionBreak(SectionBreakType) method. This method allows users to specify the type of section break you want to insert. he following table provides an overview of the section break types, along with their corresponding Enums and descriptions:

Section Break Enum Description
New page SectionBreakType.New_Page Start the new section on a new page.
Continuous SectionBreakType.No_Break Start the new section without starting a new page, allowing for continuous content flow.
Odd page SectionBreakType.Odd_Page Start the new section on the next odd-numbered page.
Even page SectionBreakType.Even_Page Start the new section on the next even-numbered page.
New column SectionBreakType.New_Column Start the new section in the next column if columns are enabled.

The detailed steps for inserting a continuous section break are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Get the first section using Document.getSections().get() method.
  • Get the second paragraph of the section using Section.getParagraphs().get() method.
  • Add a continuous section break to the end of the paragraph using Paragraph.insertSectionBreak(SectionBreakType.No_Break) method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.SectionBreakType;

public class InsertSectionBreak {
    public static void main(String[] args) {
        //Create an object of Document class
        Document doc = new Document();

        //Load a Word document
        doc.loadFromFile("Sample.docx");

        //Get the first section
        Section section = doc.getSections().get(0);

        //Get the second paragraph
        Paragraph paragraph = section.getParagraphs().get(1);

        //Insert a section break
        paragraph.insertSectionBreak(SectionBreakType.No_Break);

        //Save the document
        doc.saveToFile("SectionBreak.docx", FileFormat.Auto);
    }
}

Java: Insert Page Breaks and Section Breaks to Word

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, 25 July 2019 05:42

Delete Attachments in PDF in Java

This article demonstrates how to delete attachments and annotation attachments in a PDF document using Spire.PDF for Java.

Delete Attachments

import com.spire.pdf.attachments.PdfAttachmentCollection;

public class DeleteAttachments {

    public static void main(String[] args) {

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

        //get the attachments collection, not containing annotation attachments
        PdfAttachmentCollection attachments = doc.getAttachments();
        
        //remove all attachments
        attachments.clear();

        //remove a specific attachment
        //attachments.removeAt(0);

        //save to file
        doc.saveToFile("output/DeleteAttachments.pdf");
        doc.close();
    }
}

Delete Annotation Attachments

import com.spire.pdf.annotations.PdfAnnotation;
import com.spire.pdf.annotations.PdfAnnotationCollection;
import com.spire.pdf.annotations.PdfAttachmentAnnotationWidget;

public class DeleteAnnotationAttachments {

    public static void main(String[] args) {

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

        //loop through the pages
        for (int i = 0; i < doc.getPages().getCount(); i++) {

            //get the annotations collection
            PdfAnnotationCollection annotationCollection = doc.getPages().get(i).getAnnotationsWidget();

            //loop through the annotations
            for (Object annotation: annotationCollection) {
                
                //determine if an annotation is an instance of PdfAttachmentAnnotationWidget 
                if (annotation instanceof PdfAttachmentAnnotationWidget){
                    
                    //remove the attachment annotation
                    annotationCollection.remove((PdfAnnotation) annotation);
                }
            }
        }

        //save to file
        doc.saveToFile("output/DeleteAnnotationAttachments.pdf");
        doc.close();
    }
}

We have already demonstrated how to set PDF Document Properties in Java. This article we will show you how to set custom properties for PDF files in Java.

import com.spire.pdf.*;

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

        String inputPath = "Sample.pdf";
        PdfDocument doc = new PdfDocument(inputPath);
        doc.loadFromFile(inputPath);

        //Set the custom properties
        doc.getDocumentInformation().setCustomerDefined("Number", "123");
        doc.getDocumentInformation().setCustomerDefined("Name", "Daisy");
        doc.getDocumentInformation().setCustomerDefined("Company", "e-iceblue");
       
        //Save the document to file
        doc.saveToFile("Output/result.pdf");
        doc.close();

    }
}

Effective screenshot after adding custom properties to PDF document:

Set custom properties for PDF files in Java

A bulleted list is a list with dots in the front and the same indentation for each item, while a numbered list with numbers in the front. Bulleted lists and numbered lists work much better than lengthy text content because, with a well-organized list, readers can easily get the structure and the point of each item. This article shows how to create lists from existing text in Word documents with Spire.Doc for Java.

Install Spire.Doc for Java

First of all, 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.6</version>
    </dependency>
</dependencies>
    

Create a Bulleted List and a Numbered List from Existing Text in a Word Document

Spire.Doc for Java provides two methods, ListFormat.applyBulletStyle() and ListFormat.applyNumberedStyle(), to create bulleted and numbered lists.

The detailed steps of creating bulleted and numbered lists are as follows.

  • Create an object of Document class.
  • Load a Word document from file using Document.loadFromFile() method.
  • Get the first section using Document.getSections().get() method.
  • Loop through the 4th to 6th paragraphs.
  • Apply bulleted list style to these paragraphs using ListFormat.applyBulletStyle() method and set their position using ListFormat.getCurrentListLevel().setNumberPosition() method.
  • Loop through the 10th to 12th paragraphs.
  • Apply numbered list style to these paragraphs using ListFormat.applyNumberedStyle() method and set their position using ListFormat.getCurrentListLevel().setNumberPosition() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.formatting.ListFormat;

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

        //Create an object of Document class
        Document document = new Document();

        //Load a Word document from file
        document.loadFromFile("C:/Samples/Sample2.docx");

        //Get the first section
        Section section = document.getSections().get(0);

        //Loop through the 4th to the 6th paragraphs
        for(int i = 3; i <= 5; i++){
            Paragraph para = section.getParagraphs().get(i);
            ListFormat listFormat = para.getListFormat();

            //Apply bulleted list style
            listFormat.applyBulletStyle();

            //Set list position
            listFormat.getCurrentListLevel().setNumberPosition(-10);
        }

        //Loop through the 10th to the 12th paragraphs
        for(int i = 9; i <= 11; i++){
            Paragraph para = section.getParagraphs().get(i);
            ListFormat listFormat = para.getListFormat();

            //Apply numbered list style
            listFormat.applyNumberedStyle();

            //Set list position
            listFormat.getCurrentListLevel().setNumberPosition(-10);

        }

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

Java: Create Bulleted or Numbered Lists from Existing Text in 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.

With the rapid development of digital technologies, more and more companies create, store, and transmit information electronically. To ensure the credibility and authenticity of the electronic documents, digital signatures have been widely used. In this article, you will learn how to add an invisible or a visible digital signature to PDF and how to remove digital signatures from PDF by using Spire.PDF for Java.

Install Spire.PDF for Java

First, 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>
    

Add an Invisible Digital Signature to PDF

The following are the steps to add an invisible digital signature to PDF using Spire.PDF for Java.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.loadFromFile() method.
  • Load a pfx certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object based on the certificate.
  • Set the document permissions using PdfSignature. setDocumentPermissions() method.
  • Save the document to another PDF file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfCertificate;
import com.spire.pdf.security.PdfCertificationFlags;
import com.spire.pdf.security.PdfSignature;

public class AddInvisibleSignature {

    public static void main(String[] args) {

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

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

        //Load a pfx certificate
        PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");

        //Create a PdfSignature object
        PdfSignature signature = new PdfSignature(doc, doc.getPages().get(doc.getPages().getCount() - 1), cert, "MySignature");

        //Set the document permission to forbid changes but allow form fill
        signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes);
        signature.setDocumentPermissions(PdfCertificationFlags.Allow_Form_Fill);

        //Save to another PDF file
        doc.saveToFile("output/InvisibleSignature.pdf");
        doc.close();
    }
}

Java: Add or Delete Digital Signatures in PDF

Add a Visible Digital Signature to PDF

The following are the steps to add a visible digital signature to PDF using Spire.PDF for Java.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFromFile() method.
  • Load a pfx certificate file while initializing the PdfCertificate object.
  • Create a PdfSignature object and specify its position and size on the document.
  • Set the signature details including date, name, location, reason, handwritten signature image, and document permissions.
  • Save the document to another PDF file using PdfDocument.SaveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.PdfFont;
import com.spire.pdf.graphics.PdfFontFamily;
import com.spire.pdf.graphics.PdfFontStyle;
import com.spire.pdf.graphics.PdfImage;
import com.spire.pdf.security.GraphicMode;
import com.spire.pdf.security.PdfCertificate;
import com.spire.pdf.security.PdfCertificationFlags;
import com.spire.pdf.security.PdfSignature;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

public class AddVisibleSignature {

    public static void main(String[] args) {

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

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

        //Load a pfx certificate
        PdfCertificate cert = new PdfCertificate("C:\\Users\\Administrator\\Desktop\\MyCertificate.pfx", "e-iceblue");

        //Create a PdfSignature object and specify its position and size
        PdfSignature signature = new PdfSignature(doc, doc.getPages().get(0), cert, "MySignature");
        Rectangle2D rect = new Rectangle2D.Float();
        rect.setFrame(new Point2D.Float((float) doc.getPages().get(0).getActualSize().getWidth() - 320, (float) doc.getPages().get(0).getActualSize().getHeight() - 140), new Dimension(270, 100));
        signature.setBounds(rect);

        //Set the graphics mode
        signature.setGraphicMode(GraphicMode.Sign_Image_And_Sign_Detail);

        //Set the signature content
        signature.setNameLabel("Signer:");
        signature.setName("Gary");
        signature.setContactInfoLabel("ContactInfo:");
        signature.setContactInfo("02881705109");
        signature.setDateLabel("Date:");
        signature.setDate(new java.util.Date());
        signature.setLocationInfoLabel("Location:");
        signature.setLocationInfo("Chengdu");
        signature.setReasonLabel("Reason: ");
        signature.setReason("The certificate of this document");
        signature.setDistinguishedNameLabel("DN: ");
        signature.setDistinguishedName(signature.getCertificate().get_IssuerName().getName());
        signature.setSignImageSource(PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\handwrittenSignature.png"));

        //Set the signature font
        signature.setSignDetailsFont(new PdfFont(PdfFontFamily.Helvetica, 10f, PdfFontStyle.Regular));

        //Set the document permission
        signature.setDocumentPermissions(PdfCertificationFlags.Forbid_Changes);
        signature.setCertificated(true);

        //Save to file
        doc.saveToFile("output/VisibleSignature.pdf");
        doc.close();
    }
}

Java: Add or Delete Digital Signatures in PDF

Remove Digital Signatures from PDF

The following are the steps to remove digital signatures from PDF using Spire.PDF for Java.

  • Create a PdfDocument object.
  • Get form widgets from the document using PdfDocument.getForm() method.
  • Loop through the widgets and determine if a specific widget is a PdfSignatureFieldWidget.
  • Remove the signature widget using PdfFieldCollection.remove() method.
  • Save the document to another PDF file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.widget.PdfFieldWidget;
import com.spire.pdf.widget.PdfFormWidget;
import com.spire.pdf.widget.PdfSignatureFieldWidget;

public class DeleteSignatures {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument("C:\\Users\\Administrator\\Desktop\\VisibleSignature.pdf");

        //Get form widgets from the document
        PdfFormWidget widgets = (PdfFormWidget) doc.getForm();

        //Loop through the widgets
        for (int i = 0; i < widgets.getFieldsWidget().getList().size(); i++)
        {
            //Get a specific widget
            PdfFieldWidget widget = (PdfFieldWidget)widgets.getFieldsWidget().getList().get(i);

            //Determine if the widget is a PdfSignatureFieldWidget
            if (widget instanceof PdfSignatureFieldWidget)
            {
                //Remove the signature widget
                widgets.getFieldsWidget().remove(widget);;
            }
        }

        //Save the document to another PDF file
        doc.saveToFile("output/DeleteSignatures.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.