日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java pdf表单域实现_Java 创建PDF表单域 - 文本框、复选框、列表框、组合框、按钮等...

發(fā)布時間:2023/12/15 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java pdf表单域实现_Java 创建PDF表单域 - 文本框、复选框、列表框、组合框、按钮等... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

表單域包括文本框、復(fù)選框、列表框、組合框、按鈕和簽名域等,主要用于收集用戶填寫或選擇的數(shù)據(jù)。這篇文章將介紹如何在Java應(yīng)用程序中給PDF文檔添加表單域。

導(dǎo)入jar文件

本文所使用的PDF類庫是Free Spire.PDF for Java, 如果你的項目是maven工程的話,在pom.xml文件中添加如下依賴,即可導(dǎo)入Free Spire.PDF for Java庫jar文件。

com.e-iceblue

e-iceblue

http://repo.e-iceblue.com/nexus/content/groups/public/

e-iceblue

spire.pdf.free

2.2.2

如果不是maven,可通過官網(wǎng)下載最新的Free Spire.PDF for Java的jar文件(是一個壓縮包,解壓縮后在lib文件夾下可找到j(luò)ar文件),然后將jar文件添加至程序中,下載地址。

實現(xiàn)代碼

Free Spire.PDF for Java支持添加和操作多種PDF表單域,下表列出了一些常見的域及其在Free Spire.PDF for Java中對應(yīng)的類名。

域名

類名

文本框

PdfTextBoxField

復(fù)選框

PdfCheckBoxField

列表框

PdfListBoxField

組合框

PdfComboBoxField

單選按鈕

PdfRadioButtonListField

按鈕

PdfButtonField

簽名域

PdfSignatureField

以下代碼展示了如何給PDF文檔添加文本框、復(fù)選框、列表框、組合框、單選按鈕和按鈕。簽名域在之前的文章中已經(jīng)寫過了,此處不再介紹。

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 CreateFormFields {

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

//創(chuàng)建PdfDocument對象

PdfDocument doc = new PdfDocument();

//添加頁面

PdfPageBase page = doc.getPages().add();

//初始化位置變量

float baseX = 100;

float baseY = 50;

//創(chuàng)建畫刷對象

PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));

PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.black));

//創(chuàng)建TrueType字體

PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12),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);//創(chuàng)建Rectangle2D對象

PdfTextBoxField textBox = new PdfTextBoxField(page, "TextBox");//創(chuàng)建文本框?qū)ο?/p>

textBox.setBounds(tbxBounds);//設(shè)置文本框的Bounds,包括位置和大小信息

textBox.setText("洋洋");//設(shè)置文本框的默認(rèn)文字

textBox.setFont(font);//設(shè)置文本框的字體

doc.getForm().getFields().add(textBox);//添加文本框到PDF域的集合

baseY +=25;

//添加復(fù)選框

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(true);

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(false);

page.getCanvas().drawString("少數(shù)民族", font,? brush2, new Point2D.Float(baseX+90, baseY));

doc.getForm().getFields().add(checkBoxField);

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("3歲", "item1"));

listBoxField.getItems().add(new PdfListFieldItem("4歲", "item2"));

listBoxField.getItems().add(new PdfListFieldItem("5歲", "item3"));;

listBoxField.setBounds(rec);

listBoxField.setFont(font);

listBoxField.setSelectedIndex(0);

doc.getForm().getFields().add(listBoxField);

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

baseY += 25;

//添加組合框

page.getCanvas().drawString("班級:", font, brush1, new Point2D.Float(0, baseY));

Rectangle2D.Float cmbBounds = new Rectangle2D.Float(baseX, baseY, 150, 15);

PdfComboBoxField comboBoxField = new PdfComboBoxField(page, "ComboBox");

comboBoxField.setBounds(cmbBounds);

comboBoxField.getItems().add(new PdfListFieldItem("幼兒園小班", "item1"));

comboBoxField.getItems().add(new PdfListFieldItem("幼兒園中班", "itme2"));

comboBoxField.getItems().add(new PdfListFieldItem("幼兒園大班", "item3"));

comboBoxField.setSelectedIndex(0);

comboBoxField.setFont(font);

doc.getForm().getFields().add(comboBoxField);

baseY += 25;

//添加按鈕

page.getCanvas().drawString("提交按鈕:", font, brush1, new Point2D.Float(0, baseY));

Rectangle2D.Float btnBounds = new Rectangle2D.Float(baseX, baseY, 50, 15);

PdfButtonField buttonField = new PdfButtonField(page, "Button");

buttonField.setBounds(btnBounds);

buttonField.setText("提交");

buttonField.setFont(font);

doc.getForm().getFields().add(buttonField);

//保存文檔

doc.saveToFile("AddFormField.pdf", FileFormat.PDF);

}

}

生成文檔:

總結(jié)

以上是生活随笔為你收集整理的java pdf表单域实现_Java 创建PDF表单域 - 文本框、复选框、列表框、组合框、按钮等...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。