Example usage for com.lowagie.text Document add

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

Introduction

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

Prototype


public boolean add(Element element) throws DocumentException 

Source Link

Document

Adds an Element to the Document.

Usage

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();/*from  w w  w.j a v a  2  s  .c o m*/

    // Code 1
    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();/* ww  w.j a  v  a 2 s .c  o  m*/

    // Code 1
    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();//from w w  w .  ja  va 2s  .c  o  m

    // Code 1
    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();/*from   w  ww  .j a  v a  2 s. co m*/

    for (int i = 0; i < 100; i++) {
        document.add(new Phrase("AAAA ")); // Code 1
    }

    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();/*www . j  ava 2 s.co m*/

    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);
    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 {/*from ww  w . ja v a 2  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();/*from  w  ww . j ava 2 s  .c  o m*/

    PdfPTable table = new PdfPTable(3);
    //table.setTotalWidth(216f);
    //table.setLockedWidth(true);
    PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
    cell.setColspan(3);
    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();

}

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

License:Open Source License

public SimplePDFTable() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("SimplePDFTable.pdf"));
    document.open();//from   w w w .  jav  a2s . c om

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

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

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

License:Open Source License

public SimplePDFTableAlignAndWidth() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("SimplePDFTableAlignAndWidth.pdf"));
    document.open();/*from  www .j  av  a  2  s.  c o m*/

    float[] colsWidth = { 1f, 2f }; // Code 1
    PdfPTable table = new PdfPTable(colsWidth);

    table.setWidthPercentage(90); // Code 2
    table.setHorizontalAlignment(Element.ALIGN_LEFT); // Code 3

    table.addCell("1");
    table.addCell("2");
    table.addCell("3");
    table.addCell("4");
    table.addCell("5");
    table.addCell("6");

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

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

License:Open Source License

public SimplePDFTableColspan() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("SimplePDFTableColspan.pdf"));
    document.open();//  www. jav a2 s  . c  o  m

    PdfPTable table = new PdfPTable(2);

    PdfPCell cell = new PdfPCell(new Paragraph("column span 2"));
    cell.setColspan(2);
    table.addCell(cell);

    table.addCell("1");
    table.addCell("2");
    table.addCell("3");
    table.addCell("4");
    table.addCell("5");
    table.addCell("6");

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