In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to add form fields to Java in PDF". In daily operation, I believe many people have doubts about how to add form fields to Java in PDF. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to add form fields in Java in PDF". Next, please follow the editor to study!
PDF form field refers to the area where users can fill in, select and other operations independently in PDF files, and its main purpose is to collect the data input or selected by users. Common form fields include text boxes, radio buttons, check boxes, list boxes, combo boxes, and so on. The text describes how to use Free Spire.PDF for Java to create PDF form fields in a Java program.
Jar file import method
Method 1:
Download the Free Spire.PDF for Java package and extract it, then import the Spire.Pdf.jar package into your Java application from the lib folder. (after successful import, as shown in the following figure)
Method 2:
Install and import through the Maven warehouse. For detailed steps, please refer to the link:
Https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html
Java code example
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 {/ / create PdfDocument object PdfDocument doc = new PdfDocument () / / add page PdfPageBase page = doc.getPages () .add (); / / initialize position variable float baseX = 100; float baseY = 0; / / create brush object PdfSolidBrush brush2 = new PdfSolidBrush (new PdfRGBColor (Color.BLUE)); PdfSolidBrush brush3 = new PdfSolidBrush (new PdfRGBColor (Color.black)) / / create the TrueType font PdfTrueTypeFont font= new PdfTrueTypeFont (new font, Font.PLAIN,12), true); / / add the text box String text = "text box:"; / / the text in front of the text box page.getCanvas () .drawString (text, font, brush2, new Point2D.Float (0, baseY)) / / draw text Rectangle2D.Float tbxBounds = new Rectangle2D.Float (baseX, baseY, 150,15) in PDF; / / create Rectangle2D object PdfTextBoxField textBox = new PdfTextBoxField (page, "TextBox"); / / create text box object textBox.setBounds (tbxBounds); / / set Bounds of text box, including location and size information textBox.setText ("Hello") / / set the default text of the text box textBox.setFont (font); / / set the font of the text box doc.getForm (). GetFields (). Add (textBox); / / add text boxes to the collection of PDF fields baseY + = 25; / / add check boxes page.getCanvas (). DrawString ("check box:", 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 ("option 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 ("option 2", font, brush3, new Point2D.Float (baseX+90, baseY)); doc.getForm (). GetFields (). Add (checkBoxField) BaseY + = 25; / / add list box page.getCanvas (). DrawString ("list box:", 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 ("Project 1", "item1")); listBoxField.getItems () .add (new PdfListFieldItem ("Project 2", "item2")); listBoxField.getItems () .add ("Project 3", "item3");; listBoxField.setBounds (rec); listBoxField.setFont (font); listBoxField.setSelectedIndex (0) Doc.getForm (). GetFields (). Add (listBoxField); baseY + = 60; / / add radio button page.getCanvas () .drawString ("radio button:", 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 ("option 1", font, brush3, new Point2D.Float (baseX + 20, baseY)); PdfRadioButtonListItem radioItem2 = new PdfRadioButtonListItem ("Item2"); radioItem2.setBounds (baseX + 70, baseY, 15,15)) Page.getCanvas () .drawString ("option 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 / / add combo box page.getCanvas (). DrawString ("combo box:", 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 ("Project 1", "item1")) ComboBoxField.getItems () .add (new PdfListFieldItem ("Project 2", "itme2")); comboBoxField.getItems () .add (new PdfListFieldItem ("Project 3", "item3")); comboBoxField.getItems () .add (new PdfListFieldItem ("Project 4", "item4"); comboBoxField.setSelectedIndex (0); comboBoxField.setFont (font); doc.getForm () .getFields () .add (comboBoxField) BaseY + = 25; / / add signature domain page.getCanvas (). DrawString ("signature domain:", 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; / / add button page.getCanvas (). DrawString ("submit button:", 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 ("submit"); buttonField.setFont (font) Doc.getForm () .getFields () .add (buttonField); / / Save document doc.saveToFile ("AddFormField.pdf");}}
At this point, the study on "how to add form fields to Java in PDF" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.