IRONSOFTWAREHOME
PDF 表單
from ironpdf import *

# Step 1.  Creating a PDF with editable forms from HTML using form and input tags
# Radio Button and Checkbox can also be implemented with input type 'radio' and 'checkbox'
form_html = """
    <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>
              <br>
              <p>Please specify your gender:</p>
              <input type='radio' id='female' name='gender' value= 'Female'>
                <label for='female'>Female</label> <br>
                <br>
              <input type='radio' id='male' name='gender' value='Male'>
                <label for='male'>Male</label> <br>
                <br>
              <input type='radio' id='non-binary/other' name='gender' value='Non-Binary / Other'>
                <label for='non-binary/other'>Non-Binary / Other</label>
              <br>
 
              <p>Please select all medical conditions that apply:</p>
              <input type='checkbox' id='condition1' name='Hypertension' value='Hypertension'>
              <label for='condition1'> Hypertension</label><br>
              <input type='checkbox' id='condition2' name='Heart Disease' value='Heart Disease'>
              <label for='condition2'> Heart Disease</label><br>
              <input type='checkbox' id='condition3' name='Stoke' value='Stoke'>
              <label for='condition3'> Stoke</label><br>
              <input type='checkbox' id='condition4' name='Diabetes' value='Diabetes'>
              <label for='condition4'> Diabetes</label><br>
              <input type='checkbox' id='condition5' name='Kidney Disease' value='Kidney Disease'>
              <label for='condition5'> Kidney Disease</label><br>
            </form>
        </body>
    </html>"""

# Instantiate Renderer
renderer = ChromePdfRenderer()
renderer.RenderingOptions.CreatePdfFormsFromHtml = True
renderer.RenderHtmlAsPdf(form_html).SaveAs("BasicForm.pdf")

# Step 2. Reading and Writing PDF form values.
form_document = PdfDocument.FromFile("BasicForm.pdf")

# Set and Read the value of the "firstname" field
first_name_field = form_document.Form.FindFormField("firstname")
first_name_field.Value = "Minnie"
print("FirstNameField value: {}".format(first_name_field.Value))

# Set and Read the value of the "lastname" field
last_name_field = form_document.Form.FindFormField("lastname")
last_name_field.Value = "Mouse"
print("LastNameField value: {}".format(last_name_field.Value))

form_document.SaveAs("FilledForm.pdf")
pip install ironpdf
PDF 表單

PDF 表單

IronPDF for Python可以從HTML建立互動式PDF表單,並以程式的方式讀取或寫入現有PDF表單的欄位值。

入門指南

在渲染包含RenderingOptions.CreatePdfFormsFromHtml = True啟用表單建立。 要在現有表單中讀取或寫入欄位值,請載入PDF並使用Form.FindFormField。

理解程式碼

建立表單

  • CreatePdfFormsFromHtml = True:在渲染時將HTML表單元素 (<checkbox> 等) 轉換為互動式PDF表單欄位。
  • RenderHtmlAsPdf(form_html):將HTML表單渲染為帶有互動可填寫欄位的PDF。 SaveAs("BasicForm.pdf")將其寫入磁碟。

讀取和寫入欄位值

  • PdfDocument.FromFile("BasicForm.pdf"):載入保存的表單PDF以進行欄位操作。
  • name屬性值之欄位的表單欄位物件,來自原始HTML。
  • field.Value = "text":以程式的方式設定表單欄位的值。
  • field.Value:讀取表單欄位的當前值。
  • SaveAs("FilledForm.pdf"):將填入所有欄位值的PDF保存。

支持的表單欄位型別

IronPDF支持文字輸入、單選按鈕、複選框、下拉選單 (<select>) 和文字區域。 所有這些都是從相應的HTML元素自動建立的,當CreatePdfFormsFromHtml啟用時。

學習使⽤IronPDF for Python來建立和填寫PDF表單!

準備開始了嗎?

版本:2026.9剛剛發布

Key in blue circle

立即免費取得 30 天試用金鑰。

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

OR
bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
預約您的免費即時演示
Booking Badge

全球數百萬工程師的信賴

Iron Software的客戶標誌
獲取您的無義務諮詢
填寫以下表格或發送電子郵件至sales@ironsoftware.com
您的詳細資訊將始終保密。
全球數百萬工程師的信賴
Iron Software的客戶標誌
立即獲取您的30天試用金鑰。
無需信用卡或帳戶建立
PDF的Python模塊下載
使用pip安裝

版本: 2026.9

  1. 下載並安裝Python 3.7+。
  2. 如果尚未安裝,請從 pypi.org 安裝 pip。
  3. 在終端執行上述命令。
Python PDF模塊
下載模塊

版本: 2026.9

手動安裝到您的專案中

  1. 下載套件
  2. 從終端運行此命令
    pip install ironpdf-2026.9-py37-none-win_amd64.whi

從$999起的授權