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

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

Introduction

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

Prototype

public PdfStamper(PdfReader reader, OutputStream os) throws DocumentException, IOException 

Source Link

Document

Starts the process of adding extra content to an existing PDF document.

Usage

From source file:org.squale.welcom.outils.pdf.WPdfUtil.java

License:Open Source License

/**
 * Ajoute dans les parametres du report l'impression au chargement du document Utilise itext.jar
 * /*ww  w .  j  a v  a  2s.  c o m*/
 * @param report : report
 * @param out : flux pour ecrire
 * @throws DocumentException : Probleme a la lecture du document
 * @throws IOException : Probleme a l'ecriture dans la stream
 */
public static void autoPrint(final byte report[], final OutputStream out)
        throws DocumentException, IOException {
    final PdfStamper stamper = new PdfStamper(new PdfReader(report), out);

    stamper.addJavaScript("this.print(false);");

    stamper.close();
}

From source file:org.squale.welcom.outils.pdf.WPdfUtil.java

License:Open Source License

/**
 * Ajoute dans les parametres du report l'impression au chargement du document avec choix pour l'imprimante Utilise
 * itext.jar/* ww w  .ja  v  a  2 s.  co  m*/
 * 
 * @param report : report
 * @param out : flux pour ecrire
 * @throws DocumentException : Probleme a la lecture du document
 * @throws IOException : Probleme a l'ecriture dans la stream
 */
public static void autoPrintPopup(final byte report[], final OutputStream out)
        throws DocumentException, IOException {
    final PdfStamper stamper = new PdfStamper(new PdfReader(report), out);

    stamper.addJavaScript("this.print(true);");

    stamper.close();
}

From source file:oscar.form.pharmaForms.formBPMH.pdf.PDFController.java

License:Open Source License

/**
 * Writes data from an object to a PDF with matching smart tags.
 * PDF should be preset with instantiation of this class.
 * /*from  w w  w  .j  av a2  s  .  com*/
 * See test class with same name for example.
 * 
 * @param Data : object or bean containing data to stamp.
 * @param Pages : page numbers to stamp data onto
 * @param outPath : outPut path for completed PDF.
 * @param pdfPath : the absolute path to an editable pdf template. 
 */
public void writeDataToPDF(String pdfPath, String outPath, Object data, String[] pages, String fileId) {

    if (pages.length <= 0) {
        _Logger.error("No page numbers provided.");
        return;
    }
    if (data == null) {
        _Logger.error("No data object provided.");
        return;
    } else {
        setDataObject(data);
    }

    if ((pdfPath != null) && (!pdfPath.isEmpty())) {
        setFilePath(pdfPath);
        _init();
    }

    if ((outPath != null) && (!outPath.isEmpty())) {
        setOutputPath(outPath);
    }

    if (getReader() != null) {

        getReader().selectPages(arrayToString(pages, ","));

        try {

            if (!getOutputPath().endsWith("/")) {
                setOutputPath(getOutputPath() + "/");
            }

            setOutputPath(
                    getOutputPath() + fileId + "_" + new Date().getTime() + "_" + getFilePath().getName());

            setFileName(new File(getOutputPath()).getName());

            if (getStamper() == null) {
                setStamper(new PdfStamper(getReader(), new FileOutputStream(getOutputPath())));
            }

            addDataToPDF();

            getStamper().getWriter().addJavaScript("this.print({bUI: true, bSilent: true, bShrinkToFit:true});",
                    false);
            getStamper().getWriter().addJavaScript("this.closeDoc(true);");

            getStamper().setFreeTextFlattening(true);
            getStamper().setFormFlattening(true);
            getStamper().setFreeTextFlattening(true);

        } catch (FileNotFoundException e1) {
            _Logger.log(Level.FATAL, null, e1);
        } catch (DocumentException e1) {
            _Logger.log(Level.FATAL, null, e1);
        } catch (IOException e1) {
            _Logger.log(Level.FATAL, null, e1);
        } finally {
            if (getStamper() != null) {
                try {
                    getStamper().close();
                } catch (DocumentException e) {
                    _Logger.log(Level.FATAL, null, e);
                } catch (IOException e) {
                    _Logger.log(Level.FATAL, null, e);
                }
            }
        }

    } else {
        _Logger.warn("Unable to load document from" + filePath);
    }

}

From source file:papertoolkit.render.sheets.PDFSheetRenderer.java

License:BSD License

/**
 * Uses the iText package to render a PDF file. iText is nice because we can write to a
 * Graphics2D context. Alternatively, we can use PDF-like commands.
 * //w  ww . j av a2  s.c o  m
 * @param destPDFFile
 */
public void renderToPDF(File destPDFFile) {
    try {
        final FileOutputStream fileOutputStream = new FileOutputStream(destPDFFile);

        final PdfReader reader = pdfSheet.getReader();
        DebugUtils.println("NumPages in Existing PDF: " + reader.getNumberOfPages());

        // allows us to stamp on top of an existing PDF
        final PdfStamper stamp = new PdfStamper(reader, fileOutputStream);

        // change the content on top of page 1
        // bottom layer for regions
        final PdfContentByte topLayer = stamp.getOverContent(1 /* page number */);
        final PdfContentByte bottomLayer = stamp.getUnderContent(1);
        renderToPDFContentLayers(destPDFFile, topLayer, bottomLayer);
        stamp.close();

        // save the pattern info to the same directory automatically
        savePatternInformation(); // do this automatically
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:pdflicense.PdfLicenseManager.java

License:Open Source License

private void writeXmp(PdfReader reader, FileOutputStream fos, byte[] xmpBytes)
        throws DocumentException, IOException {
    PdfStamper stamp = new PdfStamper(reader, fos);

    stamp.setXmpMetadata(xmpBytes);//from ww  w.j  a  v a  2 s.  c  o m
    stamp.close();
}

From source file:questions.forms.AddActionToField.java

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