วิธีการทํางานกับแบบฟอร์มที่ปฏิกิริยาใน Java
คู่มือนี้แสดงวิธีการสร้างและจัดการสนาม AcroForm ในเอกสาร PDF โดยใช้ Aspose.PDF FOSS สําหรับ Java คุณจะได้เรียนรู้เพิ่มปุ่มวิทยุ, ตกรอบเช็ค และสนานข้อความ และอ่านค่าสน่ําฟอร์มที่ส่งมา.
สร้างกลุ่มปุ่มวิทยุ
RadioButtonField ตัวแทนกลุ่มของตัวเลือกที่แยกกันและกัน แต่ละตัวอย่างคือ เพิ่มด้วยเครื่องหมาย และ Rectangle ตําแหน่งกรอบ. วางค่าเริ่มต้นโดยการเรียก setValue() มีเครื่องหมายทางเลือกหนึ่ง:
try (Document doc = new Document()) {
PageCollection pages = doc.getPages();
Page page = pages.add();
RadioButtonField radio = new RadioButtonField(page);
radio.setPartialName("choice");
radio.addOption("Yes", new Rectangle(50, 50, 70, 70));
radio.addOption("No", new Rectangle(50, 80, 70, 100));
doc.getForm().add(radio, 1);
radio.setValue("Yes");
doc.save("form.pdf");
}เพิ่มช่องเช็ค
A การทําการ CheckboxField เป็นสนาม boolean ตัวเดียว Set setChecked(true) เพื่อทํามัน การตรวจสอบโดยตั้งค่า สายการเพิ่มเติมเข้าฟอร์มที่หมายเลขหน้าเฉพาะเจาะจง (1-อัตรานุกรรม):
try (Document doc = new Document()) {
PageCollection pages = doc.getPages();
Page page = pages.add();
CheckboxField cb = new CheckboxField(page, new Rectangle(50, 50, 70, 70));
cb.setPartialName("agree");
cb.setChecked(true);
doc.getForm().add(cb, 1);
doc.save("form.pdf");
}เพิ่มสนามข้อความ
TextBoxField รับการใส่ข้อความของผู้ใช้ Set a default value with 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("Enter your name");
doc.getForm().add(text, 1);
doc.save("form.pdf");
}รับค่าที่เลือก
หลังจากการอัตราบรรจุแบบฟอร์ม, ค้นหาค่าปัจจุบันของสนามโดยชื่อส่วนของมัน. แทงสนานไปยังชนิดที่คอนกรีตของเขาเพื่อเรียกวิธีเฉพาะประเภท:
try (Document doc = new Document("form.pdf")) {
RadioButtonField radio = (RadioButtonField) doc.getForm().get("choice");
String selected = radio.getValue();
System.out.println("Selected: " + selected);
}