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:classroom.newspaper_b.Newspaper08.java

public static void main(String[] args) {
    try {/*  w  ww . j  a v a  2  s  .c om*/
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));

        PdfAnnotation annotation1 = PdfAnnotation.createText(stamper.getWriter(),
                new Rectangle(LLX1, LLY1, URX1, URY1), "Advertisement 1", MESSAGE, false, "Insert");
        PdfAppearance ap = stamper.getOverContent(1).createAppearance(W1, H1);
        ap.setRGBColorStroke(0xFF, 0x00, 0x00);
        ap.setLineWidth(3);
        ap.moveTo(0, 0);
        ap.lineTo(W1, H1);
        ap.moveTo(W1, 0);
        ap.lineTo(0, H1);
        ap.stroke();
        annotation1.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, ap);
        stamper.addAnnotation(annotation1, 1);

        PdfAnnotation annotation2 = PdfAnnotation.createText(stamper.getWriter(),
                new Rectangle(LLX2, LLY2, URX2, URY2), "Advertisement 2", MESSAGE, true, "Insert");
        annotation2.put(PdfName.C, new PdfArray(new float[] { 0, 0, 1 }));
        stamper.addAnnotation(annotation2, 1);

        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:classroom.newspaper_b.Newspaper09.java

public static void main(String[] args) {
    try {/*  w  w  w . j a  v  a 2 s . c  o m*/
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));

        PdfAnnotation annotation1 = PdfAnnotation.createSquareCircle(stamper.getWriter(),
                new Rectangle(LLX1, LLY1, URX1, URY1), MESSAGE, true);
        annotation1.put(PdfName.T, new PdfString("Advertisement 1"));
        annotation1.put(PdfName.C, new PdfArray(new float[] { 1, 0, 0 }));
        stamper.addAnnotation(annotation1, 1);

        PdfAnnotation annotation2 = PdfAnnotation.createText(stamper.getWriter(),
                new Rectangle(LLX2, LLY2, URX2, URY2), "Advertisement 2", MESSAGE, false, null);
        annotation2.put(PdfName.NM, new PdfString("ad2"));
        // the text must be read only, and the annotation set to NOVIEW
        annotation2.put(PdfName.F, new PdfNumber(PdfAnnotation.FLAGS_READONLY | PdfAnnotation.FLAGS_NOVIEW));

        // we create a popup annotation that will define where the rectangle will appear
        PdfAnnotation popup = PdfAnnotation.createPopup(stamper.getWriter(),
                new Rectangle(LLX2 + 50, LLY2 + 120, URX2 - 80, URY2 - 120), null, false);

        // we add a reference to the text annotation to the popup annotation
        popup.put(PdfName.PARENT, annotation2.getIndirectReference());
        // we add a reference to the popup annotation to the text annotation
        annotation2.put(PdfName.POPUP, popup.getIndirectReference());

        // we add both annotations to the writer
        stamper.addAnnotation(annotation2, 1);
        stamper.addAnnotation(popup, 1);

        // the text annotation can't be viewed (it's invisible)
        // we create a widget annotation named mywidget (it's a button field)
        PushbuttonField field = new PushbuttonField(stamper.getWriter(), new Rectangle(LLX2, LLY2, URX2, URY2),
                "button");
        PdfAnnotation widget = field.getField();
        PdfDictionary dict = new PdfDictionary();
        // we write some javascript that makes the popup of the text annotation visible/invisible on mouse enter/exit
        String js1 = "var t = this.getAnnot(this.pageNum, 'ad2'); t.popupOpen = true; var w = this.getField('button'); w.setFocus();";
        PdfAction enter = PdfAction.javaScript(js1, stamper.getWriter());
        dict.put(PdfName.E, enter);
        String js2 = "var t = this.getAnnot(this.pageNum, 'ad2'); t.popupOpen = false;";
        PdfAction exit = PdfAction.javaScript(js2, stamper.getWriter());
        dict.put(PdfName.X, exit);
        // we add the javascript as additional action
        widget.put(PdfName.AA, dict);
        // we add the button field
        stamper.addAnnotation(widget, 1);

        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:classroom.newspaper_b.Newspaper10.java

