كيفية العمل مع النماذج التفاعلية في جافا
تظهر هذه الدورة كيفية إنشاء وإدارة حقول AcroForm في مستندات PDF باستخدام Aspose.PDF FOSS for 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");
}إضافة مربع اختبار
(أ) CheckboxField هو حقل بولي واحد. 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 يقبل إدخال النص من المستخدم. حدد قيمة افتراضية مع 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);
}