Example usage for com.lowagie.text.pdf PdfStamper close

List of usage examples for com.lowagie.text.pdf PdfStamper close

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfStamper close.

Prototype

public void close() throws DocumentException, IOException 

Source Link

Document

Closes the document.

Usage

From source file:questions.forms.AddActionToField.java

public static void main(String[] args) {
    try {//from   w ww  . j a v  a  2  s . c  o  m
        PdfReader reader = new PdfReader(RESOURCE);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        AcroFields form = stamper.getAcroFields();
        Item fd = form.getFieldItem("Who");
        PdfDictionary dict = (PdfDictionary) PdfReader.getPdfObject((PdfObject) fd.getWidgetRef(0));
        PdfDictionary aa = dict.getAsDict(PdfName.AA);
        if (aa == null)
            aa = new PdfDictionary();
        aa.put(new PdfName("Fo"),
                PdfAction.javaScript("app.alert('Who has got the focus!?');", stamper.getWriter()));
        dict.put(PdfName.AA, aa);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:questions.forms.AddFieldToExistingForm.java

public static void main(String[] args) {
    PdfReader reader;/*from   w w  w.  ja va  2  s . c om*/
    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 {//  w  w w  . j a  v a 2  s  .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.FillDynamicXfa.java

public static void main(String[] args) {
    try {//from  w  w  w . ja va2 s . c om
        PdfReader reader = new PdfReader(RESOURCE_PDF);
        File file = new File(RESOURCE_DATA);
        byte[] data = new byte[(int) file.length()];
        FileInputStream is = new FileInputStream(file);
        int offset = 0;
        int numRead = 0;
        int datalength = data.length;
        while (offset < datalength && (numRead = is.read(data, offset, datalength - offset)) >= 0) {
            offset += numRead;
        }
        PdfDictionary root = reader.getCatalog();
        PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM);
        PdfArray xfa = acroform.getAsArray(PdfName.XFA);
        for (int i = 0; i < xfa.size(); i += 2) {
            if ("datasets".equals(xfa.getAsString(i).toString())) {
                PRStream s = (PRStream) xfa.getAsStream(i + 1);
                s.setData(data);
            }
        }
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:questions.forms.FillDynamicXfa2.java

public static void main(String[] args) {
    try {/*from w w w .j  av a 2s . c om*/
        // getting new data from a "datasets" XML snippet
        File file = new File(RESOURCE_DATA);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document newdoc = db.parse(file);
        Element element = newdoc.getDocumentElement();
        NodeList nodelist = element.getElementsByTagNameNS("http://www.xfa.org/schema/xfa-data/1.0/", "data");
        Node newdata = nodelist.item(0);

        // replacing the XFA in an existing document
        PdfReader reader = new PdfReader(RESOURCE_PDF);
        XfaForm xfa = new XfaForm(reader);
        Document doc = xfa.getDomDocument();
        NodeList list = doc.getElementsByTagNameNS("http://www.xfa.org/schema/xfa-data/1.0/", "datasets");
        list.item(0).replaceChild(doc.importNode(newdata, true), list.item(0).getFirstChild());
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        xfa.setDomDocument(doc);
        xfa.setChanged(true);
        XfaForm.setXfa(xfa, stamper.getReader(), stamper.getWriter());
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }
}

From source file:questions.forms.FillEnabledForm.java

public static void main(String[] args) {
    try {/*w w  w . j  a  v a2s . com*/
        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 {/*from w w w.java2s .  c o 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 {//  www  . j a v  a 2s .  com
        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  .ja v a 2  s.co 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();/*from w w  w  . ja v  a2 s. 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();
}