Java 創建、填充PDF表單域

表單域,可以按用途分爲多種不同的類型,常見的有文本框、多行文本框、密碼框、隱藏域、複選框、單選框和下拉選擇框等,目的是用於採集用戶的輸入或選擇的數據。
下面的示例中,將分享通過Java編程在PDF中添加以及填充表單域的方法。包括:文本框、複選框、單選按鈕、列表框、組合框、簽名域、按鈕等。這裏填充表單域可分爲2種情況,一種是在創建表單域時填充,一種是加載已經創建好表單域的文檔進行填充。此外,對於已經創建表單域並填寫好的文檔,也可以設置只讀,防止修改、編輯等。

要點概括:

1.創建表單域
2.填充表單域
3.設置表單域只讀

工具:Free Spire.PDF for Java(免費版)
Jar文件獲取及導入:
方法1:通過官網下載jar文件包。下載後,解壓文件,並將lib文件夾下的Spire.Pdf.jar文件導入到java程序。
方法2:可通過maven倉庫安裝導入到maven項目。

**Java代碼示例(供參考)

【示例1】 創建並填充PDF表單域

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.fields.*;
import com.spire.pdf.graphics.*;

public class AddFormFieldsToPdf {

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

        //創建PdfDocument對象,並添加頁面
        PdfDocument doc = new PdfDocument();        
        PdfPageBase page = doc.getPages().add();

        //初始化位置變量
        float baseX = 100;
        float baseY = 0;

        //創建畫刷對象
        PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
        PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.black));

        //創建TrueType字體
        PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,10),true); 

        //添加文本框
        String text = "姓名:";//添加文本
        page.getCanvas().drawString(text, font, brush1, new Point2D.Float(0, baseY));//在PDF中繪製文字
        Rectangle2D.Float tbxBounds = new Rectangle2D.Float(baseX, baseY , 150, 15);//創建Rectangle2D對象
        PdfTextBoxField textBox = new PdfTextBoxField(page, "TextBox");//創建文本框對象
        textBox.setBounds(tbxBounds);//設置文本框的Bounds
        textBox.setText("劉興");//填充文本框
        textBox.setFont(font);//應用文本框的字體
        doc.getForm().getFields().add(textBox);//添加文本框到PDF域的集合
        baseY +=25;

        //添加複選框
        page.getCanvas().drawString("所在院系:", font, brush1, new Point2D.Float(0, baseY));
        java.awt.geom.Rectangle2D.Float rec1 = new java.awt.geom.Rectangle2D.Float(baseX, baseY, 15, 15);
        PdfCheckBoxField checkBoxField = new PdfCheckBoxField(page, "CheckBox1");//創建第一個複選框對象
        checkBoxField.setBounds(rec1);
        checkBoxField.setChecked(false);//填充複選框
        page.getCanvas().drawString("經管系", font, brush2, new Point2D.Float(baseX + 20, baseY));
        java.awt.geom.Rectangle2D.Float rec2 = new java.awt.geom.Rectangle2D.Float(baseX + 70, baseY, 15, 15);
        PdfCheckBoxField checkBoxField1 = new PdfCheckBoxField(page, "CheckBox2");//創建第二個複選框對象
        checkBoxField1.setBounds(rec2);
        checkBoxField1.setChecked(true);//填充複選框
        page.getCanvas().drawString("創新班", font,  brush2, new Point2D.Float(baseX+90, baseY));      
        doc.getForm().getFields().add(checkBoxField);//添加複選框到PDF
        baseY += 25;

        //添加列表框
        page.getCanvas().drawString("錄取批次:", font, brush1, new Point2D.Float(0, baseY));
        java.awt.geom.Rectangle2D.Float rec = new java.awt.geom.Rectangle2D.Float(baseX, baseY, 150, 50);
        PdfListBoxField listBoxField = new PdfListBoxField(page, "ListBox");//創建列表框對象
        listBoxField.getItems().add(new PdfListFieldItem("第一批次", "item1"));
        listBoxField.getItems().add(new PdfListFieldItem("第二批次", "item2"));
        listBoxField.getItems().add(new PdfListFieldItem("第三批次", "item3"));
        listBoxField.setBounds(rec);
        listBoxField.setFont(font);
        listBoxField.setSelectedIndex(0);//填充列表框
        doc.getForm().getFields().add(listBoxField);//添加列表框到PDF
        baseY += 60;

        //添加單選按鈕
        page.getCanvas().drawString("招收方式:", font, brush1, new Point2D.Float(0, baseY));
        PdfRadioButtonListField radioButtonListField = new PdfRadioButtonListField(page, "Radio");//創建單選按鈕對象
        PdfRadioButtonListItem radioItem1 = new PdfRadioButtonListItem("Item1");//創建第一個單選按鈕
        radioItem1.setBounds(new Rectangle2D.Float(baseX, baseY, 15, 15));
        page.getCanvas().drawString("全日制", font, brush2, new Point2D.Float(baseX + 20, baseY));
        PdfRadioButtonListItem radioItem2 = new PdfRadioButtonListItem("Item2");//創建第二個單選按鈕
        radioItem2.setBounds(new Rectangle2D.Float(baseX + 70, baseY, 15, 15));
        page.getCanvas().drawString("成人教育", font, brush2, new Point2D.Float(baseX + 90, baseY));
        radioButtonListField.getItems().add(radioItem1);
        radioButtonListField.getItems().add(radioItem2);
        radioButtonListField.setSelectedIndex(0);//選擇填充第一個單選按鈕
        doc.getForm().getFields().add(radioButtonListField);//添加單選按鈕到PDF
        baseY += 25;

        //添加組合框
        page.getCanvas().drawString("最高學歷:", font, brush1, new Point2D.Float(0, baseY));
        Rectangle2D.Float cmbBounds = new Rectangle2D.Float(baseX, baseY, 150, 15);//創建cmbBounds對象
        PdfComboBoxField comboBoxField = new PdfComboBoxField(page, "ComboBox");//創建comboBoxField對象
        comboBoxField.setBounds(cmbBounds);
        comboBoxField.getItems().add(new PdfListFieldItem("博士", "item1"));
        comboBoxField.getItems().add(new PdfListFieldItem("碩士", "itme2"));
        comboBoxField.getItems().add(new PdfListFieldItem("本科", "item3"));
        comboBoxField.getItems().add(new PdfListFieldItem("大專", "item4"));
        comboBoxField.setSelectedIndex(0);      
        comboBoxField.setFont(font);
        doc.getForm().getFields().add(comboBoxField);//添加組合框到PDF
        baseY += 25;

        //添加簽名域
        page.getCanvas().drawString("本人簽字確認\n以上信息屬實:", font, brush1, new Point2D.Float(0, baseY));
        PdfSignatureField sgnField= new PdfSignatureField(page,"sgnField");//創建sgnField對象
        Rectangle2D.Float sgnBounds = new Rectangle2D.Float(baseX, baseY, 150, 80);//創建sgnBounds對象
        sgnField.setBounds(sgnBounds);          
        doc.getForm().getFields().add(sgnField);//添加sgnField到PDF
        baseY += 90;

        //添加按鈕
        page.getCanvas().drawString("", font, brush1, new Point2D.Float(0, baseY));
        Rectangle2D.Float btnBounds = new Rectangle2D.Float(baseX, baseY, 50, 15);//創建btnBounds對象
        PdfButtonField buttonField = new PdfButtonField(page, "Button");//創建buttonField對象
        buttonField.setBounds(btnBounds);
        buttonField.setText("提交");//設置按鈕顯示文本
        buttonField.setFont(font);
        doc.getForm().getFields().add(buttonField);//添加按鈕到PDF  

        //保存文檔
        doc.saveToFile("result.pdf", FileFormat.PDF);              
    }
}

