這篇文章主要介紹“Java怎么在PDF中添加表單域”,在日常操作中,相信很多人在Java怎么在PDF中添加表單域問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Java怎么在PDF中添加表單域”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
PDF表單域是指用戶在PDF文件中可以自主進(jìn)行填寫(xiě)、選擇等操作的區(qū)域,其主要目的是采集用戶輸入或選擇的數(shù)據(jù)。常見(jiàn)的表單域包括文本框、單選按鈕、復(fù)選框、列表框和組合框等。文本將介紹如何使用Free Spire.PDF for Java在Java程序中創(chuàng)建PDF表單域。
Jar文件導(dǎo)入方法
方法一:
下載Free Spire.PDF for Java包并解壓縮,然后從lib文件夾下,將Spire.Pdf.jar包導(dǎo)入到你的Java應(yīng)用程序中。(導(dǎo)入成功后如下圖所示)
方法二:
通過(guò)Maven倉(cāng)庫(kù)安裝導(dǎo)入。詳細(xì)的操作步驟請(qǐng)參考鏈接:
https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html
Java代碼示例
import java.awt.*; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; 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 { //創(chuàng)建PdfDocument對(duì)象 PdfDocument doc = new PdfDocument(); //添加頁(yè)面 PdfPageBase page = doc.getPages().add(); //初始化位置變量 float baseX = 100; float baseY = 0; //創(chuàng)建畫(huà)刷對(duì)象 PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE)); PdfSolidBrush brush3 = new PdfSolidBrush(new PdfRGBColor(Color.black)); //創(chuàng)建TrueType字體 PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("微軟雅黑",Font.PLAIN,12),true); //添加文本框 String text = "文本框:"; //文本框前的文字 page.getCanvas().drawString(text, font, brush2, new Point2D.Float(0, baseY)); //在PDF中繪制文字 Rectangle2D.Float tbxBounds = new Rectangle2D.Float(baseX, baseY , 150, 15); //創(chuàng)建Rectangle2D對(duì)象 PdfTextBoxField textBox = new PdfTextBoxField(page, "TextBox"); //創(chuàng)建文本框?qū)ο髏extBox.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("復(fù)選框:", font, brush2, 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("選項(xiàng)1", font, brush3, 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("選項(xiàng)2", font, brush3, new Point2D.Float(baseX+90, baseY)); doc.getForm().getFields().add(checkBoxField); baseY += 25; //添加列表框 page.getCanvas().drawString("列表框:", font, brush2, 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("項(xiàng)目1", "item1")); listBoxField.getItems().add(new PdfListFieldItem("項(xiàng)目2", "item2")); listBoxField.getItems().add(new PdfListFieldItem("項(xiàng)目3", "item3"));; listBoxField.setBounds(rec); listBoxField.setFont(font); listBoxField.setSelectedIndex(0); doc.getForm().getFields().add(listBoxField); baseY += 60; //添加單選按鈕 page.getCanvas().drawString("單選按鈕:", font, brush2, 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("選項(xiàng)1", font, brush3, new Point2D.Float(baseX + 20, baseY)); PdfRadioButtonListItem radioItem2 = new PdfRadioButtonListItem("Item2"); radioItem2.setBounds(new Rectangle2D.Float(baseX + 70, baseY, 15, 15)); page.getCanvas().drawString("選項(xiàng)2", font, brush3, 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, brush2, 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("項(xiàng)目1", "item1")); comboBoxField.getItems().add(new PdfListFieldItem("項(xiàng)目2", "itme2")); comboBoxField.getItems().add(new PdfListFieldItem("項(xiàng)目3", "item3")); comboBoxField.getItems().add(new PdfListFieldItem("項(xiàng)目4", "item4")); comboBoxField.setSelectedIndex(0); comboBoxField.setFont(font); doc.getForm().getFields().add(comboBoxField); baseY += 25; //添加簽名域 page.getCanvas().drawString("簽名域:", font, brush2, new Point2D.Float(0, baseY)); PdfSignatureField sgnField= new PdfSignatureField(page,"sgnField"); Rectangle2D.Float sgnBounds = new Rectangle2D.Float(baseX, baseY, 150, 80); sgnField.setBounds(sgnBounds); doc.getForm().getFields().add(sgnField); baseY += 90; //添加按鈕 page.getCanvas().drawString("提交按鈕:", font, brush2, 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"); } }
到此,關(guān)于“Java怎么在PDF中添加表單域”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
分享文章:Java怎么在PDF中添加表單域-創(chuàng)新互聯(lián)
文章來(lái)源:http://chinadenli.net/article22/ddisjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)站內(nèi)鏈、外貿(mào)建站、做網(wǎng)站、關(guān)鍵詞優(yōu)化、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容