public static void main(String[] args) {
    try {/*from w  w w  .j a va2s .  co m*/
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        PushbuttonField button;
        Rectangle rect;

        rect = new Rectangle(100, 980, 700, 1000);
        button = new PushbuttonField(stamper.getWriter(), rect, "click");
        button.setBackgroundColor(Color.ORANGE);
        button.setText("Click here to close window");
        button.setLayout(PushbuttonField.LAYOUT_LABEL_ONLY);
        button.setAlignment(Element.ALIGN_RIGHT);
        PdfFormField menubar = button.getField();
        String js = "var f1 = getField('click'); f1.display = display.hidden;"
                + "var f2 = getField('advertisement'); f2.display = display.hidden;";
        menubar.setAction(PdfAction.javaScript(js, stamper.getWriter()));
        stamper.addAnnotation(menubar, 1);

        rect = new Rectangle(100, 500, 700, 980);
        button = new PushbuttonField(stamper.getWriter(), rect, "advertisement");
        button.setBackgroundColor(Color.WHITE);
        button.setBorderColor(Color.ORANGE);
        button.setImage(Image.getInstance(IMG));
        button.setText("Buy the book iText in Action");
        button.setLayout(PushbuttonField.LAYOUT_LABEL_TOP_ICON_BOTTOM);
        PdfFormField advertisement = button.getField();
        advertisement.setAction(new PdfAction("http://www.1t3xt.com/docs/book.php"));
        stamper.addAnnotation(advertisement, 1);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:classroom.newspaper_b.Newspaper11.java

@SuppressWarnings("unchecked")
public static void main(String[] args) {
    try {//from w w  w  . jav a  2  s  . co  m
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        PdfContentByte canvas = stamper.getOverContent(1);
        canvas.setRGBColorFill(0xFF, 0xFF, 0xFF);
        canvas.rectangle(LLX1, LLY1, W1, H1);
        canvas.rectangle(LLX2, LLY2, W2, H2);
        canvas.fill();
        addTextField(stamper, new Rectangle(LLX1, LLY1, URX1, URY1), "field1", 1);
        addTextField(stamper, new Rectangle(LLX2, LLY2, URX2, URY2), "field2", 1);
        stamper.close();

        reader = new PdfReader(RESULT);
        AcroFields fields = reader.getAcroFields();
        Set<String> fieldnames = fields.getFields().keySet();
        for (String fieldname : fieldnames) {
            System.out.print(fieldname);
            System.out.print(": page ");
            float[] positions = fields.getFieldPositions(fieldname);
            System.out.print(positions[0]);
            System.out.print(" [ ");
            System.out.print(positions[1]);
            System.out.print(", ");
            System.out.print(positions[2]);
            System.out.print(", ");
            System.out.print(positions[3]);
            System.out.print(", ");
            System.out.print(positions[4]);
            System.out.println("]");
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:classroom.newspaper_b.Newspaper12.java

public static void main(String[] args) {
    Newspaper11.main(args);/*from w w w  .  j  a v  a2s . c o  m*/
    try {
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        AcroFields fields = stamper.getAcroFields();
        fields.setField("field1", "Advertissement 1");
        float[] positions1 = fields.getFieldPositions("field1");
        putImage(stamper.getOverContent((int) positions1[0]), IMG1, positions1);
        fields.setField("field2", "Advertissement 2");
        float[] positions2 = fields.getFieldPositions("field2");
        putImage(stamper.getOverContent((int) positions2[0]), IMG2, positions2);
        stamper.setFormFlattening(true);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:classroom.newspaper_b.Newspaper13.java

public static void main(String[] args) {
    try {/*from ww  w  . j  av a  2 s .  c o m*/
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        addButton(stamper, new Rectangle(LLX1, LLY1, URX1, URY1), PATH1, "button1", 1);
        addButton(stamper, new Rectangle(LLX2, LLY2, URX2, URY2), PATH2, "button2", 1);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:classroom.newspaper_b.Newspaper14.java

public static void main(String[] args) {
    Newspaper13.main(args);/*from   ww w.  j ava 2  s  . c om*/
    try {
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        changeField(stamper, "button1", PATH1);
        changeField(stamper, "button2", PATH2);
        stamper.setFormFlattening(true);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:com.bluexml.side.Framework.alfresco.workflow.pdfGenerator.structure.AlfrescoStructure.java

License:Open Source License

public static PdfStamper manageAlfrescoPDF(PdfReader reader, Map<String, String> commands)
        throws IOException, NoPdfFileException, MissingOutputPathForPDFException, DocumentException,
        MissingOverridePdfKeyException, FileExistsException, FileNotFoundException {
    PdfStamper stamper = null;/*from w  ww  .  j ava 2  s.co  m*/
    if (!commands.containsKey(ConstantsLanguage.OUTPUT_PDF_KEY)) {
        throw new MissingOutputPathForPDFException(MissingOutputPathForPDFException.DOES_NOT_EXISTS);
    } else if (commands.containsKey(ConstantsLanguage.OUTPUT_PDF_KEY)) {
        OutputStream outputStream = null;
        String path = commands.get(ConstantsLanguage.OUTPUT_PDF_KEY);
        if (getNodeRefFromAlfrescoPathToPdf(path) == null) {
            outputStream = createPdf(path, commands);
        } else {
            if (commands.containsKey(ConstantsLanguage.FORCE_OVERRIDE_PDF_KEY)) {
                if (commands.get(ConstantsLanguage.FORCE_OVERRIDE_PDF_KEY)
                        .equals(ConstantsLanguage.FORCE_OVERRIDE_PDF_VALUES[0])) {
                    outputStream = getStreamFromAlfrescoPathToPdf(path);
                }
            } else {
                throw new MissingOverridePdfKeyException(MissingOverridePdfKeyException.DOES_NOT_EXISTS);
            }
        }
        stamper = new PdfStamper(reader, outputStream);
    }
    return stamper;
}

From source file:com.c6rs.catsix.reg.pdf.PopulatePDF.java

/**
 * @param args/* ww  w . j  a  v  a 2s  .co  m*/
 * @throws IOException
 * @throws DocumentException
 */
public static void main(String[] args) throws IOException, DocumentException {

    PdfReader reader = new PdfReader(PopulatePDF.class.getResourceAsStream("waiver_form.pdf"));
    FileOutputStream out = new FileOutputStream("/TEMP/waiver.pdf");
    PdfStamper stamp = new PdfStamper(reader, out);

    AcroFields fields = stamp.getAcroFields();
    fields.setField(DATE_FIELD, new Date(System.currentTimeMillis()).toString());
    fields.setField(EVENT_FIELD, "THE STATE FAIR AFFAIR");
    fields.setField(EVENT_DATES_FIELD, new Date(System.currentTimeMillis()).toString());
    fields.setField(LICENSE_FIELD, "123456789");
    fields.setField(RACING_AGE_FIELD, "33");
    fields.setField(FULLNAME_FIELD, "JOE RACER");
    fields.setField(ADDRESS_FIELD, "123 MAIN STREET");
    fields.setField(CITY_FIELD, "MINNEAPOLIS");
    fields.setField(STATE_FIELD, "MN");
    fields.setField(ZIP_FIELD, "5416");
    fields.setField(PHONE_FIELD, "555-555-5555");
    fields.setField(EMAIL_FIELD, "JOE.RACER@GMAIL.COM");
    fields.setField(CONTACT_NAME_FIELD, "JOAN RACER");
    fields.setField(CONTACT_PHONE_FIELD, "555-555-5555");

    // Need to lock fields and prevent further edits?
    //stamp.setFormFlattening(true); 

    stamp.close();

}

From source file:com.homesoft.component.report.pdf.PdfWrappedStamper.java

License:Open Source License

private ByteArrayOutputStream stampInputStream(InputStream is) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    PdfReader reader = null;//from ww w . j  ava 2 s.c  o  m
    PdfStamper stamp = null;
    try {
        reader = new PdfReader(is);
        stamp = new PdfStamper(reader, os);
    } catch (DocumentException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    if (reader == null || stamp == null) {
        return null;
    }

    int total = reader.getNumberOfPages();
    for (int i = 1; i <= total; i++) {
        PdfContentByte over = stamp.getOverContent(i);

        boolean fixedImageEnabled = GlobalConfig.getInstance().getStamperFixedImageEnabled();
        boolean dynamicImageEnabled = GlobalConfig.getInstance().getStamperDynamicImageEnabled();
        if (imageEnabled && fixedImageEnabled) {
            image.setRotationDegrees(GlobalConfig.getInstance().getStamperFixedImageRotationDegree());
            image.setAbsolutePosition(GlobalConfig.getInstance().getStamperFixedImagePositionX(),
                    GlobalConfig.getInstance().getStamperFixedImagePositionY());// position
            over.setGState(gsFixed);
            try {
                over.addImage(image);
            } catch (DocumentException e) {
                log.error(e.getMessage(), e);
            }
        }

        if (imageEnabled && dynamicImageEnabled) {
            int jx = GlobalConfig.getInstance().getStamperDynamicImagePositionXStart();
            int jy = GlobalConfig.getInstance().getStamperDynamicImagePositionYStart();
            while (jy < 800) {
                // img.setRotation(30);
                image.setRotationDegrees(GlobalConfig.getInstance().getStamperDynamicImageRotationDegree());
                image.setAbsolutePosition(jx, jy);
                over.setGState(gsDynamic);
                try {
                    over.addImage(image);
                } catch (DocumentException e) {
                    log.error(e.getMessage(), e);
                }
                jx += GlobalConfig.getInstance().getStamperDynamicImagePositionXStep();
                jy += GlobalConfig.getInstance().getStamperDynamicImagePositionYStep();
            }
        }
    }

    try {
        stamp.close();
    } catch (DocumentException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
    reader.close();
    return os;
}