Java: Flatten Form Fields in PDF

2022-08-16 08:35:00 Written by Koohji

Form fields are often used in PDF documents to collect information from users. In some cases, you may need to flatten form fields in a PDF. For instance, when you want to prevent other viewers from editing the information you entered in the form fields of a PDF questionnaire. This article will introduce how to flatten form fields in PDF in Java using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you need 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 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>11.11.11</version>
    </dependency>
</dependencies>

Flatten a Specific Form Field in PDF in Java

The following are the steps to flatten a specific form field in a PDF document using Spire.PDF for Java:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Get the form widget collection from the document.
  • Get a specific form field from the widget collection by its name or index through PdfFormWidget.getFieldsWidget().get() method.
  • Flatten the form field through PdfField.setFlatten() method.
  • Save the result document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.PdfFormWidget;

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

        //Get the form widget collection
        PdfFormWidget formWidget = (PdfFormWidget)pdf.getForm();
        //Get a specific form field by its name
        PdfField form = formWidget.getFieldsWidget().get("Address");
        //Get a specific form field by its index
        //PdfField form = formWidget.getFieldsWidget().get(2);
        //Flatten the form
        form.setFlatten(true);

        //Save the result document
        pdf.saveToFile("FlattenSpecific.pdf");
    }
}

Java:  Flatten Form Fields in PDF

Flatten All Form Fields in PDF in Java

The following are the steps to flatten all the form fields in a PDF document using Spire.PDF for Java:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Flatten all the form fields in the document through PdfDocument.getForm().isFlatten() method.
  • Save the result document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;

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

        //Flatten all the forms in the document
        pdf.getForm().isFlatten(true);

        //Save the result document
        pdf.saveToFile("FlattenAll.pdf");
    }
}

Java:  Flatten Form Fields 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.

Insert Images to a Table in Word in Java

2020-07-10 07:21:49 Written by Koohji

This article demonstrates how to insert images to table cells in a Word document using Spire.Doc for Java.

import com.spire.doc.AutoFitBehaviorType;
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.Table;
import com.spire.doc.fields.DocPicture;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class InsertImageToTableCell {

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

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

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

        //Add a table
        Table table = section.addTable(true);
        table.resetCells(2,2);
        table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);

        //Load an image to InputStream
        InputStream inputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\company-logo.png");
        
        //Insert the image to the cell(0,0)
        DocPicture picture = table.get(0,0).addParagraph().appendPicture(inputStream);
        
        //Set the width and height of the image
        picture.setWidth(100);
        picture.setHeight(100);

        //Insert another image to the cell(1,1)
        inputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\intro.png");
        picture = table.get(1,1).addParagraph().appendPicture(inputStream);
        picture.setWidth(100);
        picture.setHeight(100);

        //Save the document
        document.saveToFile("InsertImgToCell.docx");
    }
}

Insert Images to a Table in Word in Java

Create a Nested Table in Word in Java

2020-07-02 08:54:26 Written by Koohji

This article demonstrates how to insert a nested table in a table cell using Spire.Doc for Java.

import com.spire.doc.*;

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

        //Create a Document object
        Document doc = new Document();
        
        //Add a section
        Section section = doc.addSection();

        //Add a table
        Table table = section.addTable(true);
        table.resetCells(2, 3);

        //Set column width
        table.getRows().get(0).getCells().get(0).setCellWidth(50f,CellWidthType.Point);
        table.getRows().get(0).getCells().get(2).setCellWidth(50f,CellWidthType.Point);
        table.getRows().get(1).getCells().get(0).setCellWidth(50f,CellWidthType.Point);
        table.getRows().get(1).getCells().get(2).setCellWidth(50f,CellWidthType.Point);
        table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window);

        //Insert content to cells
        table.get(0,0).addParagraph().appendText("SI.No.");
        String text = "Earthwork excavation for foundation of buildings, water supply, "
                + "sanitary lines and electrical conduits either in pits or in "
                + "trenches 1.5m and above in width, in ordinary soil not exceeding "
                + "1.5m in depth including dressing the bottom and sides of pits and  "
                + "trenches, stacking the excavated soil clear.";
        table.get(0,1).addParagraph().appendText(text);
        table.get(0,2).addParagraph().appendText("Qty");

        //Add a nested table to cell(0,1)
        Table nestedTable = table.get(0,1).addTable();
        nestedTable.resetCells(3, 4);
        nestedTable.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);

        //Add content to the cells of nested table
        nestedTable.get(0,0).addParagraph().appendText("SI.No.");
        nestedTable.get(0,1).addParagraph().appendText("Item");
        nestedTable.get(0,2).addParagraph().appendText("Qty");
        nestedTable.get(0,3).addParagraph().appendText("Rate");
        nestedTable.get(1,0).addParagraph().appendText("1");
        nestedTable.get(1,1).addParagraph().appendText("Sand");
        nestedTable.get(1,2).addParagraph().appendText("30");
        nestedTable.get(1,3).addParagraph().appendText("45");
        nestedTable.get(2,0).addParagraph().appendText("2");
        nestedTable.get(2,1).addParagraph().appendText("Cement");
        nestedTable.get(2,2).addParagraph().appendText("30");
        nestedTable.get(2,3).addParagraph().appendText("50");

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

Create a Nested Table in Word in Java

page 43

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details