Example usage for com.lowagie.text Document newPage

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

Introduction

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

Prototype


public boolean newPage() 

Source Link

Document

Signals that an new page has to be started.

Usage

From source file:questions.graphics2D.SplitCanvasDifficult.java

public static void main(String[] args) {
    Document document = new Document();
    try {//from   w ww . j  a v a 2  s .  c  o  m
        document.setPageSize(new Rectangle(100, 100));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        // create the canvas for the complete drawing:
        PdfContentByte directContent = writer.getDirectContentUnder();
        Graphics2D g2d;
        // distribute the image over 4 pages:
        g2d = directContent.createGraphicsShapes(100, 100);
        g2d.setPaint(new Color(255, 150, 150));
        g2d.setStroke(new BasicStroke(5.0f));
        g2d.drawLine(25, 25, 25, 100);
        g2d.drawLine(25, 25, 100, 25);
        g2d.dispose();
        document.newPage();
        g2d = directContent.createGraphicsShapes(100, 100);
        g2d.setPaint(new Color(255, 150, 150));
        g2d.setStroke(new BasicStroke(5.0f));
        g2d.drawLine(0, 25, 75, 25);
        g2d.drawLine(75, 25, 75, 100);
        g2d.dispose();
        document.newPage();
        g2d = directContent.createGraphicsShapes(100, 100);
        g2d.setPaint(new Color(255, 150, 150));
        g2d.setStroke(new BasicStroke(5.0f));
        g2d.drawLine(25, 0, 25, 75);
        g2d.drawLine(25, 75, 100, 75);
        g2d.dispose();
        document.newPage();
        g2d = directContent.createGraphicsShapes(100, 100);
        g2d.setPaint(new Color(255, 150, 150));
        g2d.setStroke(new BasicStroke(5.0f));
        g2d.drawLine(0, 75, 75, 75);
        g2d.drawLine(75, 0, 75, 75);
        g2d.dispose();
    } catch (DocumentException de) {
        de.printStackTrace();
        return;
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return;
    }
    document.close();
}

From source file:questions.graphics2D.SplitCanvasEasy.java

public static void main(String[] args) {
    Document document = new Document();
    try {//w ww  .  j a va 2s . co  m
        document.setPageSize(new Rectangle(100, 100));
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        // create the canvas for the complete drawing:
        PdfContentByte directContent = writer.getDirectContentUnder();
        PdfTemplate canvas = directContent.createTemplate(200, 200);
        Graphics2D g2d = canvas.createGraphicsShapes(200, 200);
        // draw to the complete drawing to the canvas:
        g2d.setPaint(new Color(150, 150, 255));
        g2d.setStroke(new BasicStroke(10.0f));
        g2d.drawArc(50, 50, 100, 100, 0, 360);
        g2d.dispose();
        // wrap the canvas inside an image:
        Image img = Image.getInstance(canvas);
        // distribute the image over 4 pages:
        img.setAbsolutePosition(0, -100);
        document.add(img);
        document.newPage();
        img.setAbsolutePosition(-100, -100);
        document.add(img);
        document.newPage();
        img.setAbsolutePosition(0, 0);
        document.add(img);
        document.newPage();
        img.setAbsolutePosition(-100, 0);
        document.add(img);
    } catch (DocumentException de) {
        de.printStackTrace();
        return;
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return;
    }
    document.close();
}

From source file:questions.images.CustomizedTitleBar.java

public static void main(String[] args) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
    writer.setInitialLeading(18);// w w  w. j av a 2  s . c o m
    document.open();

    Image button = Image.getInstance(RESOURCE);
    document.add(getTitleBar(writer, button, "My first title"));
    for (int i = 0; i < 20; i++)
        document.add(new Paragraph("Some text: " + i));
    document.add(getTitleBar(writer, button, "My second title"));
    for (int i = 0; i < 10; i++)
        document.add(new Paragraph("Some text: " + i));
    document.newPage();
    document.add(getTitleBar(writer, button, "My third title"));
    for (int i = 0; i < 10; i++)
        document.add(new Paragraph("Some text: " + i));

    document.close();
}

From source file:questions.images.MakingImageTransparent.java

