List of usage examples for com.lowagie.text.pdf PdfStamper getAcroFields
public AcroFields getAcroFields()
AcroFields
object that allows to get and set field values and to merge FDF forms. From source file:questions.forms.AddFieldToExistingForm.java
public static void main(String[] args) { PdfReader reader;/*from w w w .j av a 2s.co m*/ try { reader = new PdfReader(FORM); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); TextField tf = new TextField(stamper.getWriter(), new Rectangle(100, 760, 400, 785), "added_field"); tf.setText("\u00e4\u00f4\u00df"); tf.setOptions(TextField.READ_ONLY); stamper.addAnnotation(tf.getTextField(), 1); AcroFields form = stamper.getAcroFields(); form.setField("Who", "\u00e4\u00f3\u00df\u00f4"); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.forms.ChangeTextFieldAlignment.java
public static void main(String[] args) { try {//www . j a va2s . c o m PdfReader reader = new PdfReader(RESOURCE); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); AcroFields form = stamper.getAcroFields(); PdfDictionary dict = form.getFieldItem("Who").getMerged(0); dict.put(PdfName.Q, new PdfNumber(1)); form.setField("Who", "Center of the World"); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.forms.FillEnabledForm.java
public static void main(String[] args) { try {/*from ww w. j ava2s . c om*/ PdfReader reader = new PdfReader(ENABLED_FORM); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT), '\0', true); AcroFields form = stamper.getAcroFields(); form.setField("form1[0].#subform[0].Body[0].EmployeeName[0]", "Bruno Lowagie"); form.setField("form1[0].#subform[0].Body[0].Address[0]", "Ad. Baeyensstraat 121"); form.setField("form1[0].#subform[0].Body[0].ZipCode[0]", "9040"); form.setField("form1[0].#subform[0].Body[0].Comments[0]", "The example FillEnabledForm shows how to prefill a Reader Enabled form preserving the user permissions."); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.forms.FillEnabledFormBreakEnabling.java
public static void main(String[] args) { try {// w w w . j a v a 2 s .co m PdfReader reader = new PdfReader(ENABLED_FORM); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); AcroFields form = stamper.getAcroFields(); form.setField("form1[0].#subform[0].Body[0].EmployeeName[0]", "Bruno Lowagie"); form.setField("form1[0].#subform[0].Body[0].Address[0]", "Ad. Baeyensstraat 121"); form.setField("form1[0].#subform[0].Body[0].ZipCode[0]", "9040"); form.setField("form1[0].#subform[0].Body[0].Comments[0]", "The example FillEnabledForm shows how NOT to prefill a Reader Enabled form. " + "Using this code snippet will lead to a Reader warning saying: \"This document " + "contained certain rights to enable special features in Adobe Reader. The document " + "has been changed since it was created and these rights are no longer valid. Please " + "contact the author for the original version of this document.\" You could also " + "search the examples on http://1t3xt.info/examples/ to find out the correct way " + "to fill a Reader Enabled form using iText."); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.forms.FillEnabledFormRemoveEnabling.java
public static void main(String[] args) { try {//from w ww . j a v a 2 s . c om PdfReader reader = new PdfReader(ENABLED_FORM); reader.removeUsageRights(); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); AcroFields form = stamper.getAcroFields(); form.setField("form1[0].#subform[0].Body[0].EmployeeName[0]", "Bruno Lowagie"); form.setField("form1[0].#subform[0].Body[0].Address[0]", "Ad. Baeyensstraat 121"); form.setField("form1[0].#subform[0].Body[0].ZipCode[0]", "9040"); form.setField("form1[0].#subform[0].Body[0].Comments[0]", "The example FillEnabledForm shows how to prefill a Reader Enabled form WITHOUT preserving the usage rights." + " If you want to preserve the usage rights, look for the example FillEnabledForm."); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.forms.KidsOnDifferentPages.java
public static void fillPdf() { try {//from w ww . jav a 2 s.c o m PdfReader reader; PdfStamper stamper; reader = new PdfReader(FORM); stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); AcroFields form = stamper.getAcroFields(); form.setField("person.name1", "hello"); form.setField("person.name2", "world"); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.forms.MultipleChoice2.java
public static void main(final String[] args) throws IOException, DocumentException { createPdf();/* w w w .java 2s. c om*/ PdfReader reader = new PdfReader(RESULT); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT2)); AcroFields form = stamper.getAcroFields(); form.setListSelection("iText", new String[] { "C", "PHP" }); stamper.close(); }
From source file:questions.forms.RemoveXfa.java
@SuppressWarnings("unchecked") public static void main(String[] args) { try {/*from ww w.j a v a 2 s .c o m*/ PdfReader reader = new PdfReader(RESOURCE); PdfDictionary root = reader.getCatalog(); PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM); acroform.remove(PdfName.XFA); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); AcroFields form = stamper.getAcroFields(); Map<String, Item> fields = form.getFields(); for (String field : fields.keySet()) { System.out.println(field); form.setField(field, "value"); } stamper.partialFormFlattening("topmostSubform[0].Page1[0].SN_NUMBER[0]"); stamper.setFormFlattening(true); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.javascript.AddJavaScriptToForm.java
public static void addJavaScript(String input, String output) throws IOException, DocumentException { PdfReader reader = new PdfReader(input); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(output)); stamper.getWriter()//from ww w. j a va 2s. c o m .addJavaScript("function setReadOnly(readonly) {" + "var partner = this.getField('partner');" + "if(readonly) {" + "partner.value = '';" + "}" + "partner.readonly = readonly;" + " }" + "function validate() {" + "var married = this.getField('married');" + "var partner = this.getField('partner');" + "if (married.value == 'yes' && partner.value == '') {" + "app.alert('please enter the name of your partner');" + "}" + "else {" + "this.submitForm({" + " cURL:\"http://1t3xt.info/examples/request.php\"," + " cSubmitAs: \"HTML\"" + "});" + "}" + " }"); AcroFields form = stamper.getAcroFields(); Item fd = form.getFieldItem("married"); PdfDictionary dictYes = (PdfDictionary) PdfReader.getPdfObject((PdfObject) fd.getWidgetRef(0)); PdfDictionary yesAction = dictYes.getAsDict(PdfName.AA); if (yesAction == null) yesAction = new PdfDictionary(); yesAction.put(new PdfName("Fo"), PdfAction.javaScript("setReadOnly(false);", stamper.getWriter())); dictYes.put(PdfName.AA, yesAction); PdfDictionary dictNo = (PdfDictionary) PdfReader.getPdfObject((PdfObject) fd.getWidgetRef(1)); PdfDictionary noAction = dictNo.getAsDict(PdfName.AA); if (noAction == null) noAction = new PdfDictionary(); noAction.put(new PdfName("Fo"), PdfAction.javaScript("setReadOnly(true);", stamper.getWriter())); dictNo.put(PdfName.AA, noAction); PdfWriter writer = stamper.getWriter(); PushbuttonField button = new PushbuttonField(writer, new Rectangle(40, 690, 200, 710), "submit"); button.setText("validate and submit"); button.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT); PdfFormField validateAndSubmit = button.getField(); validateAndSubmit.setAction(PdfAction.javaScript("validate();", stamper.getWriter())); stamper.addAnnotation(validateAndSubmit, 1); stamper.close(); }
From source file:questions.javascript.RemoveJavaScript.java
public static void main(String[] args) throws DocumentException, IOException { // creating the form with JS AddJavaScriptToForm.main(args);/*from ww w . j a va2s . co m*/ // removing the document level JS PdfReader reader = new PdfReader(AddJavaScriptToForm.RESULT); PdfDictionary root = reader.getCatalog(); PdfDictionary names = root.getAsDict(PdfName.NAMES); names.remove(PdfName.JAVASCRIPT); if (names.size() == 0) { root.remove(PdfName.NAMES); } reader.removeUnusedObjects(); // filling out and flattening the form // (if you don't flatten, you'll get JS errors!) PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); AcroFields form = stamper.getAcroFields(); form.setField("married", "no"); stamper.setFormFlattening(true); stamper.close(); }