Example usage for com.lowagie.text Document open

List of usage examples for com.lowagie.text Document open

Introduction

In this page you can find the example usage for com.lowagie.text Document open.

Prototype

boolean open

To view the source code for com.lowagie.text Document open.

Click Source Link

Document

Is the document open or not?

Usage

From source file:com.geek.tutorial.itext.acroform.RadioCheckBoxForm.java

License:Open Source License

public RadioCheckBoxForm() throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("RadioCheckBoxForm.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();

    BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    Rectangle rect;/*ww  w .ja  v  a  2s.  c o m*/

    // Code 1 create radio button
    String[] radios = { "Radio1", "Radio2", "Radio3" };
    PdfFormField radioField = PdfFormField.createRadioButton(writer, true);
    radioField.setFieldName("radioField");
    radioField.setValueAsName(radios[0]);
    for (int i = 0; i < radios.length; i++) {
        rect = new Rectangle(40, 815 - i * 30, 60, 797 - i * 30);
        addRadioButton(writer, rect, radioField, radios[i], i == 0);
        cb.beginText();
        cb.setFontAndSize(bf, 12);
        cb.showTextAligned(Element.ALIGN_LEFT, radios[i], 70, 802 - i * 30, 0);
        cb.endText();
    }
    writer.addAnnotation(radioField);

    // Code 2 create checkbox button
    String[] options = { "Check1", "Check2", "Check3" };
    for (int i = 0; i < options.length; i++) {
        rect = new Rectangle(160, 815 - i * 30, 180, 797 - i * 30);
        addCheckbox(writer, rect, options[i]);
        cb.beginText();
        cb.setFontAndSize(bf, 12);
        cb.showTextAligned(Element.ALIGN_LEFT, options[i], 190, 802 - i * 30, 0);
        cb.endText();
    }

    // Code 3 add function and button for showing state 
    writer.addJavaScript(
            "function showState(){" + "app.alert('Radio:'+ this.getField('radioField').value +'\\n\\n'+"
                    + "'Check1:'+this.getField('Check1').value +'\\n'+"
                    + "'Check2:'+this.getField('Check2').value +'\\n'+"
                    + "'Check3:'+this.getField('Check3').value);" + "}");
    PushbuttonField push = new PushbuttonField(writer, new Rectangle(80, 710, 150, 730), "pushAction");
    push.setBackgroundColor(Color.LIGHT_GRAY);
    push.setBorderColor(Color.GRAY);
    push.setText("Show State");
    push.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
    push.setTextColor(Color.BLACK);
    PdfFormField pushbutton = push.getField();
    pushbutton.setAction(PdfAction.javaScript("showState()", writer));
    writer.addAnnotation(pushbutton);

    document.close();
}

From source file:com.geek.tutorial.itext.acroform.TextFieldForm.java

License:Open Source License

