Example usage for com.itextpdf.text Document newPage

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

Introduction

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

Prototype


public boolean newPage() 

Source Link

Document

Signals that an new page has to be started.

Usage

From source file:PDFIMG.IMAGEStoPDF.java

public static void convertToIMG(String[] RESOURCES, String result, String path, int s)
        throws FileNotFoundException, DocumentException, BadElementException, IOException {
    System.out.println(":)");

    // step 1/*w  w  w.jav a  2 s .  co  m*/
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(result));
    // step 3
    document.open();
    // step 4
    // Adding a series of images
    Image img;
    System.out.println(":) :)");
    for (String image : RESOURCES) {
        System.out.println(image);

        img = Image.getInstance(path + image);

        document.setPageSize(img);
        document.newPage();
        img.setAbsolutePosition(0, 0);
        document.add(img);
        System.out.println(":(");
    }
    System.out.println(":) :) :) Pdf is outo");
    // step 5
    document.close();

    if (s == 1) {
        JOptionPane.showMessageDialog(null, "Converted Successfully");
        //JOptionPane.showMessageDialog(null, "Finished\nFile save in :\nC:\\DjVu++Task\\ImagestoPDF\\"+RESOURCES[0].substring(0,RESOURCES[0].lastIndexOf("."))+".pdf");
    }
}

From source file:pipe.PdfMaker.java

