Example usage for com.itextpdf.text.pdf PdfStamper addAnnotation

List of usage examples for com.itextpdf.text.pdf PdfStamper addAnnotation

Introduction

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

Prototype

public void addAnnotation(final PdfAnnotation annot, final int page) 

Source Link

Document

Adds an annotation of form field in a specific page.

Usage

From source file:app.App.java

private void setLink(int pageId, PdfStamper stamper) throws ClassNotFoundException, IOException {
    // load the sqlite-JDBC driver using the current class loader
    Class.forName("org.sqlite.JDBC");

    Connection connection = null;
    try {//  www. j  a  v a  2  s  . c o m
        // create a database connection
        connection = DriverManager.getConnection("jdbc:sqlite:" + DATABASE);
        Statement statement = connection.createStatement();
        statement.setQueryTimeout(30); // set timeout to 30 sec.

        ResultSet rs = statement.executeQuery(
                "select * from page_annotations " + "where annotation_type = 'goto' and page_id = " + pageId);
        float spendX = 0;
        float spendY = 0;
        float coordX = 0;
        float coordY = 0;
        int pageDest = 0;
        Rectangle rect;
        PdfAnnotation annotation;
        while (rs.next()) {
            spendX = rs.getFloat("pdf_width");
            spendY = rs.getFloat("pdf_height");
            coordX = rs.getFloat("pdf_x");// + rs.getFloat("pdf_width");
            coordY = rs.getFloat("pdf_y");
            pageDest = rs.getInt("annotation_target");

            Font font = FontFactory.getFont(FontFactory.COURIER, 10, Font.UNDERLINE);
            // Blue
            font.setColor(0, 0, 255);
            Chunk chunk = new Chunk("GOTO", font);
            Anchor anchor = new Anchor(chunk);
            ColumnText.showTextAligned(stamper.getUnderContent(pageId), Element.ALIGN_LEFT, anchor,
                    coordX + spendX, coordY, 0);

            rect = new Rectangle(coordX, coordY, spendX, spendY);
            annotation = PdfAnnotation.createLink(stamper.getWriter(), rect, PdfAnnotation.HIGHLIGHT_INVERT,
                    new PdfAction("#" + pageDest));
            stamper.addAnnotation(annotation, pageId);
        }
    } catch (SQLException e) {
        // if the error message is "out of memory", 
        // it probably means no database file is found
        System.err.println(e.getMessage());
    } finally {
        try {
            if (connection != null)
                connection.close();
        } catch (SQLException e) {
            // connection close failed.
            System.err.println(e);
        }
    }
}

From source file:com.mycompany.mavenproject1.SubmitForm.java

