แนวข้อสัมพันธ์
คู่มือนี้แสดงวิธีการสร้างและจัดการสนาม AcroForm ในเอกสาร PDF โดยใช้ Aspose.PDF FOSS for Java คุณจะเรียนรู้เพิ่มปุ่มวิทยุ, ตกรอบเช็ค, สนามข้อความ, กล่องคอมโบ และกลองรายชื่อ และเรียกคืนค่าสนานที่ส่งมา.
API AcroForm
การ Form กลุ่มที่สามารถเข้าถึงได้ผ่านทาง Document.getForm(), จัดการทุกสนาม AcroForm ใน เอกสาร สายการเพิ่มโดยเรียก Form.add(field, pageNumber).
RadioButtonField
สร้างกลุ่มปุ่มวิทยุ โดยแต่ละตัวเลือกเป็นของตัวเอง Rectangle.ทุกทางเลือกคือ เพิ่มด้วยเครื่องหมาย และกล่องกําหนด setValue() การเลือกตัวเลือกก่อนหน้า:
try (Document doc = new Document()) {
PageCollection pages = doc.getPages();
Page page = pages.add();
RadioButtonField radio = new RadioButtonField(page);
radio.setPartialName("color");
radio.addOption("Red", new Rectangle(50, 50, 70, 70));
radio.addOption("Blue", new Rectangle(50, 80, 70, 100));
doc.getForm().add(radio, 1);
radio.setValue("Red");
doc.save("form.pdf");
}setValue() เลือกตัวเลือกโดยโปรแกรม. getValue() กลับตัวเลขปัจจุบัน ชื่อตัวเลือกที่เลือกลูก.
CheckboxField
CheckboxField แสดงถึงสนามการเข้า boolean เดียวในแบบ PDF Set setChecked(true) เพิ่มมันเข้าฟอร์มที่ a เลขหน้าเฉพาะ (อัตรา 1 อินเด็กซ์):
try (Document doc = new Document()) {
PageCollection pages = doc.getPages();
Page page = pages.add();
CheckboxField cb = new CheckboxField(page, new Rectangle(100, 100, 120, 120));
cb.setPartialName("agree");
cb.setChecked(true);
doc.getForm().add(cb, 1);
doc.save("form.pdf");
}TextBoxField
TextBoxField รับการใส่ข้อความแบบฟรี แนะค่าตั้งโดย default ด้วย setValue() และ ลงฟอร์มในสนามก่อนการบันทึก:
try (Document doc = new Document()) {
PageCollection pages = doc.getPages();
Page page = pages.add();
TextBoxField text = new TextBoxField(page, new Rectangle(50, 200, 250, 220));
text.setPartialName("name");
text.setValue("Default text");
doc.getForm().add(text, 1);
doc.save("form.pdf");
}ComboBoxField และ ListBoxFeld
ComboBoxField และ ListBoxField ทั้งสองรับรองว่ามีการเลือกตัวอย่างที่สามารถเลือกได้ ตัวเลือกสตริงผ่าน addOption() และเปิดเผย getValue() เพื่อหาตัวเลือกได้.
FormEditor
FormEditor เป็นคลาสหน้าต่างสําหรับการทํางานกับสนาม AcroForm. บิดเอกสาร มี bindPdf(), ใช้การเปลี่ยนแปลง, จากนั้นเรียก save().
การอ่านค่าสนาม
หลังจากการอัตราบรรจุแบบฟอร์ม, รวบรวมค่าปัจจุบันของสนามที่ได้รับชื่อโดยเรียก doc.getForm().get(name) และการโยนแบบสนามคอนกรีต:
try (Document doc = new Document("form.pdf")) {
RadioButtonField radio = (RadioButtonField) doc.getForm().get("color");
String selected = radio.getValue();
System.out.println("Selected: " + selected);
}