private void addTitlePage(Document document) throws DocumentException {
    Paragraph preface = new Paragraph();
    // We add one empty line
    addEmptyLine(preface, 1);/*w ww .java 2 s  .  c  o  m*/
    // Lets write a big header
    Paragraph para = new Paragraph("Meine Pfeifensammlung", catFont);
    para.setAlignment(Element.ALIGN_CENTER);
    preface.add(para);

    addEmptyLine(preface, 1);
    // Will create: Report generated by: _name, _date
    preface.add(new Paragraph("Erstellt von: " + System.getProperty("user.name"), arialSmall));
    addEmptyLine(preface, 7);
    Image image = null;
    String frontpic = "db" + File.separator + "pipe-frontpage.jpg";

    try {
        ImageIcon icon = new ImageIcon(frontpic.toString());
        java.awt.Image img = icon.getImage();
        java.awt.Image newimg = img.getScaledInstance(500, -1, java.awt.Image.SCALE_SMOOTH);
        image = Image.getInstance(newimg, null);
    } catch (BadElementException e) {
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
    Paragraph picture = new Paragraph("");
    picture.setAlignment(Element.ALIGN_CENTER);
    picture.add(image);
    preface.add(picture);
    addEmptyLine(preface, 10);

    preface.add(new Paragraph(
            "Dieser Katalog wurde mit Mat's pipe-manager erstellt.\nDie Software ist frei verfgbar und herunterladbar ber das Forum von www.komm-zur-pfeife.de.",
            arialSmall));

    document.add(preface);
    // Start a new page
    document.newPage();
}

From source file:pl.marcinmilkowski.hocrtopdf.Main.java

License:Open Source License

/**
 * @param args//from   www  .j ava  2s  .  co m
 */
public static void main(String[] args) {
    try {
        if (args.length < 1 || args[0] == "--help" || args[0] == "-h") {
            System.out.print("Usage: java pl.marcinmilkowski.hocrtopdf.Main INPUTURL.html OUTPUTURL.pdf\n"
                    + "\n" + "Converts hOCR files into PDF\n" + "\n"
                    + "Example: java pl.marcinmilkowski.hocrtopdf.Main hocr.html output.pdf\n");
            if (args.length < 1)
                System.exit(-1);
            else
                System.exit(0);
        }
        URL inputHOCRFile = null;
        FileOutputStream outputPDFStream = null;
        try {
            File file = new File(args[0]);
            inputHOCRFile = file.toURI().toURL();
        } catch (MalformedURLException e) {
            System.out.println("The first parameter has to be a valid file.");
            System.out.println("We got an error: " + e.getMessage());
            System.exit(-1);
        }
        try {
            outputPDFStream = new FileOutputStream(args[1]);
        } catch (FileNotFoundException e) {
            System.out.println("The second parameter has to be a valid URL");
            System.exit(-1);
        }

        // The resolution of a PDF file (using iText) is 72pt per inch
        float pointsPerInch = 72.0f;

        // Using the jericho library to parse the HTML file
        Source source = new Source(inputHOCRFile);

        int pageCounter = 1;

        Document pdfDocument = null;
        PdfWriter pdfWriter = null;
        PdfContentByte cb = null;
        RandomAccessFileOrArray ra = null;

        // Find the tag of class ocr_page in order to load the scanned image
        StartTag pageTag = source.getNextStartTag(0, "class", OCRPAGE);
        while (pageTag != null) {
            int prevPos = pageTag.getEnd();
            Pattern imagePattern = Pattern.compile("image\\s+([^;]+)");
            Matcher imageMatcher = imagePattern.matcher(pageTag.getElement().getAttributeValue("title"));
            if (!imageMatcher.find()) {
                System.out.println("Could not find a tag of class \"ocr_page\", aborting.");
                System.exit(-1);
            }
            // Load the image
            Image pageImage = null;
            try {
                File file = new File(imageMatcher.group(1));
                pageImage = Image.getInstance(file.toURI().toURL());
            } catch (MalformedURLException e) {
                System.out.println("Could not load the scanned image from: " + "file://" + imageMatcher.group(1)
                        + ", aborting.");
                System.exit(-1);
            }
            if (pageImage.getOriginalType() == Image.ORIGINAL_TIFF) { // this might
                                                                      // be
                                                                      // multipage
                                                                      // tiff!
                File file = new File(imageMatcher.group(1));
                if (pageCounter == 1 || ra == null) {
                    ra = new RandomAccessFileOrArray(file.toURI().toURL());
                }
                int nPages = TiffImage.getNumberOfPages(ra);
                if (nPages > 0 && pageCounter <= nPages) {
                    pageImage = TiffImage.getTiffImage(ra, pageCounter);
                }
            }
            int dpiX = pageImage.getDpiX();
            if (dpiX == 0) { // for images that don't set the resolution we assume
                             // 300 dpi
                dpiX = 300;
            }
            int dpiY = pageImage.getDpiY();
            if (dpiY == 0) { // as above for dpiX
                dpiY = 300;
            }
            float dotsPerPointX = dpiX / pointsPerInch;
            float dotsPerPointY = dpiY / pointsPerInch;
            float pageImagePixelHeight = pageImage.getHeight();
            if (pdfDocument == null) {
                pdfDocument = new Document(new Rectangle(pageImage.getWidth() / dotsPerPointX,
                        pageImage.getHeight() / dotsPerPointY));
                pdfWriter = PdfWriter.getInstance(pdfDocument, outputPDFStream);
                pdfDocument.open();
                // Put the text behind the picture (reverse for debugging)
                // cb = pdfWriter.getDirectContentUnder();
                cb = pdfWriter.getDirectContent();
            } else {
                pdfDocument.setPageSize(new Rectangle(pageImage.getWidth() / dotsPerPointX,
                        pageImage.getHeight() / dotsPerPointY));
                pdfDocument.newPage();
            }
            // first define a standard font for our text
            BaseFont base = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED);
            Font defaultFont = new Font(base, 8);
            // FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD,
            // CMYKColor.BLACK);

            cb.setHorizontalScaling(1.0f);

            pageImage.scaleToFit(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY);
            pageImage.setAbsolutePosition(0, 0);
            // Put the image in front of the text (reverse for debugging)
            // pdfWriter.getDirectContent().addImage(pageImage);
            pdfWriter.getDirectContentUnder().addImage(pageImage);

            // In order to place text behind the recognised text snippets we are
            // interested in the bbox property
            Pattern bboxPattern = Pattern.compile("bbox(\\s+\\d+){4}");
            // This pattern separates the coordinates of the bbox property
            Pattern bboxCoordinatePattern = Pattern.compile("(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
            // Only tags of the ocr_line class are interesting
            StartTag ocrTag = source.getNextStartTag(prevPos, "class", OCRPAGEORLINE);
            while (ocrTag != null) {
                prevPos = ocrTag.getEnd();
                if ("ocrx_word".equalsIgnoreCase(ocrTag.getAttributeValue("class"))) {
                    net.htmlparser.jericho.Element lineElement = ocrTag.getElement();
                    Matcher bboxMatcher = bboxPattern.matcher(lineElement.getAttributeValue("title"));
                    if (bboxMatcher.find()) {
                        // We found a tag of the ocr_line class containing a bbox property
                        Matcher bboxCoordinateMatcher = bboxCoordinatePattern.matcher(bboxMatcher.group());
                        bboxCoordinateMatcher.find();
                        int[] coordinates = { Integer.parseInt((bboxCoordinateMatcher.group(1))),
                                Integer.parseInt((bboxCoordinateMatcher.group(2))),
                                Integer.parseInt((bboxCoordinateMatcher.group(3))),
                                Integer.parseInt((bboxCoordinateMatcher.group(4))) };
                        String line = lineElement.getContent().getTextExtractor().toString();
                        float bboxWidthPt = (coordinates[2] - coordinates[0]) / dotsPerPointX;
                        float bboxHeightPt = (coordinates[3] - coordinates[1]) / dotsPerPointY;

                        // Put the text into the PDF
                        cb.beginText();
                        // Comment the next line to debug the PDF output (visible Text)
                        cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);
                        // height
                        cb.setFontAndSize(defaultFont.getBaseFont(), Math.max(Math.round(bboxHeightPt), 1));
                        // width
                        cb.setHorizontalScaling(bboxWidthPt / cb.getEffectiveStringWidth(line, false));
                        cb.moveText((coordinates[0] / dotsPerPointX),
                                ((pageImagePixelHeight - coordinates[3]) / dotsPerPointY));
                        cb.showText(line);
                        cb.endText();
                        cb.setHorizontalScaling(1.0f);
                    }
                } else {
                    if ("ocr_page".equalsIgnoreCase(ocrTag.getAttributeValue("class"))) {
                        pageCounter++;
                        pageTag = ocrTag;
                        break;
                    }
                }
                ocrTag = source.getNextStartTag(prevPos, "class", OCRPAGEORLINE);
            }
            if (ocrTag == null) {
                pdfDocument.close();
                break;
            }
        }
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:printers.AbstractHorariosPrinter.java

License:Open Source License

private void creaUnSoloDocumento() throws DocumentException, FileNotFoundException {
    Rectangle hoja;//from ww w . j  a v a 2  s .c  om
    if (rotated) {
        hoja = PageSize.A4.rotate();
    } else {
        hoja = PageSize.A4;
    }
    Document doc = new Document(hoja);
    PdfWriter.getInstance(doc, new FileOutputStream(fileDst));
    doc.open();
    int size = hojasHorarios.size();
    for (int n = 0; n < size; n++) {
        printCabecera(doc, cabeceras.get(n));
        printHojaHorario(doc, hojasHorarios.get(n));
        printPieDePagina(doc, piesDePagina.get(n));
        doc.newPage();
    }
    doc.close();
}

From source file:printers.HojaDeFirmaPrinter.java

License:Open Source License

/**
 *
 *//*from w  ww.j av a2s  .c o  m*/
public void imprime() {
    Document doc = new Document();
    try {
        PdfWriter.getInstance(doc, new FileOutputStream(fileDst));
    } catch (DocumentException ex) {
        Logger.getLogger(HojaDeFirmaPrinter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(HojaDeFirmaPrinter.class.getName()).log(Level.SEVERE, null, ex);
    }
    ArrayList<GregorianCalendar> dias = dp.getAcademicCalendar().computeArrayAcademicDays(inicio, fin);
    if (dias.size() > 0) {
        doc.open();
        for (Aula aula : aulas) {
            for (GregorianCalendar dia : dias) {
                creaHojaDeFirma(doc, aula, dia);
                doc.newPage();
            }
        }
        doc.close();
    } else {
        JOptionPane.showMessageDialog(null,
                "<html>No se ha obtenido ningn da vlido. Revisa los das no lectivos<br>"
                        + "y el inicio y fin del periodo acadmico</html>",
                "Error", JOptionPane.ERROR_MESSAGE);
    }
}

From source file:printInv.GenerateInvoice.java

private void createPDF(String pdfFilename) {

    Document doc = new Document();
    PdfWriter docWriter = null;/*from  w w  w .  j  a  va 2 s. co  m*/
    initializeFonts();

    try {
        //   String path = "docs/" + pdfFilename;
        String path = pdfFilename;
        docWriter = PdfWriter.getInstance(doc, new FileOutputStream(path));
        doc.addAuthor("SmartWMS");
        doc.addCreationDate();
        doc.addProducer();
        doc.addCreator("SmartWMS");
        doc.addTitle("Invoice");
        doc.setPageSize(PageSize.LETTER);

        doc.open();
        PdfContentByte cb = docWriter.getDirectContent();

        boolean beginPage = true;
        int y = 0;
        System.out.println("n ===========" + n);
        for (int i = 0; i < n; i++) {
            if (beginPage) {
                beginPage = false;
                generateLayout(doc, cb);
                generateHeader(doc, cb);
                y = 615;
            }
            generateDetail(doc, cb, i, y);
            y = y - 15;
            if (y < 50) {
                printPageNumber(cb);
                doc.newPage();
                beginPage = true;
            }
        }
        printPageNumber(cb);
        cb.beginText();
        cb.setFontAndSize(bfBold, 10);
        cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "Grand Total : " + total, 570, 35, 0);
        cb.endText();

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            doc.close();
        }
        if (docWriter != null) {
            docWriter.close();
        }
    }
}

From source file:printom.PDFCreator.java

public static void createBOL(ArrayList<Shipment> aShipmentList, int aTemplate) {

    ArrayList<Shipment> shipmentList = aShipmentList;
    Order chosenOrder = shipmentList.get(0).getChosenOrder();

    String bolCode = shipmentList.get(0).getBolCode();
    String bolFileName = bolCode + ".pdf";

    String myCustName = Customer.getCustomerName(chosenOrder.getCustID());
    String myShipDate = Prompter.dateString(shipmentList.get(0).getDateShipped());
    String myCarrier = shipmentList.get(0).getCarrier();
    String myInstructs = shipmentList.get(0).getInstructions();
    Address myAddress = Address.retAddress(shipmentList.get(0).getChosenOrder().getShipAddressID());

    try {//w w w.j a v  a 2 s .com

        //BaseFont bf1 = BaseFont.createFont("c:/windows/fonts/consola.ttf",
        //BaseFont.WINANSI, BaseFont.EMBEDDED);
        Font timesJob = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD, BaseColor.WHITE);
        Font timesDef = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.NORMAL, BaseColor.BLACK);

        Font courierNorm = new Font(Font.FontFamily.COURIER, 11, Font.NORMAL, BaseColor.BLACK);
        Font courierSBig = new Font(Font.FontFamily.COURIER, 13, Font.NORMAL, BaseColor.BLACK);

        // step 1
        Document document = new Document(PageSize.LETTER, 18, 18, 126, 54);
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(bolFileName));
        // step 3
        document.open();
        // step 4
        // we set the compression to 0 so that we can read the PDF syntax
        writer.setCompressionLevel(0);
        // writes something to the direct content using a convenience method
        PdfContentByte canvas = writer.getDirectContentUnder();

        // Graphic Letterheard
        Image image = Image.getInstance("AresLH.jpg");
        image.scalePercent(50f);
        image.setAbsolutePosition(43, 680);

        if (aTemplate == 1)
            document.add(image);

        // 11" = 792
        // 8.5 = 612
        // TEXT SIZE = 11
        // LINE SPACING = 11
        // PARAGRAPH SPACE = 3 * 11
        int myPageWidth = 612;
        int myPageHeight = 792;
        int myTopMargin = 655;
        int myTopSubMargin = 622;
        int myPageLeftMargin = 54;

        // HEADER LINES
        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("ARES BOL# " + bolCode, courierNorm),
                myPageLeftMargin, myTopMargin, 0);
        ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, new Phrase("DATE: " + myShipDate, courierNorm),
                myPageWidth - myPageLeftMargin, myTopMargin, 0);

        // CONSIGNEE ADDRESS
        int myAddressStartY = myTopSubMargin;
        int myCurrentY = myTopSubMargin;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("CONSIGNEE", courierNorm),
                myPageLeftMargin, myCurrentY, 0);

        myCurrentY -= 11;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("ADDRESS:", courierNorm),
                myPageLeftMargin, myCurrentY, 0);

        myCurrentY = myAddressStartY;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
                new Phrase(myAddress.getAttention(), courierNorm), 125, myCurrentY, 0);

        myCurrentY -= 11;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myAddress.getAddress1(), courierNorm),
                125, myCurrentY, 0);

        myCurrentY -= 11;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
                new Phrase(myAddress.getCity() + ", " + myAddress.getState() + " " + myAddress.getZipCode(),
                        courierNorm),
                125, myCurrentY, 0);

        myCurrentY -= 11;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myAddress.getLastLine(), courierNorm),
                125, myCurrentY, 0);

        // FROM ADDRESS
        myCurrentY = myAddressStartY;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("FROM:", courierNorm),
                myPageLeftMargin + 288, myCurrentY, 0);

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
                new Phrase("Ares Printing & Packaging", courierNorm), 98 + 288, myCurrentY, 0);

        myCurrentY -= 11;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
                new Phrase("63 Flushing Ave, Bldg 5", courierNorm), 98 + 288, myCurrentY, 0);

        myCurrentY -= 11;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Brooklyn Navy Yard", courierNorm),
                98 + 288, myCurrentY, 0);

        myCurrentY -= 11;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Brooklyn, NY 11205", courierNorm),
                98 + 288, myCurrentY, 0);

        // CARRIER, FOB AND SPECIAL INSTRUCTIONS
        myCurrentY -= 33;
        int myItemStartY = myCurrentY;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("CARRIER: " + myCarrier, courierNorm),
                myPageLeftMargin, myCurrentY, 0);

        myCurrentY -= 11;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("FOB: Brooklyn", courierNorm),
                myPageLeftMargin, myCurrentY, 0);

        myCurrentY -= 11;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
                new Phrase("SPECIAL INSTRUCTIONS: " + myInstructs, courierNorm), myPageLeftMargin, myCurrentY,
                0);

        int myBOLTPallets = 0;
        int myBOLTCases = 0;
        int myBOLTPieces = 0;

        myCurrentY -= 22;

        ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
                new Phrase("---------------------------------------------------------------------------",
                        courierNorm),
                myPageLeftMargin, myCurrentY, 0);

        // Start Item/ShipLine Information
        for (Shipment shipment : shipmentList) {

            ArrayList<Inventory> shipInventoryList = shipment.getInvenList();
            ArrayList<Inventory.InventoryLine> invenLineList = new ArrayList<>();

            Item item = new Item(shipInventoryList.get(0).getItemID());
            String apo = "";

            for (int i = 0; i < shipInventoryList.size(); i++) {

                if (i == 0) {
                    apo += shipInventoryList.get(i).getJobNum();
                } else {
                    apo += ", " + shipInventoryList.get(i).getJobNum();
                }

                invenLineList.addAll(shipInventoryList.get(i).getInvenLines());

            }
            //START ITEM
            myCurrentY -= 22;

            if (myCurrentY < 100) {
                document.newPage();
                if (aTemplate == 1) {
                    document.add(image);
                }
                myCurrentY = myTopSubMargin;
            }

            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
                    new Phrase("ITEM NAME: " + item.getItemName(), courierNorm), myPageLeftMargin, myCurrentY,
                    0);

            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
                    new Phrase("CUSTOMER PO#: " + chosenOrder.getPOrderNum(), courierNorm),
                    myPageLeftMargin + 288, myCurrentY, 0);

            myCurrentY -= 11;

            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
                    new Phrase("ITEM CODE: " + item.getItemCode(), courierNorm), myPageLeftMargin, myCurrentY,
                    0);

            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("APO: " + apo, courierNorm),
                    myPageLeftMargin + 288, myCurrentY, 0);

            myCurrentY -= 22;

            ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("Pallets", courierNorm),
                    myPageLeftMargin + 115, myCurrentY, 0);

            ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
                    new Phrase("Cases per pallet", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);

            ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("Pieces per case", courierNorm),
                    myPageLeftMargin + 340, myCurrentY, 0);

            // ITEM 1
            myCurrentY -= 12;

            int myItemTPallets = 0;
            int myItemTCases = 0;
            int myItemTPieces = 0;

            for (Inventory.InventoryLine invenLine : invenLineList) {

                ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
                        new Phrase(String.valueOf(invenLine.getPallets()), courierNorm), myPageLeftMargin + 115,
                        myCurrentY, 0);

                ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("@", courierNorm),
                        myPageLeftMargin + 150, myCurrentY, 0);

                ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
                        new Phrase(String.valueOf(invenLine.getCases()), courierNorm), myPageLeftMargin + 215,
                        myCurrentY, 0);

                ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase("@", courierNorm),
                        myPageLeftMargin + 281, myCurrentY, 0);

                ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
                        new Phrase(String.valueOf(invenLine.getPieces()), courierNorm), myPageLeftMargin + 340,
                        myCurrentY, 0);

                // LINE 2
                myCurrentY -= 11;

                myItemTPallets += invenLine.getPallets();
                myItemTCases += invenLine.getPallets() * invenLine.getCases();
                myItemTPieces += invenLine.getPallets() * invenLine.getCases() * invenLine.getPieces();

                myBOLTPallets += myItemTPallets;
                myBOLTCases += myItemTCases;
                myBOLTPieces += myItemTPieces;

            }

            myCurrentY -= 22;

            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("ITEM TOTALS:", courierNorm),
                    myPageLeftMargin, myCurrentY, 0);

            ColumnText.showTextAligned(
                    canvas, Element.ALIGN_LEFT, new Phrase("   PALLETS: " + myItemTPallets + "   CASES: "
                            + myItemTCases + "   QUANTITY: " + myItemTPieces, courierNorm),
                    myPageLeftMargin + 95, myCurrentY, 0);

            myCurrentY -= 11;

            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
                    new Phrase("---------------------------------------------------------------------------",
                            courierNorm),
                    myPageLeftMargin, myCurrentY, 0);

        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////           

        /*
                
        //START ITEM
                    myCurrentY -= 22;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("ITEM NAME: ZCART-248", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("CUSTOMER PO#: 2011444", courierNorm), myPageLeftMargin + 288, myCurrentY, 0);
                
                    myCurrentY -= 11;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("ITEM CODE: ZCART-248", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("APO: 14900", courierNorm), myPageLeftMargin + 288, myCurrentY, 0);
                
                    myCurrentY -= 22;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("Pallets", courierNorm), myPageLeftMargin + 115, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("Cases per pallet", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("Pieces per case", courierNorm), myPageLeftMargin + 340, myCurrentY, 0);
                
                
                
                     // ITEM 1
                    myCurrentY -= 12;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("11", courierNorm), myPageLeftMargin + 115, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 150, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("24", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 281, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("180", courierNorm), myPageLeftMargin + 340, myCurrentY, 0);
                
           // LINE 2
                    myCurrentY -= 11;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("1", courierNorm), myPageLeftMargin + 115, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 150, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("18", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 281, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("180", courierNorm), myPageLeftMargin + 340, myCurrentY, 0);
                
                
                
                    myCurrentY -= 33;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("ITEM TOTALS:", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("   PALLETS: 12   CASES: 282   QUANTITY: 50,760", courierNorm), myPageLeftMargin + 95, myCurrentY, 0);
                
                    myCurrentY -= 11;
                            
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("---------------------------------------------------------------------------", courierNorm), myPageLeftMargin, myCurrentY, 0);
        //////////////         
        //END ITEM
        ////////////// 
                            
                            
                            
                            
                            
                    myCurrentY -= 22;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("ITEM NAME: ZCART-248", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("CUSTOMER PO#: 2011444", courierNorm), myPageLeftMargin + 288, myCurrentY, 0);
                
                    myCurrentY -= 11;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("ITEM CODE: ZCART-248", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("APO: 14900", courierNorm), myPageLeftMargin + 288, myCurrentY, 0);
                
                    myCurrentY -= 22;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("Pallets", courierNorm), myPageLeftMargin + 115, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("Cases per pallet", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("Pieces per case", courierNorm), myPageLeftMargin + 340, myCurrentY, 0);
                
                    myCurrentY -= 6;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("_______________________________________________", courierNorm), myPageLeftMargin + 85, myCurrentY, 0);
        //////////////         
        //END ITEM
        ////////////// 
                            
                    myCurrentY -= 12;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("11", courierNorm), myPageLeftMargin + 115, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 150, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("24", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 281, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("180", courierNorm), myPageLeftMargin + 340, myCurrentY, 0);
                
           // LINE 2
                    myCurrentY -= 11;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("1", courierNorm), myPageLeftMargin + 115, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 150, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("18", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 281, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("180", courierNorm), myPageLeftMargin + 340, myCurrentY, 0);
                
                    myCurrentY -= 1;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("_______________________________________________", courierNorm), myPageLeftMargin + 85, myCurrentY, 0);
                
                    myCurrentY -= 33;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("ITEM TOTALS:", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("   PALLETS: 12   CASES: 282   QUANTITY: 50,760", courierNorm), myPageLeftMargin + 95, myCurrentY, 0);
                
                    myCurrentY -= 11;
                            
        //////////////         
        //START ITEM
        ////////////// 
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("---------------------------------------------------------------------------", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    //START ITEM
                    myCurrentY -= 22;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("ITEM NAME: ZCART-248", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("CUSTOMER PO#: 2011444", courierNorm), myPageLeftMargin + 288, myCurrentY, 0);
                
                    myCurrentY -= 11;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("ITEM CODE: ZCART-248", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("APO: 14900", courierNorm), myPageLeftMargin + 288, myCurrentY, 0);
                
                    myCurrentY -= 22;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("Pallets", courierNorm), myPageLeftMargin + 115, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("Cases per pallet", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("Pieces per case", courierNorm), myPageLeftMargin + 340, myCurrentY, 0);
                
                    myCurrentY -= 6;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("_______________________________________________", courierNorm), myPageLeftMargin + 85, myCurrentY, 0);
                
                     // ITEM 1
                    myCurrentY -= 12;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("11", courierNorm), myPageLeftMargin + 115, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 150, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("24", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 281, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("180", courierNorm), myPageLeftMargin + 340, myCurrentY, 0);
                
           // LINE 2
                    myCurrentY -= 11;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("1", courierNorm), myPageLeftMargin + 115, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 150, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("18", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 281, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("180", courierNorm), myPageLeftMargin + 340, myCurrentY, 0);
                
                    myCurrentY -= 1;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("_______________________________________________", courierNorm), myPageLeftMargin + 85, myCurrentY, 0);
                
                    myCurrentY -= 33;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("ITEM TOTALS:", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("   PALLETS: 12   CASES: 282   QUANTITY: 50,760", courierNorm), myPageLeftMargin + 95, myCurrentY, 0);
                
                    myCurrentY -= 11;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("---------------------------------------------------------------------------", courierNorm), myPageLeftMargin, myCurrentY, 0);
        //////////////         
        //END ITEM
        ////////////// 
                    */

        //////////////         
        //START FOOTER
        //////////////     
        myCurrentY -= 33;

        if (myCurrentY < 85) {
            document.newPage();
            myCurrentY = myTopSubMargin;
        }

        ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
                new Phrase("SHIPMENT RECEIVED IN GOOD CONDITION BY:", courierNorm), myPageLeftMargin + 128,
                myCurrentY, 0);

        myCurrentY -= 33;

        ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
                new Phrase("______________________________________", courierNorm), myPageLeftMargin + 128,
                myCurrentY, 0);
        myCurrentY -= 16;

        ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
                new Phrase("(SIGNATURE) (DATE & TIME)", courierNorm), myPageLeftMargin + 128, myCurrentY, 0);

        myCurrentY += 49;

        ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT,
                new Phrase("TOTAL PALLETS: " + myBOLTPallets, courierSBig), myPageLeftMargin + 500, myCurrentY,
                0);

        myCurrentY -= 13;

        ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT,
                new Phrase("TOTAL CASES: " + myBOLTCases, courierSBig), myPageLeftMargin + 500, myCurrentY, 0);

        myCurrentY -= 13;

        ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT,
                new Phrase("TOTAL WEIGHT: ~" + ((myBOLTCases * 25) + (myBOLTPallets * 40)) + " lbs",
                        courierSBig),
                myPageLeftMargin + 500, myCurrentY, 0);
        //////////////         
        //END FOOTER
        ////////////// 
        /*
                    document.newPage();
                            
                    myCurrentY = myTopSubMargin;
                
        //////////////         
        //START ITEM
        ////////////// 
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("---------------------------------------------------------------------------", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    //START ITEM
                    myCurrentY -= 22;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("ITEM NAME: ZCART-248", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("CUSTOMER PO#: 2011444", courierNorm), myPageLeftMargin + 288, myCurrentY, 0);
                
                    myCurrentY -= 11;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("ITEM CODE: ZCART-248", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("APO: 14900", courierNorm), myPageLeftMargin + 288, myCurrentY, 0);
                
                    myCurrentY -= 22;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("Pallets", courierNorm), myPageLeftMargin + 115, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("Cases per pallet", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("Pieces per case", courierNorm), myPageLeftMargin + 340, myCurrentY, 0);
                
                    myCurrentY -= 6;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("_______________________________________________", courierNorm), myPageLeftMargin + 85, myCurrentY, 0);
                
                     // ITEM 1
                    myCurrentY -= 12;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("11", courierNorm), myPageLeftMargin + 115, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 150, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("24", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 281, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("180", courierNorm), myPageLeftMargin + 340, myCurrentY, 0);
                
           // LINE 2
                    myCurrentY -= 11;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("1", courierNorm), myPageLeftMargin + 115, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 150, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("18", courierNorm), myPageLeftMargin + 215, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("@", courierNorm), myPageLeftMargin + 281, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER,
            new Phrase("180", courierNorm), myPageLeftMargin + 340, myCurrentY, 0);
                
                    myCurrentY -= 1;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("_______________________________________________", courierNorm), myPageLeftMargin + 85, myCurrentY, 0);
                
                    myCurrentY -= 33;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("ITEM TOTALS:", courierNorm), myPageLeftMargin, myCurrentY, 0);
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("   PALLETS: 12   CASES: 282   QUANTITY: 50,760", courierNorm), myPageLeftMargin + 95, myCurrentY, 0);
                
                    myCurrentY -= 11;
                
                    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
            new Phrase("---------------------------------------------------------------------------", courierNorm), myPageLeftMargin, myCurrentY, 0);
        //////////////         
        //END ITEM
        ////////////// 
                    */

        // step 5
        document.close();

        if (Desktop.isDesktopSupported()) {
            try {
                File f = new File(bolFileName);
                Desktop.getDesktop().open(f);
            } catch (IOException ex) {
                // no application registered for PDFs
            }
        }

    } catch (IOException | DocumentException ex) {
        Logger.getLogger(PDFCreator.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:py.com.palermo.imprimeetiquetas.web.ProductoController.java

public String createPdf() throws IOException, DocumentException {

    if (hayParaImprimir()) {

        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();
        response.setContentType("application/x-pdf");
        response.setHeader("Content-Disposition", "attachment; filename=\"etiquetas.pdf\"");

        // step 1
        Document document = new Document(new Rectangle(86, 35));
        // step 2

        document.setMargins(0f, 0f, 0f, 0f);

        PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
        // step 3
        document.open();//w  ww . jav a2s .com

        // step 4
        PdfContentByte cb = writer.getDirectContent();

        for (ProductoCantidad p : productos) {
            if (p.getCantidad() > 0) {

                BarcodeEAN codeEAN = new BarcodeEAN();
                codeEAN.setCode(p.getCodigo());
                codeEAN.setCodeType(Barcode.EAN13);
                codeEAN.setBarHeight(10f);
                codeEAN.setX(0.7f);
                codeEAN.setSize(4f);

                NumberFormat nf = NumberFormat.getCurrencyInstance(new Locale("es", "py"));
                Font fontbold = FontFactory.getFont("Times-Roman", 5, Font.NORMAL);
                Chunk productTitle = new Chunk(p.getNombre() + "," + " " + nf.format(p.getPrecio()), fontbold);

                // EAN 13
                Paragraph pTitile = new Paragraph(productTitle);
                pTitile.setAlignment(Element.ALIGN_CENTER);
                pTitile.setLeading(0, 1);

                PdfPTable table = new PdfPTable(1);
                table.setPaddingTop(0f);

                table.setWidthPercentage(96);
                PdfPCell cell = new PdfPCell();
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setBorder(Rectangle.NO_BORDER);
                cell.addElement(codeEAN.createImageWithBarcode(cb, null, BaseColor.BLACK));

                table.addCell(cell);

                PdfPCell cell2 = new PdfPCell();
                cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell2.setBorder(Rectangle.NO_BORDER);
                cell2.addElement(pTitile);

                table.addCell(cell2);

                for (int i = 0; i < p.getCantidad(); i++) {
                    document.add(table);
                    document.newPage();
                }
            }
        }

        // step 5
        document.close();

        response.getOutputStream().flush();
        response.getOutputStream().close();
        FacesContext.getCurrentInstance().responseComplete();
    } else {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "No hay nada que imprimir", ""));
    }
    return null;
}

