Java で PDF フォームフィールドを作成して入力する方法 (コード例)
この記事の内容は、Java で PDF フォーム フィールドを作成および入力する方法 (コード例) に関するもので、必要な方は参考にしていただければ幸いです。
フォーム フィールドは、用途に応じてさまざまな種類に分類できます。一般的なものには、テキスト ボックス、複数行のテキスト ボックス、パスワード ボックス、隠しフィールド、チェック ボックス、ラジオ ボタン、ドロップダウン選択ボックスなどがあります。目的は、ユーザー入力または選択されたデータを収集することです。次の例では、Java プログラミングを使用して PDF にフォーム フィールドを追加および入力する方法を紹介します。ここでのフォームフィールドへの入力は、フォームフィールド作成時に入力する場合と、フォームフィールドが作成済みの文書に入力する場合の2つに分けられます。さらに、フォームフィールドを作成して入力した文書については、変更や編集を禁止するために読み取り専用に設定することもできます。
重要なポイントのまとめ:
1. フォーム フィールドを作成します
2. フォーム フィールドを設定します。フィールドは読み取り専用
ツール:Free Spire.PDF for Java v2.0.0 (無料バージョン)
Jar###ファイルのインポート手順
1:Java プログラム内に新しいフォルダーを作成し、「Lib」という名前を付けます。そして、製品パッケージ内の 2 つの jar ファイルを、新しく作成したフォルダーにコピーします。
ステップ 2
:ファイルをコピーした後、それらを参照クラス ライブラリに追加します。これらの 2 つの jar ファイルを選択し、右クリックします。を押しながらクリックし、「ビルドパス」-「ビルドパスに追加」を選択します。引用を完了します。
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); } }
2.
既存のフォーム フィールド ドキュメントをロードして設定しますテスト ドキュメントは次のとおりです:
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); } }
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"); } }
以上がJava で PDF フォームフィールドを作成して入力する方法 (コード例)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Java の Weka へのガイド。ここでは、weka java の概要、使い方、プラットフォームの種類、利点について例を交えて説明します。

この記事では、Java Spring の面接で最もよく聞かれる質問とその詳細な回答をまとめました。面接を突破できるように。

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです

Java での日付までのタイムスタンプに関するガイド。ここでは、Java でタイムスタンプを日付に変換する方法とその概要について、例とともに説明します。

カプセルは3次元の幾何学的図形で、両端にシリンダーと半球で構成されています。カプセルの体積は、シリンダーの体積と両端に半球の体積を追加することで計算できます。このチュートリアルでは、さまざまな方法を使用して、Javaの特定のカプセルの体積を計算する方法について説明します。 カプセルボリュームフォーミュラ カプセルボリュームの式は次のとおりです。 カプセル体積=円筒形の体積2つの半球体積 で、 R:半球の半径。 H:シリンダーの高さ(半球を除く)。 例1 入力 RADIUS = 5ユニット 高さ= 10単位 出力 ボリューム= 1570.8立方ユニット 説明する 式を使用してボリュームを計算します。 ボリューム=π×R2×H(4

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHPは、シンプルな構文と高い実行効率を備えたWeb開発に適しています。 2。Pythonは、簡潔な構文とリッチライブラリを備えたデータサイエンスと機械学習に適しています。