創建(填充)效果:Java 創建、填充PDF表單域
【示例2】加載並填充已有的表單域文檔
測試文檔如下:
Java 創建、填充PDF表單域

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.*;

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

        //創建PdfDocument對象,並加載PDF文檔
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("output.pdf");

        //獲取文檔中的域
        PdfFormWidget form = (PdfFormWidget) doc.getForm();        
        //獲取域控件集合
        PdfFormFieldWidgetCollection formWidgetCollection = form.getFieldsWidget();

        //遍歷域控件並填充數據
        for (int i = 0; i < formWidgetCollection.getCount(); i++) {

            PdfField field = formWidgetCollection.get(i);         
            if (field instanceof PdfTextBoxFieldWidget) {
                PdfTextBoxFieldWidget textBoxField = (PdfTextBoxFieldWidget) field;
                textBoxField.setText("吳 敏");
            }  
            if (field instanceof PdfCheckBoxWidgetFieldWidget) {
                PdfCheckBoxWidgetFieldWidget checkBoxField = (PdfCheckBoxWidgetFieldWidget) field;
                switch(checkBoxField.getName()){
                case "CheckBox1":
                    checkBoxField.setChecked(true);
                    break;
                case "CheckBox2":
                    checkBoxField.setChecked(true);
                    break;
                }
            }
            if (field instanceof PdfRadioButtonListFieldWidget) {
                PdfRadioButtonListFieldWidget radioButtonListField = (PdfRadioButtonListFieldWidget) field;
                radioButtonListField.setSelectedIndex(1);
            }
            if (field instanceof PdfListBoxWidgetFieldWidget) {
                PdfListBoxWidgetFieldWidget listBox = (PdfListBoxWidgetFieldWidget) field;
                listBox.setSelectedIndex(1);
            }

            if (field instanceof PdfComboBoxWidgetFieldWidget) {
                PdfComboBoxWidgetFieldWidget comboBoxField = (PdfComboBoxWidgetFieldWidget) field;
                comboBoxField.setSelectedIndex(1);
            }
        }

        //保存文檔
        doc.saveToFile("FillFormFields.pdf", FileFormat.PDF);
    }
}

填充效果:
Java 創建、填充PDF表單域

【示例3】限制表單域編輯(只讀)

import com.spire.pdf.PdfDocument;

public class FieldReadonly_PDF {
    public static void main(String[] args) throws Exception {
    {
    //創建PdfDocument對象,並加載包含表單域的PDF文檔
    PdfDocument pdf = new PdfDocument();
    pdf.loadFromFile("test.pdf");

    //將文檔中的所有表單域設置爲只讀
    pdf.getForm().setReadOnly(true);

    //保存文檔
    pdf.saveToFile("result.pdf");   
     }
  }

生成的文檔中,表單域將不可編輯,爲只讀狀態。
(本文完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章