Example usage for com.lowagie.text.pdf PdfReader PdfReader

List of usage examples for com.lowagie.text.pdf PdfReader PdfReader

Introduction

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

Prototype

public PdfReader(PdfReader reader) 

Source Link

Document

Creates an independent duplicate.

Usage

From source file:questions.forms.FillEnabledFormRemoveEnabling.java

public static void main(String[] args) {
    try {//from  w  ww.  ja v  a2  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.GetTextFields.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    try {/*from   w w w.ja  v  a2 s.  c om*/
        PrintStream out = new PrintStream(new File(RESULT));

        PdfLister lister = new PdfLister(out);

        PdfReader reader = new PdfReader(RESOURCE);

        PdfDictionary root = reader.getCatalog();
        PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM);

        out.println("These are the form's font dictionaries:");
        PdfDictionary fonts = acroform.getAsDict(PdfName.DR).getAsDict(PdfName.FONT);
        for (PdfName key : (Set<PdfName>) fonts.getKeys()) {
            lister.listDict((PdfDictionary) PdfReader.getPdfObject(fonts.get(key)));
        }
        out.println("--------------");

        out.println("This is the default appearance for the complete form:");
        lister.listAnyObject(PdfReader.getPdfObject(acroform.get(PdfName.DA)));
        out.println("--------------");

        AcroFields form = reader.getAcroFields();
        Map<String, Item> fields = form.getFields();
        Item item;
        for (String name : fields.keySet()) {
            out.println(name);
            if (form.getFieldType(name) == AcroFields.FIELD_TYPE_TEXT) {
                item = form.getFieldItem(name);
                PdfDictionary dict = (PdfDictionary) item.getMerged(0);

                out.println("This is the field's font dictionary:");
                fonts = dict.getAsDict(PdfName.DR).getAsDict(PdfName.FONT);
                for (PdfName key : (Set<PdfName>) fonts.getKeys()) {
                    lister.listDict((PdfDictionary) PdfReader.getPdfObject(fonts.get(key)));
                }
                out.println("---");
                out.println("This is the field's default appearance");
                lister.listAnyObject(PdfReader.getPdfObject(dict.get(PdfName.DA)));
            } else {
                out.println("NOT A TEXT FIELD!");
            }
            out.println("--------------");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:questions.forms.KidsOnDifferentPages.java

public static void fillPdf() {
    try {/*from www  .j av a 2s .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.MultipleChoice.java

public static void readList() throws IOException {
    PdfReader reader = new PdfReader(RESULT);
    AcroFields form = reader.getAcroFields();
    String[] selection = form.getListSelection("iText");
    for (int i = 0; i < selection.length; i++) {
        System.out.println(selection[i]);
    }//from ww w .j a  v a2  s .  c  o m
}

From source file:questions.forms.MultipleChoice2.java

public static void main(final String[] args) throws IOException, DocumentException {
    createPdf();//from  w w w.  j a v a  2  s . c  o  m
    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.ReadXfa.java

public static void main(String[] args) {
    try {/*from   w  w w .j a v a2s  .c  o m*/
        PdfReader reader = new PdfReader(RESOURCE);
        FileOutputStream os = new FileOutputStream(RESULT);
        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) {
            System.out.println("Reading: " + xfa.getAsString(i));
            PRStream s = (PRStream) xfa.getAsStream(i + 1);
            os.write(PdfReader.getStreamBytes(s));
        }
        os.flush();
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:questions.forms.ReadXfa2.java

public static void main(String[] args) {
    try {//from w ww .  j  a v a 2s.c  o m
        PdfReader reader = new PdfReader(RESOURCE);
        FileOutputStream os = new FileOutputStream(RESULT);
        XfaForm xfa = new XfaForm(reader);
        Document doc = xfa.getDomDocument();
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        tf.transform(new DOMSource(doc), new StreamResult(os));
        reader.close();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }
}

From source file:questions.forms.RemoveXfa.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    try {/*  ww w  . java2 s.co  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.images.ResizeImage.java

public static void main(String[] args) throws IOException, DocumentException {
    createPdf(ORIGINAL);// w  w  w . j av  a 2  s . c o m
    PdfReader reader = new PdfReader(ORIGINAL);
    resizeImagesInPage(reader, 1, 0.5f);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
    stamper.close();
}

From source file:questions.importpages.ConcatenateMakeTOC.java

public static void main(String[] args) {
    Document document = new Document();
    try {//from w  w  w . j ava 2 s .c  o m
        PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT));
        copy.setViewerPreferences(PdfWriter.PageModeUseOutlines);
        document.open();
        List<HashMap<String, Object>> bookmarks = new ArrayList<HashMap<String, Object>>();
        List<HashMap<String, Object>> kids;
        PdfReader reader;
        int page = 1;
        for (int i = 0; i < 3; i++) {
            createPdf(i);
            HashMap<String, Object> titlepage = new HashMap<String, Object>();
            titlepage.put("Title", String.format("Document %d", i + 1));
            titlepage.put("Action", "GoTo");
            titlepage.put("Page", String.format("%d Fit", page));
            kids = new ArrayList<HashMap<String, Object>>();
            reader = new PdfReader(RESULTS[i]);
            for (int j = 1; j <= reader.getNumberOfPages(); j++) {
                copy.addPage(copy.getImportedPage(reader, j));
                HashMap<String, Object> kid = new HashMap<String, Object>();
                kid.put("Title", String.format("Page %d in document %d", j, i + 1));
                kid.put("Action", "GoTo");
                kid.put("Page", String.format("%d FitH 806", page));
                kids.add(kid);
                page++;
            }
            titlepage.put("Kids", kids);
            bookmarks.add(titlepage);
        }
        copy.setOutlines(bookmarks);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    document.close();
}