From source file:qcas.InstructorDashboardController.java

/**
 * method to export the image to pdf format
 * @throws IOException//from  w  ww . ja va  2 s  .c  o m
 * @throws DocumentException
 */
@FXML
public void exportToPdf() throws IOException, DocumentException {
    Document document = new Document();
    String output = "Current" + time + ".pdf";
    FileOutputStream fos = new FileOutputStream(output);
    PdfWriter writer = PdfWriter.getInstance(document, fos);
    writer.open();
    document.open();
    WritableImage image = lineChartAnchorPaneBR.snapshot(new SnapshotParameters(), null);
    Image returnImage = pdfExport(image, document);
    document.add(returnImage);
    image = pieChartAnchorPane.snapshot(new SnapshotParameters(), null);
    returnImage = pdfExport(image, document);
    document.newPage();
    document.add(returnImage);
    image = stackedBarAnchorPane.snapshot(new SnapshotParameters(), null);
    returnImage = pdfExport(image, document);
    document.newPage();
    document.add(returnImage);
    image = lineChartAnchorPaneBR.snapshot(new SnapshotParameters(), null);
    returnImage = pdfExport(image, document);
    document.newPage();
    document.add(returnImage);
    document.close();
    writer.close();
    pdfDownloadedLabel.setVisible(true);

}

From source file:report.pdfs.Basics_PDF_Report.java

private void addTitlePage(Document document) throws DocumentException {
    Paragraph preface = new Paragraph();
    // We add one empty line
    addEmptyLine(preface, 1);/*from   ww  w.  jav  a 2 s  . c om*/
    // Lets write a big header
    preface.add(new Paragraph("Basics Overview", catFont));

    addEmptyLine(preface, 1);
    // Will create: Report generated by: _name, _date
    preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-3$
            smallBold));
    addEmptyLine(preface, 3);
    preface.add(new Paragraph("This document describes something which is very important ", smallBold));

    addEmptyLine(preface, 8);

    preface.add(new Paragraph("This document is a preliminary version and not subject to "
            + "your license agreement or any other agreement with vogella.com ;-).", redFont));

    document.add(preface);
    // Start a new page
    document.newPage();
}