public static void main(String args[]) {
    try {//from   w w w .  ja  v a2 s.  c  o m
        Rectangle rect;
        // GIF Image
        Image gif = Image.getInstance(GIF);
        gif.setAbsolutePosition(0, 0);
        gif.setTransparency(new int[] { 0x5B, 0x5D });
        rect = new Rectangle(gif.getScaledWidth(), gif.getScaledHeight());
        rect.setBackgroundColor(Color.YELLOW);

        Document document = new Document(rect);
        PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        document.add(gif);

        // JPEG Image
        java.awt.Image awtImage = Toolkit.getDefaultToolkit().createImage(JPG);
        Image jpg = Image.getInstance(awtImage, null, true);
        jpg.setTransparency(new int[] { 0xF0, 0xFF });
        jpg.setAbsolutePosition(0, 0);
        rect = new Rectangle(jpg.getScaledWidth(), jpg.getScaledHeight());
        rect.setBackgroundColor(Color.YELLOW);

        document.setPageSize(rect);
        document.newPage();
        document.add(jpg);
        document.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:questions.importpages.ConcatenateMakeTOC.java

public static void createPdf(int i) {
    Document document = new Document();
    try {//from  w  ww. java  2  s .co m
        PdfWriter.getInstance(document, new FileOutputStream(RESULTS[i]));
        document.open();
        int doc = i + 1;
        for (i = 1; i < 4; i++) {
            document.add(new Paragraph(String.format("Document %d; page %d", doc, i)));
            document.newPage();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    document.close();
}

From source file:questions.importpages.HelloWorldImportedPages.java

/**
 * Generates a PDF file with bookmarks./*from  ww w.  j a  v  a2s  . c  o  m*/
 * 
 * @param filename
 *            the filename of the PDF file.
 */
private static void createPdf(String filename) {
    // we create a document with multiple pages and bookmarks
    Document document = new Document(PageSize.A4);
    try {
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        document.add(new Paragraph(
                "In this document, we are going to say hello to different beings in different languages."));
        document.newPage();
        Paragraph hello = new Paragraph(
                "(English:) hello, " + "(Esperanto:) he, alo, saluton, (Latin:) heu, ave, "
                        + "(French:) all\u00f4, (Italian:) ciao, (German:) hallo, he, heda, holla, "
                        + "(Portuguese:) al\u00f4, ol\u00e1, hei, psiu, bom d\u00eda, (Dutch:) hallo, dag, "
                        + "(Spanish:) ola, eh, (Catalan:) au, bah, eh, ep, "
                        + "(Swedish:) hej, hejsan(Danish:) hallo, dav, davs, goddag, hej, "
                        + "(Norwegian:) hei; morn, (Papiamento:) halo; hallo; k\u00ed tal, "
                        + "(Faeroese:) hall\u00f3, hoyr, (Turkish:) alo, merhaba, (Albanian:) tungjatjeta");
        Chapter universe = new Chapter("To the Universe:", 1);
        Section section;
        section = universe.addSection("to the World:");
        section.add(hello);
        section = universe.addSection("to the Sun:");
        section.add(hello);
        section = universe.addSection("to the Moon:");
        section.add(hello);
        section = universe.addSection("to the Stars:");
        section.add(hello);
        document.add(universe);
        Chapter people = new Chapter("To the People:", 2);
        section = people.addSection("to mothers and fathers:");
        section.add(hello);
        section = people.addSection("to brothers and sisters:");
        section.add(hello);
        section = people.addSection("to wives and husbands:");
        section.add(hello);
        section = people.addSection("to sons and daughters:");
        section.add(hello);
        section = people.addSection("to complete strangers:");
        section.add(hello);
        document.add(people);
        document.setPageSize(PageSize.A4.rotate());
        Chapter animals = new Chapter("To the Animals:", 3);
        section = animals.addSection("to cats and dogs:");
        section.add(hello);
        section = animals.addSection("to birds and bees:");
        section.add(hello);
        section = animals.addSection("to farm animals and wild animals:");
        section.add(hello);
        section = animals.addSection("to bugs and beatles:");
        section.add(hello);
        section = animals.addSection("to fish and shellfish:");
        section.add(hello);
        document.add(animals);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();
}

From source file:questions.importpages.NameCard.java

public static void createOneCard() throws DocumentException, IOException {
    Rectangle rect = new Rectangle(Utilities.millimetersToPoints(86.5f), Utilities.millimetersToPoints(55));
    Document document = new Document(rect);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(CARD));
    writer.setViewerPreferences(PdfWriter.PrintScalingNone);
    document.open();//from   w w  w.j a  v a  2  s .co m
    PdfReader reader = new PdfReader(LOGO);
    Image img = Image.getInstance(writer.getImportedPage(reader, 1));
    img.scaleToFit(rect.getWidth() / 1.5f, rect.getHeight() / 1.5f);
    img.setAbsolutePosition((rect.getWidth() - img.getScaledWidth()) / 2,
            (rect.getHeight() - img.getScaledHeight()) / 2);
    document.add(img);
    document.newPage();
    BaseFont bf = BaseFont.createFont(FONT, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    Font font = new Font(bf, 12);
    font.setColor(new CMYKColor(1, 0.5f, 0, 0.467f));
    ColumnText column = new ColumnText(writer.getDirectContent());
    Paragraph p;
    p = new Paragraph("Bruno Lowagie\n1T3XT\nbruno@1t3xt.com", font);
    p.setAlignment(Element.ALIGN_CENTER);
    column.addElement(p);
    column.setSimpleColumn(0, 0, rect.getWidth(), rect.getHeight() * 0.75f);
    column.go();
    document.close();
}

From source file:questions.objects.DifferentLeadings.java

public static void main(String[] args) {
    Document document = new Document(PageSize.A7);
    try {/*from   w w  w. j a  v a2  s  .c  om*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();
        Chunk space = new Chunk(' ');
        String text = "Quick brown fox jumps over the lazy dog.";
        Phrase phrase1 = new Phrase(text, new Font(Font.HELVETICA, 12));
        Phrase phrase2 = new Phrase(new Chunk(text, new Font(Font.TIMES_ROMAN, 24)));
        Phrase phrase3 = new Phrase(text, new Font(Font.COURIER, 8));
        Phrase phrase4 = new Phrase(text, new Font(Font.HELVETICA, 4));
        Paragraph paragraph = new Paragraph();
        paragraph.add(phrase1);
        paragraph.add(space);
        paragraph.add(phrase2);
        paragraph.add(space);
        paragraph.add(phrase3);
        paragraph.add(space);
        paragraph.add(phrase4);
        paragraph.setMultipliedLeading(1.5f);
        paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
        ColumnText column = new ColumnText(writer.getDirectContent());
        column.setSimpleColumn(document.left(), document.bottom(), document.right(), document.top());
        column.addElement(paragraph);
        column.go();
        document.newPage();
        document.add(paragraph);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    document.close();
}

From source file:questions.objects.NewPageColumns.java

public static void main(String[] args) {

    // step 1//  w  w  w .  ja v  a 2s.c om
    Document document = new Document(PageSize.A6);
    try {
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        // step 3
        document.open();
        // step 4
        for (int i = 0; i < 25; i++)
            document.add(new Paragraph("Hello paragraph 1." + i));
        document.newPage();
        ColumnText column = new ColumnText(writer.getDirectContent());
        column.setSimpleColumn(PageSize.A6.getLeft(36), PageSize.A6.getBottom(36), PageSize.A6.getRight(36),
                PageSize.A6.getTop(36));
        for (int i = 0; i < 20; i++)
            column.addElement(new Paragraph("Hello column 1." + i));
        int status = column.go();
        while (ColumnText.hasMoreText(status)) {
            document.newPage();
            column.setYLine(PageSize.A6.getTop(36));
            status = column.go();
        }
        document.newPage();
        for (int i = 0; i < 10; i++)
            column.addElement(new Paragraph("Hello column 2." + i));
        status = column.go();
        while (ColumnText.hasMoreText(status)) {
            document.newPage();
            column.setYLine(PageSize.A6.getTop(36));
            status = column.go();
        }
        document.newPage();
        for (int i = 0; i < 5; i++)
            document.add(new Paragraph("Hello paragraph 2." + i));
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }
    // step 5
    document.close();
}

From source file:questions.separators.LineSeparator1.java

public static void main(String[] args) {
    Document document = new Document();
    try {//from ww  w .  ja  v  a  2 s  .  c  om
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.open();

        Paragraph separator = new Paragraph(0);
        separator.add(new Chunk(new LineSeparator(1, 80, Color.RED, Element.ALIGN_LEFT, -2)));

        ColumnText column = new ColumnText(writer.getDirectContent());
        for (int i = 0; i < 5; i++) {
            column.addElement(StarSeparators.TEXT);
            column.addElement(separator);
        }
        column.setSimpleColumn(36, 36, 295, 806);
        column.go();
        column.setSimpleColumn(300, 36, 559, 806);
        column.go();
        document.newPage();

        for (int i = 0; i < 10; i++) {
            document.add(StarSeparators.TEXT);
            document.add(separator);
        }
        document.close();
    } catch (Exception de) {
        de.printStackTrace();
    }
}