public void extractFromPdf(String src, String dest) throws DocumentException, IOException {

    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));

    PushbuttonField button = new PushbuttonField(stamper.getWriter(), new Rectangle(36, 700, 112, 730), "get");

    //stamper.getWriter().addJavaScript(Utilities.readFileToString(RESOURCE));
    button.setText("SAVE My INFO");
    button.setBackgroundColor(new GrayColor(0.7f));
    button.setVisibility(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
    PdfFormField submit = button.getField();

    /*//from www  . j av a 2  s.  c o m
          button.setBackgroundColor(new GrayColor(0.7f));
          button.setVisibility(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
     */

    //PdfFormField pull_data = upload_info.getField();
    //  pull_data.setAction(PdfAction.javaScript(Utilities.readFileToString(submit_button_script), stamper.getWriter()));
    stamper.getWriter().addJavaScript(Utilities.readFileToString(checkbox));
    stamper.getWriter().addJavaScript(Utilities.readFileToString(upload_info));
    //PushbuttonField submitButton=stamper.getAcroFields().getNewPushbuttonFromField("UseSavedInfo");
    //PdfFormField field=submitButton.getField();
    //field.setAction(PdfAction.javaScript("app.alert('hello')" ,stamper.getWriter()));
    /*
     PushbuttonField useMySavedInfo = new PushbuttonField(
      stamper.getWriter(), new Rectangle(36, 1000, 559, 806), "MySavedInfo" );
      useMySavedInfo.setText("Upload info");
      useMySavedInfo.setBackgroundColor(new GrayColor(0.7f));
      useMySavedInfo.setVisibility(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
       PdfFormField  extractInfo=useMySavedInfo.getField();
    */
    //  extractInfo.setAction(PdfAction.javaScript("alert('hello')", stamper.getWriter()));
    //  extractInfo.setAction(PdfAction.);
    //stamper.

    /*
    AcroFields fields = stamper.getAcroFields();
     PushbuttonField submitButton=fields.getNewPushbuttonFromField("SubmitForm");
    */
    //System.out.println(submitButton.getAppearance().getHeight());
    //System.out.println(submitButton.getAppearance().getWidth());
    //System.out.println(submitButton.getAppearance());
    //submitButton
    //submitButton.getField().get
    //submitButton.getWriter().setAdditionalAction(, PdfAction.javaScript("app.alert('os')",stamper.getWriter()));
    //submitButton.getWriter().setAdditionalAction(PdfName., action);
    //PdfFormField sb=submitButton.getField();
    //sb.setAction(PdfAction.javaScript("app.alert('test')", stamper.getWriter()));

    // sumbitInfo.setAction(PdfAction.javaScript("app.alert('test')", stamper.getWriter()));
    //submit.setAction(PdfAction.javaScript("app.alert('test')", stamper.getWriter()));

    //PushbuttonField useInfo = fields.getNewPushbuttonFromField("UseSavedInfo");
    //PdfAnnotation getInfo=useInfo.getField();
    //getInfo.setAction(PdfAction.javaScript("app.alert('action!')", stamper.getWriter()));

    // ffield.SetAdditionalActions(PdfName.E, PdfAction("app.alert('action!')"));

    AcroFields fields = stamper.getAcroFields();

    // PushbuttonField saveInfo = fields.getNewPushbuttonFromField("SaveInfo");
    //   PdfFormField fd=saveInfo.getField();
    //fd.setAction(PdfAction.createSubmitForm("http://127.0.0.1/index.php",null,PdfAction.SUBMIT_HTML_FORMAT));
    //PdfAppearance pa =saveInfo.getAppearance();
    // pa.setAction(PdfAction.createSubmitForm("http://127.0.0.1/index.php",null,PdfAction.SUBMIT_HTML_FORMAT), 0, 0, 0, 0);
    Set<String> fldNames = fields.getFields().keySet();

    //AcroFields fields = stamper.getAcroFields();
    for (String fldName : fldNames) {
        System.out.println(fldName + ": " + fields.getField(fldName));
    }
    //submit.setAdditionalAction(PdfAction.javaScript(Utilities.readFileToString(submit_button_script), stamper.getWriter()));
    //http://www.mycrewid.com/Alexander/index.php
    //submit.setAction(PdfAction.createSubmitForm(
    //       "http://127.0.0.1/index.php", null,
    //     PdfAction.SUBMIT_HTML_FORMAT));
    submit.setAction(
            PdfAction.javaScript(Utilities.readFileToString(submit_button_script), stamper.getWriter()));
    stamper.addAnnotation(submit, 1);
    stamper.close();
    //PdfAction.javaScript("this.getField('FirstName').value=util.printd(\"dd mmmm yyyy\",new Date())",stamper.getWriter());

    //stamper.addAnnotation(submitButton, 1);
    //stamper.addAnnotation(sb,1);
    // submit.setAdditionalActions(PdfName.E, PdfAction.javaScript(Utilities.readFileToString(submit_button_script),stamper.getWriter()));

}

From source file:com.ots.jsp1.itext.ADAStamper.java

License:Open Source License

public void addADAAsWatermark(InputStream inStream, OutputStream outputStream, String ADA)
        throws DocumentException, IOException {
    PdfStamper stamper = null;
    PdfReader reader = null;/*from w  w w  . ja v a2 s.  c om*/
    try {

        reader = new PdfReader(inStream);
        //The zero byte means we dont want to change the version number of the PDF file.
        //true->not to change any of the original bytes
        stamper = new PdfStamper(reader, outputStream, '\0', true);
        int numberOfPages = reader.getNumberOfPages();

        for (int currentPage = 1; currentPage <= numberOfPages; currentPage++) {

            PdfAppearance canvas = PdfAppearance.createAppearance(stamper.getWriter(), 100, 30);
            canvas.setFontAndSize(
                    BaseFont.createFont(fontFilePath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, true), 11);
            Rectangle pageSize = reader.getPageSizeWithRotation(currentPage);
            Rectangle watermarkPosition = new Rectangle(pageSize.getRight() - 150, pageSize.getTop() - 30,
                    pageSize.getRight() - 50, pageSize.getTop() - 10, 0);
            PdfAnnotation annotation = PdfAnnotation.createFreeText(stamper.getWriter(), watermarkPosition, ADA,
                    canvas);
            annotation.put(PdfName.F, new PdfNumber(PdfAnnotation.FLAGS_READONLY));

            //  annotation.put(PdfName.FONT, canvas);
            //  PdfAnnotation annotation = PdfAnnotation.createText(stamper.getWriter(), watermarkPosition, "", ADA, true, "Key");
            // 

            PdfBorderDictionary borderDictionary = new PdfBorderDictionary(0, PdfBorderDictionary.STYLE_SOLID);

            annotation.setBorderStyle(borderDictionary);
            stamper.addAnnotation(annotation, currentPage);
        }
    } finally {
        stamper.close();
        reader.close();
    }

}