public TextFieldForm() throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TextFieldForm.pdf"));
    document.open();

    PdfPTable table = new PdfPTable(2);
    table.getDefaultCell().setPadding(5f); // Code 1, will only affect empty field
    table.setHorizontalAlignment(Element.ALIGN_LEFT);
    PdfPCell cell;//from   w w  w.  j  a va  2 s  .c  o m

    // Code 2, add name TextField      
    table.addCell("Name");
    TextField nameField = new TextField(writer, new Rectangle(0, 0, 200, 10), "nameField");
    nameField.setBackgroundColor(Color.WHITE);
    nameField.setBorderColor(Color.BLACK);
    nameField.setBorderWidth(1);
    nameField.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
    nameField.setText("");
    nameField.setAlignment(Element.ALIGN_LEFT);
    nameField.setOptions(TextField.REQUIRED);
    cell = new PdfPCell();
    cell.setMinimumHeight(10);
    cell.setCellEvent(new FieldCell(nameField.getTextField(), 200, writer));
    table.addCell(cell);

    // force upper case javascript
    writer.addJavaScript("var nameField = this.getField('nameField');" + "nameField.setAction('Keystroke',"
            + "'forceUpperCase()');" + "" + "function forceUpperCase(){"
            + "if(!event.willCommit)event.change = " + "event.change.toUpperCase();" + "}");

    // Code 3, add empty row
    table.addCell("");
    table.addCell("");

    // Code 4, add age TextField
    table.addCell("Age");
    TextField ageComb = new TextField(writer, new Rectangle(0, 0, 30, 10), "ageField");
    ageComb.setBorderColor(Color.BLACK);
    ageComb.setBorderWidth(1);
    ageComb.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
    ageComb.setText("12");
    ageComb.setAlignment(Element.ALIGN_RIGHT);
    ageComb.setMaxCharacterLength(2);
    ageComb.setOptions(TextField.COMB | TextField.DO_NOT_SCROLL);
    cell = new PdfPCell();
    cell.setMinimumHeight(10);
    cell.setCellEvent(new FieldCell(ageComb.getTextField(), 30, writer));
    table.addCell(cell);

    // validate age javascript
    writer.addJavaScript("var ageField = this.getField('ageField');"
            + "ageField.setAction('Validate','checkAge()');" + "function checkAge(){" + "if(event.value < 12){"
            + "app.alert('Warning! Applicant\\'s age can not" + " be younger than 12.');" + "event.value = 12;"
            + "}}");

    // add empty row
    table.addCell("");
    table.addCell("");

    // Code 5, add age TextField
    table.addCell("Comment");
    TextField comment = new TextField(writer, new Rectangle(0, 0, 200, 100), "commentField");
    comment.setBorderColor(Color.BLACK);
    comment.setBorderWidth(1);
    comment.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
    comment.setText("");
    comment.setOptions(TextField.MULTILINE | TextField.DO_NOT_SCROLL);
    cell = new PdfPCell();
    cell.setMinimumHeight(100);
    cell.setCellEvent(new FieldCell(comment.getTextField(), 200, writer));
    table.addCell(cell);

    // check comment characters length javascript
    writer.addJavaScript("var commentField = " + "this.getField('commentField');" + "commentField"
            + ".setAction('Keystroke','checkLength()');" + "function checkLength(){"
            + "if(!event.willCommit && " + "event.value.length > 100){" + "app.alert('Warning! Comment can not "
            + "be more than 100 characters.');" + "event.change = '';" + "}}");

    // add empty row
    table.addCell("");
    table.addCell("");

    // Code 6, add submit button   
    PushbuttonField submitBtn = new PushbuttonField(writer, new Rectangle(0, 0, 35, 15), "submitPOST");
    submitBtn.setBackgroundColor(Color.GRAY);
    submitBtn.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
    submitBtn.setText("POST");
    submitBtn.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
    PdfFormField submitField = submitBtn.getField();
    submitField.setAction(PdfAction.createSubmitForm("http://www.geek-tutorials.com/java/itext/submit.php",
            null, PdfAction.SUBMIT_HTML_FORMAT));
    cell = new PdfPCell();
    cell.setMinimumHeight(15);
    cell.setCellEvent(new FieldCell(submitField, 35, writer));
    table.addCell(cell);

    // Code 7, add reset button
    PushbuttonField resetBtn = new PushbuttonField(writer, new Rectangle(0, 0, 35, 15), "reset");
    resetBtn.setBackgroundColor(Color.GRAY);
    resetBtn.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED);
    resetBtn.setText("RESET");
    resetBtn.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
    PdfFormField resetField = resetBtn.getField();
    resetField.setAction(PdfAction.createResetForm(null, 0));
    cell = new PdfPCell();
    cell.setMinimumHeight(15);
    cell.setCellEvent(new FieldCell(resetField, 35, writer));
    table.addCell(cell);

    document.add(table);
    document.close();
}

From source file:com.geek.tutorial.itext.bookmarks.Anchor.java

License:Open Source License

public Anchor() throws Exception {

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("anchor.pdf"));
    document.open();

    // Code 1/* w w  w.  j a  v a  2s  . c  o m*/
    Font font = new Font();
    font.setColor(Color.BLUE);
    font.setStyle(Font.UNDERLINE);

    document.add(new Chunk("Chapter 1"));
    document.add(new Paragraph(new Chunk("Press here to go chapter 2", font).setLocalGoto("2")));// Code 2
    document.newPage();

    document.add(new Chunk("Chapter 2").setLocalDestination("2"));
    document.add(new Paragraph(
            new Chunk("http://www.geek-tutorials.com", font).setAnchor("http://www.geek-tutorials.com")));//Code 3
    document.add(
            new Paragraph(new Chunk("Open outline.pdf chapter 3", font).setRemoteGoto("outline.pdf", "3")));//Code 4

    document.close();

}

From source file:com.geek.tutorial.itext.bookmarks.Outline.java

License:Open Source License

public Outline() throws Exception {

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("outline.pdf"));
    document.open();

    // Code 1//from w  w w  .j ava  2  s . c o  m
    document.add(new Chunk("Chapter 1").setLocalDestination("1"));
    document.newPage();

    document.add(new Chunk("Chapter 2").setLocalDestination("2"));
    document.add(new Paragraph(new Chunk("Sub 2.1").setLocalDestination("2.1")));
    document.add(new Paragraph(new Chunk("Sub 2.2").setLocalDestination("2.2")));
    document.newPage();

    document.add(new Chunk("Chapter 3").setLocalDestination("3"));

    // Code 2
    PdfContentByte cb = writer.getDirectContent();
    PdfOutline root = cb.getRootOutline();

    // Code 3
    PdfOutline oline1 = new PdfOutline(root, PdfAction.gotoLocalPage("1", false), "Chapter 1");

    PdfOutline oline2 = new PdfOutline(root, PdfAction.gotoLocalPage("2", false), "Chapter 2");
    oline2.setOpen(false);
    PdfOutline oline2_1 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.1", false), "Sub 2.1");
    PdfOutline oline2_2 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.2", false), "Sub 2.2");

    PdfOutline oline3 = new PdfOutline(root, PdfAction.gotoLocalPage("3", false), "Chapter 3");

    document.close();
}

