IronPDF for Java 允許開發者通過將 HTML 轉換為 PDF 來構建動態、交互式 PDF 表單 - 文字輸入、複選框、下拉列表和單選按鈕。 HTML 優先的方法讓您可以使用標準 Web 技術完全掌控表單的布局和行為,而無需學習專有的表單 API。 一旦您了解 IronPDF 如何建模表單字段,您還可以以編程方式填寫現有的 PDF 表單。
傳統的 Java PDF 程式庫要求您通過低階 PDF 物件構建表單字段。 IronPDF 採取了不同的方法:在HTML和 CSS 中編寫您的表單,然後將其渲染為保留每個互動式字段的 PDF。 該結果與 Adobe Acrobat Reader 和所有主要的 PDF 查看器相容。
編寫包含標準表單元素的 HTML 字串 (<input>, <textarea>, <select>)
調用 PdfDocument.renderHtmlAsPdf() 將 HTML 轉換為 PDF
使用 saveAs() 保存生成的文件
import com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument;// Set license key before any rendering callLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");// HTML form with a text field and submit buttonString formHtml = "<h2>Registration Form</h2>" + "<form>" + "<label>Name: <input type='text' name='fullname' placeholder='Enter name'></label><br>" + "<button type='submit'>Submit</button>" + "</form>";// Render HTML to PDF — form fields are preservedPdfDocument pdf = PdfDocument.renderHtmlAsPdf(formHtml);pdf.saveAs("registration-form.pdf");
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
// Set license key before any rendering call
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// HTML form with a text field and submit button
String formHtml = "<h2>Registration Form</h2>" +
"<form>" +
"<label>Name: <input type='text' name='fullname' placeholder='Enter name'></label><br>" +
"<button type='submit'>Submit</button>" +
"</form>";
// Render HTML to PDF — form fields are preserved
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(formHtml);
pdf.saveAs("registration-form.pdf");
IronPDF 通過將W3C HTML 表單規範作為真實來源來從 HTML 建立 PDF 表單。 每個標準表單元素 - <input>, <textarea>, <select>, <checkbox> 和 <radio> - 均被保留在輸出的 PDF 中作為互動式字段。 您可以使用 CSS 為這些元素設置樣式,並包含自定義字體以滿足您的品牌需求。
在建立表單時,IronPDF 在生成的 PDF 中保留了 HTML 表單元素的互動性。 使用者可以直接在他們的 PDF 查看器中填寫表單而無需其他軟體。 這些表單與 Adobe Acrobat Reader 和所有普遍部署的 PDF 查看器配合使用,使其能夠被企業和消費者環境輕易存取。 如需進階渲染選項,請參閱PDF 生成設置文件。
提示: IronPDF 無需運行中的瀏覽器或已安裝的 PDF 列印驅動程式。 渲染引擎與程式庫綁定在一起,因此表單在開發機器、CI 伺服器和雲部署上渲染一致。
我如何建立文字輸入和文字區域表單?
文字輸入和文字區域是最常見的 PDF 表單元素 - 適用於收集名字、電子郵件地址、評論和多行描述。 IronPDF 將 <input type="text"> 映射到單行 PDF 文字字段,將 <textarea> 映射到保留其行列尺寸的多行字段。
HTML 表單元素轉換為 PDF 時保持其功能。 使用者可以通過單擊字段輸入文字,並使用 Tab 鍵在輸入框之間移動,就像在任何 Web 表單中一樣。 預設值、佔位符文字和 HTML5 驗證屬性如 required 和 pattern 在轉換過程中會被遵循。 為了細粒度的布局控製,可以將表單元素與自定義紙張尺寸和邊距設置結合使用。
import com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument;// Set the license key for IronPDFLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");// HTML with single-line inputs and a multi-line textareaString htmlContent = """<html> <body> <h2>Editable PDF Form</h2> <form> First name: <br> <input type='text' name='firstname' value=''> <br> Last name: <br> <input type='text' name='lastname' value=''> <br> Address: <br> <textarea name='address' rows='4' cols='50'></textarea> </form> </body></html>""";// Render HTML — both <input> and <textarea> become interactive PDF fieldsPdfDocument pdfDoc = PdfDocument.renderHtmlAsPdf(htmlContent);// Save the documentpdfDoc.saveAs("textAreaAndInputForm.pdf");
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
// Set the license key for IronPDF
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// HTML with single-line inputs and a multi-line textarea
String htmlContent = """
<html>
<body>
<h2>Editable PDF Form</h2>
<form>
First name: <br> <input type='text' name='firstname' value=''> <br>
Last name: <br> <input type='text' name='lastname' value=''> <br>
Address: <br> <textarea name='address' rows='4' cols='50'></textarea>
</form>
</body>
</html>
""";
// Render HTML — both <input> and <textarea> become interactive PDF fields
PdfDocument pdfDoc = PdfDocument.renderHtmlAsPdf(htmlContent);
// Save the document
pdfDoc.saveAs("textAreaAndInputForm.pdf");
Java
renderHtmlAsPdf 調用 PdfDocument 類將 HTML 轉換為完全互動式的 PDF。 每個命名的表單字段成為 PDF AcroForm 字典中的獨立字段,可被任何符合標準的 PDF 查看器閱讀。 您在 name 屬性中設置的字段名稱 (firstname, lastname, address) 在輸出中保持不變,並可在提取表單資料時讀回。
輸出 PDF 文件:
我如何建立複選框和下拉框表單?
複選框和下拉框涵蓋了 PDF 表單中最常見的兩種選擇模式:二進制開/關選擇和從列表中選擇單項。IronPDF 將 <input type="checkbox"> 映射到 PDF 複選框字段,將 <select> 元素映射到 PDF 下拉框字段。
下拉框字段允許使用者從由 <option> 元素定義的固定選項列表中進行選擇。 您可以通過為選項新增 selected 屬性預選擇一個預設值,使用 <optgroup> 將相關選擇分組,並使用 CSS 使下拉框的外觀與您的文件設計匹配。 在 <select> 元素上的 name 屬性成為生成的 PDF 中的字段名稱,這使得表單資料提取變得簡單。 如需如何使用其他工具處理表單,請參閱Apache PDFBox 專案獲取有關 PDF AcroForm 字段如何儲存此選擇資料的詳細資訊。
import com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument;// Set the license key for IronPDFLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");// HTML with a checkbox and a select dropdownString htmlContent = """<html> <body> <h2>Editable PDF Form</h2> <h3>Task Completed</h3> <label> <input type='checkbox' id='taskCompleted' name='taskCompleted'> Mark task as completed </label> <h3>Select Priority</h3> <label for='priority'>Choose priority level:</label> <select id='priority' name='priority'> <option value='high'>High</option> <option value='medium' selected>Medium</option> <option value='low'>Low</option> </select> </body></html>""";// Render HTML — checkbox and combobox fields are preserved in the PDFPdfDocument pdfDoc = PdfDocument.renderHtmlAsPdf(htmlContent);// Save the documentpdfDoc.saveAs("checkboxAndComboboxForm.pdf");
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
// Set the license key for IronPDF
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// HTML with a checkbox and a select dropdown
String htmlContent = """
<html>
<body>
<h2>Editable PDF Form</h2>
<h3>Task Completed</h3>
<label>
<input type='checkbox' id='taskCompleted' name='taskCompleted'> Mark task as completed
</label>
<h3>Select Priority</h3>
<label for='priority'>Choose priority level:</label>
<select id='priority' name='priority'>
<option value='high'>High</option>
<option value='medium' selected>Medium</option>
<option value='low'>Low</option>
</select>
</body>
</html>
""";
// Render HTML — checkbox and combobox fields are preserved in the PDF
PdfDocument pdfDoc = PdfDocument.renderHtmlAsPdf(htmlContent);
// Save the document
pdfDoc.saveAs("checkboxAndComboboxForm.pdf");
Java
複選框字段儲存一個布林狀態(在 PDF 表示法中是 /On 或 /Off),下游程式碼在處理提交的表單時可以讀取該狀態。 下拉框儲存所選擇的 <option> 的 value 屬性。 這兩種型別的字段在保存的 PDF 中保持互動性 - 使用者可以勾選或更改下拉選擇而無需任何額外的插件或 JavaScript。
輸出 PDF 文件:
我如何建立單選按鈕表單?
單選按鈕提供了互斥選擇:一旦使用者選擇了組中的一個選項,將自動取消選擇該組中先前選擇的選項。 IronPDF 將共享 name 屬性的 <input type="radio"> 元素映射到 PDF AcroForm 字典中的單個組合單選字段。
組中的所有單選按鈕必須共享相同的 name 屬性值 - 這就是在 PDF 中連接它們的原因。 每個按鈕的 value 屬性決定選擇該選項時字段儲存什麼內容。 如果組中的空按鈕被選中,則字段值為 None。 您可以使用載入的 PdfDocument 上的 getForm() 和 getFields() 方法讀取當前選擇,這讓您可以構建預填表單或驗證提交資料的工作流。 有關讀取表單字段的實例,請參閱PDF 表單資料範例。
import com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument;// Set the license key for IronPDFLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");// HTML with a radio button group — all three share name='traveltype'String htmlContent = """<html> <body> <h2>Editable PDF Form</h2> Choose your preferred travel type: <br> <input type='radio' name='traveltype' value='Bike'> Bike <br> <input type='radio' name='traveltype' value='Car'> Car <br> <input type='radio' name='traveltype' value='Airplane'> Airplane </body></html>""";// Render HTML — all three radio inputs become one grouped field in the PDFPdfDocument pdfDoc = PdfDocument.renderHtmlAsPdf(htmlContent);// Save the documentpdfDoc.saveAs("radioButtonForm.pdf");
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
// Set the license key for IronPDF
License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");
// HTML with a radio button group — all three share name='traveltype'
String htmlContent = """
<html>
<body>
<h2>Editable PDF Form</h2>
Choose your preferred travel type: <br>
<input type='radio' name='traveltype' value='Bike'> Bike <br>
<input type='radio' name='traveltype' value='Car'> Car <br>
<input type='radio' name='traveltype' value='Airplane'> Airplane
</body>
</html>
""";
// Render HTML — all three radio inputs become one grouped field in the PDF
PdfDocument pdfDoc = PdfDocument.renderHtmlAsPdf(htmlContent);
// Save the document
pdfDoc.saveAs("radioButtonForm.pdf");
Java
由於三個 <input type="radio"> 元素共享 name="traveltype" 屬性,IronPDF 在輸出 PDF 中建立一個單個 AcroForm 單選組。 例如,在 Acrobat Reader 中選擇"Car",將字段值設置為 Car 並清除任何之前的選擇。 此行為反映了使用者對 Web 表單中單選按鈕組的預期,使得從 Web 到 PDF 資料收集的過渡自然。
IronPDF for Java 從標準 HTML 建立 PDF 表單。撰寫包含 <input>、<textarea> 或 <select> 元素的 HTML 字串,將其傳遞給 PdfDocument.renderHtmlAsPdf(),然後保存結果。每個命名的欄位在輸出 PDF 中成為互動式 AcroForm 欄位。
IronPDF 在 Java 中支援哪些型別的表單欄位?
IronPDF 將所有標準 HTML 表單元素對應到其 PDF 等效元素:<input type='text'> 變成單行文字欄位,<textarea> 變成多行文字欄位,<input type='checkbox'> 變成核取方塊欄位,<select> 變成組合方塊,並且以相同 name 屬性分組的 <input type='radio'> 變成單選按鈕群組。
我需要 Adobe Acrobat 來在 Java 中建立可填寫的 PDF 嗎?
不需要。IronPDF 包含其自己的渲染引擎,因此不需要桌面 PDF 應用程式。它產生的可填寫 PDF 與 Adobe Acrobat Reader、Foxit Reader 和所有主要的 PDF 檢視器相容。
我可以用 CSS 來在 Java 中設計 PDF 表單嗎?
可以。因為 IronPDF 將 HTML 渲染為 PDF,您可以使用 CSS 控制欄位大小、字體、顏色和佈局。透過 <link> 標籤載入的自訂網頁字體也會嵌入到輸出 PDF 中。
可以。使用 PdfDocument.fromFile() 載入填寫的 PDF,然後呼叫 getForm().getFields() 迭代所有 AcroForm 欄位並讀取其當前值。欄位名稱對應到您在原始 HTML 中設定的 name 屬性。
What are some best practices for creating PDF forms using IronPDF?
Best practices include assigning `name` attributes to every form element, testing forms on multiple PDF viewers, using HTML5 validation attributes, and securely managing your license key for deployments.
How does IronPDF ensure consistency in PDF form rendering?
IronPDF's rendering engine comes bundled with the library, ensuring that forms render identically across all environments, including development machines, CI servers, and cloud deployments.
What can IronPDF's advanced rendering settings control?
Advanced rendering settings in IronPDF allow you to control paper size, margins, headers, and footers to match specific document requirements, enhancing flexibility in PDF creation.
What is a common issue observed when converting forms with IronPDF and how can it be resolved?
A common issue is losing interactivity in the PDF form due to missing `name` attributes on elements. Always include a `name` so IronPDF can correctly identify each form field in the resulting PDF.