From source file:com.geek.tutorial.itext.image.EmbedImage.java

License:Open Source License

public EmbedImage() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("EmbedImage.pdf"));
    document.open();

    // Code 1//w w  w  .  j  av  a 2  s  .co  m
    document.add(new Phrase("Please press "));
    document.add(new Chunk(Image.getInstance("save.gif"), 0, 0));
    document.add(new Phrase(" to save the file."));

    document.close();
}

From source file:com.geek.tutorial.itext.image.SimpleImages.java

License:Open Source License

public SimpleImages() throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("SimpleImages.pdf"));
    document.open();

    // Code 1//from  ww  w  .j a v  a2 s  .  com
    document.add(new Paragraph("Simple Image"));
    com.lowagie.text.Image image = com.lowagie.text.Image.getInstance("mouse.jpg");
    document.add(image);

    // Code 2
    document.add(new Paragraph("\n" + "AWT Image"));
    java.awt.Image awtImg = java.awt.Toolkit.getDefaultToolkit().createImage("square.jpg");
    com.lowagie.text.Image image2 = com.lowagie.text.Image.getInstance(awtImg, null);
    document.add(image2);
    document.newPage();

    // Code 3
    document.add(new Paragraph("Multipages tiff file"));
    RandomAccessFileOrArray ra = new RandomAccessFileOrArray("multipage.tif");
    int pages = TiffImage.getNumberOfPages(ra);
    for (int i = 1; i <= pages; i++) {
        document.add(TiffImage.getTiffImage(ra, i));
    }
    document.newPage();

    // Code 4
    document.add(new Paragraph("Animated Gifs"));
    GifImage img = new GifImage("bee.gif");
    int frame_count = img.getFrameCount();
    for (int i = 1; i <= frame_count; i++) {
        document.add(img.getImage(i));
    }
    document.close();
}

From source file:com.geek.tutorial.itext.image.TextWrapping.java

License:Open Source License

public TextWrapping() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("TextWrapping.pdf"));
    document.open();

    for (int i = 0; i < 100; i++) {
        document.add(new Phrase("AAAA ")); // Code 1
    }/*from   w ww  .j a va  2  s  .  com*/

    com.lowagie.text.Image image = com.lowagie.text.Image.getInstance("square.jpg");
    image.setAlignment(Image.RIGHT | Image.TEXTWRAP); // Code 2
    document.add(image);

    document.add(new Phrase("\n\n")); // Code 3
    for (int i = 0; i < 100; i++) {
        document.add(new Phrase("BBBB "));
    }

    document.close();

}

From source file:com.geek.tutorial.itext.image.Transformation.java

License:Open Source License

public Transformation() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("transformation.pdf"));
    document.open();

    Image img = Image.getInstance("square.jpg");
    img.setAbsolutePosition(100, 650); // Code 1

    img.scaleAbsolute(100, 100); // Code 2

    img.setRotationDegrees(40); // Code 3

    document.add(img);/* w  ww.  j a  v  a  2s  . c o m*/
    document.close();

}

From source file:com.geek.tutorial.itext.servlet.PDFServlet.java

License:Open Source License

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("application/pdf"); // Code 1
    Document document = new Document();
    try {//w w w  .j  a  va2  s.c  om
        PdfWriter.getInstance(document, response.getOutputStream()); // Code 2
        document.open();

        // Code 3
        PdfPTable table = new PdfPTable(2);
        table.addCell("1");
        table.addCell("2");
        table.addCell("3");
        table.addCell("4");
        table.addCell("5");
        table.addCell("6");

        // Code 4
        document.add(table);
        document.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:com.geek.tutorial.itext.table.PDFTable.java

License:Open Source License

public PDFTable() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("table_example.pdf"));
    document.open();

    PdfPTable table = new PdfPTable(3);
    //table.setTotalWidth(216f);
    //table.setLockedWidth(true);
    PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
    cell.setColspan(3);/*from  w  w w  .  ja va 2s. co  m*/
    table.addCell(cell);
    table.addCell("1.1");
    table.addCell("2.1");
    table.addCell("3.1");
    table.addCell("1.2");
    table.addCell("2.2");
    table.addCell("3.2");

    document.add(table);

    document.close();

}