Example usage for org.apache.pdfbox.pdmodel PDDocument PDDocument

List of usage examples for org.apache.pdfbox.pdmodel PDDocument PDDocument

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocument PDDocument.

Prototype

public PDDocument() 

Source Link

Document

Creates an empty PDF document.

Usage

From source file:Bulletin.myPdf.java

public myPdf(String Path) {
    this.Path = Path;
    this.document = new PDDocument();
}

From source file:Bulletin.test.java

public static void test2() {
    try {// ww  w  .ja va2 s . c  o  m
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(PDRectangle.A4);
        PDPageContentStream cos = null;
        try {
            cos = new PDPageContentStream(document, page);
        } catch (IOException ex) {
            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
        }

        float X = 501.4f;
        float Y = 556.0f;
        //            String text = "HALLO";
        //            String text = "HALLO#HOE#GAAT#HET";
        //            String text = "HALLO#HOE#GAAT";
        String text = "Non atteints";
        int rh = 10;

        cos.moveTo(X, Y - 50);
        cos.lineTo(X, Y + 50);
        cos.stroke();
        cos.moveTo(X - 50, Y);
        cos.lineTo(X + 50, Y);
        cos.stroke();

        cos.beginText();
        cos.setFont(PDType1Font.HELVETICA, 6);
        float dtw = 0.5f * getTextWidth(PDType1Font.HELVETICA, text, 6);
        float dth = 0.0f * getFontHeight(PDType1Font.HELVETICA, 6);
        int nbLines = text.split("#").length;

        Y += 0.5f * (nbLines - 1) * rh;

        for (String line : text.split("#")) {
            Matrix M = Matrix.getTranslateInstance(X, Y);
            M.concatenate(Matrix.getRotateInstance(Math.toRadians(0), 0, 0));
            M.concatenate(Matrix.getTranslateInstance(-dtw, -dth));
            cos.setTextMatrix(M);
            cos.showText(line);
            Y -= rh;
        }
        cos.close();
        document.addPage(page);
        document.save("/tmp/test.pdf");
        document.close();

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

From source file:casmi.Applet.java

License:Open Source License

@Override
public void drawWithGraphics(Graphics g) {
    setGLParam(g.getGL());/*from www.  ja  v a 2s  . co m*/

    drawObjects(g);

    updateObjects();

    // Calculate real fps.
    {
        frame++;
        long now = System.currentTimeMillis();
        long elapse = now - baseTime;
        if (1000 < elapse) {
            workingFPS = frame * 1000.0 / elapse;
            baseTime = now;
            frame = 0;
        }
    }

    // capture image
    if (saveImageFlag) {
        saveImageFlag = false;

        try {
            switch (imageType) {
            case JPG:
            case PNG:
            case BMP:
            case GIF:
            default:
                Screenshot.writeToFile(new File(saveFile), width, height, !saveBackground);
                break;
            case PDF: {
                BufferedImage bi = Screenshot.readToBufferedImage(width, height, !saveBackground);
                PDDocument doc = new PDDocument();
                PDRectangle rect = new PDRectangle(width, height);
                PDPage page = new PDPage(rect);
                doc.addPage(page);
                PDXObjectImage ximage = new PDJpeg(doc, bi);
                PDPageContentStream contentStream = new PDPageContentStream(doc, page);
                contentStream.drawImage(ximage, 0, 0);
                contentStream.close();
                doc.save(saveFile);
                doc.close();
                break;
            }
            }
        } catch (GLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (COSVisitorException e) {
            e.printStackTrace();
        }
    }
}

From source file:Castle.TextStripperNGTest.java

/**
 * Test of processLocation method, of class TextStripper.
 *///from w w  w  . java 2  s .  c  om
@Test
public void testProcessLocation() throws Exception {
    System.out.println("processLocation");
    PDDocument doc = new PDDocument();
    TextStripper instance = new TextStripper();
    instance.processLocation(doc);
}

From source file:cdiscisa.StreamUtil.java

private static void imprimirDiplomas(ArrayList<Participante> listaParticipantes, Curso c, Directorio d,
            String chkDipFirma, String chkDipLogo, String savePath, Map<String, String> dosc, String instructor,
            Map<String, String> abreviaturas) throws IOException {

        ListIterator<Participante> it = listaParticipantes.listIterator();
        Participante p1 = null;/* w w w .java2  s  .c  om*/
        Participante p2 = null;
        String abrev_curso = "";

        // Create a document and add a page to it
        PDDocument document = new PDDocument();
        PDDocument documentSingle;

        InputStream file = null;

        BufferedImage logo = null;
        BufferedImage firma = null;

        try {

            //logo = new File(cdiscisa.Cdiscisa.class.getClassLoader().getResource("files/logo.png").getFile());
            //logo = cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/logo.png");
            logo = ImageIO.read(cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/logo.png"));
            //logo = cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/logo.png");

            if (instructor.equalsIgnoreCase("Ing. Jorge Antonio Razn Gutierrez")) {
                //firma = new File(cdiscisa.Cdiscisa.class.getClassLoader().getResource("files/firmaCoco.png").getFile());
                firma = ImageIO
                        .read(cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/firmaCoco.png"));
            } else if (instructor.equalsIgnoreCase("Manuel Anguiano Razn")) {
                firma = ImageIO.read(
                        cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/firmaManuel.png"));
                //firma = new File(cdiscisa.Cdiscisa.class.getClassLoader().getResource("files/firmaManuel.png").getFile());
            } else {
                firma = ImageIO
                        .read(cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/firmaJorge.png"));
                //firma = new File(cdiscisa.Cdiscisa.class.getClassLoader().getResource("files/firmaJorge.png").getFile());
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error al cargar la imagen del logo o la firma \nfile: "
                    + String.valueOf(logo) + "\n" + String.valueOf(firma) + "\n" + ex.toString());
        }

        PDImageXObject firmaObject = null;
        PDImageXObject logoObject = null;

        try {
            if (chkDipLogo.equalsIgnoreCase("true")) {
                //logoObject = PDImageXObject.createFromFile(logo, document);                
                logoObject = LosslessFactory.createFromImage(document, logo);
            }

            if (chkDipFirma.equalsIgnoreCase("true")) {
                // firmaObject = PDImageXObject.createFromFile(firma, document);
                firmaObject = LosslessFactory.createFromImage(document, firma);
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error al crear objetos de logo o firma \nfile: "
                    + String.valueOf(logoObject) + "\n" + String.valueOf(firmaObject) + "\n" + ex.toString());
        }

        try {
            file = cdiscisa.Cdiscisa.class.getClassLoader()
                    .getResourceAsStream("files/n_diploma_simple_vacio_nf_nl_nr.pdf");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,
                    "Error al cargar el el diploma single base. \ndiploma: files/n_diploma_simple_vacio_nf_nl_nr.pdf \nfile: "
                            + String.valueOf(file) + "\n" + ex.toString());
        }

        documentSingle = PDDocument.load(file);

        PDPage pageSingle = (PDPage) documentSingle.getDocumentCatalog().getPages().get(0);
        COSDictionary pageDictSingle = pageSingle.getCOSObject();
        COSDictionary newPageSingleDict = new COSDictionary(pageDictSingle);
        PDPage templatePageSingle = new PDPage(newPageSingleDict);

        // Create a document and add a page to it

        PDDocument documentDoble;

        InputStream file2 = null;

        try {
            file2 = cdiscisa.Cdiscisa.class.getClassLoader()
                    .getResourceAsStream("files/n_diploma_doble_vacio_nf_nl_nr.pdf");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,
                    "Error al cargar el el diploma doble base. \ndiploma: n_diploma_doble_vacio_nf_nl_nr.pdf\nfile: "
                            + String.valueOf(file2) + "\n" + ex.toString());
        }

        documentDoble = PDDocument.load(file2);

        PDPage pageDoble = (PDPage) documentDoble.getDocumentCatalog().getPages().get(0);
        COSDictionary pageDobleDict = pageDoble.getCOSObject();
        COSDictionary newPageDobleDict = new COSDictionary(pageDobleDict);

        PDPage templatePageDoble = new PDPage(newPageDobleDict);

        InputStream isFont1 = null, isFont2 = null, isFont3 = null;

        try {
            isFont1 = cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/Calibri.ttf");
            isFont2 = cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/CalibriBold.ttf");
            isFont3 = cdiscisa.Cdiscisa.class.getClassLoader().getResourceAsStream("files/Pristina.ttf");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,
                    "Error al cargar el una fuente \nisFont1: " + String.valueOf(isFont1) + "\nisFont2: "
                            + String.valueOf(isFont2) + "\nisFont3: " + String.valueOf(isFont3) + "\n"
                            + ex.toString());
        }
        PDFont calibri = null;
        PDFont calibriBold = null;
        PDFont pristina = null;

        calibri = PDType0Font.load(document, isFont1);
        calibriBold = PDType0Font.load(document, isFont2);
        pristina = PDType0Font.load(document, isFont3);

        if (listaParticipantes.size() % 2 == 0 && listaParticipantes.size() >= 2) {

            while (it.hasNext()) {
                p1 = it.next();
                p2 = it.next();

                imprimirDiplomaDoble(p1, p2, c, d, document, templatePageDoble, calibri, calibriBold, pristina,
                        logoObject, firmaObject, instructor);
            }
        } else {
            if (listaParticipantes.size() > 1) {
                //Lista es impar y contiene mas de 2 participantes.
                while (it.hasNext()) {

                    if (it.nextIndex() == listaParticipantes.size() - 1) {
                        p1 = it.next();
                        imprimirDiplomaArriba(p1, c, d, document, templatePageSingle, calibri, calibriBold,
                                pristina, logoObject, firmaObject, instructor);
                        break;
                    }

                    p1 = it.next();
                    p2 = it.next();

                    imprimirDiplomaDoble(p1, p2, c, d, document, templatePageDoble, calibri, calibriBold, pristina,
                            logoObject, firmaObject, instructor);
                }
            } else if (listaParticipantes.size() == 1) {
                p1 = it.next();
                imprimirDiplomaArriba(p1, c, d, document, templatePageSingle, calibri, calibriBold, pristina,
                        logoObject, firmaObject, instructor);
            }

        }

        Format formatter = new SimpleDateFormat("ddMMMYYYY", new Locale("es", "MX"));
        String formatedDate = formatter.format(c.fecha_inicio);

        String abrev = abreviaturas.get(c.nombre_curso);

        if (c.walmart) {
            document.save(savePath + File.separator + "Diplomas_" + d.formato + "_" + d.unidad + "_"
                    + d.determinante + "_" + abrev + "_" + formatedDate + ".pdf");
            document.close();
            dosc.put(savePath + File.separator + "Diplomas_" + d.formato + "_" + d.unidad + "_" + d.determinante
                    + "_" + abrev + "_" + formatedDate + ".pdf", d.determinante);
        } else {
            document.save(savePath + File.separator + "Diplomas_" + d.determinante + "_" + abrev + "_"
                    + formatedDate + ".pdf");
            document.close();
            dosc.put(savePath + File.separator + "Diplomas_" + d.determinante + "_" + abrev + "_" + formatedDate
                    + ".pdf", d.determinante);
        }
    }

From source file:ch.dowa.jassturnier.pdf.PdfGenerator.java

public void exportTemplateWithTable(TableBuilder tableBuilder, String fileName, String title)
        throws IOException {
    String vLogoPath = !ResourceLoader.readProperty("LOGOPATH").isEmpty()
            ? ResourceLoader.readProperty("LOGOPATH")
            : null;/*from  w  ww  . j  av  a2 s .c om*/

    final PDDocument document = new PDDocument();
    boolean firstPage = true;
    PDImageXObject image = null;
    float imageStartX = 0F;
    float imageStartY = 0F;
    PDRectangle imgSize = null;

    TableDrawer drawer = TableDrawer.builder().table(tableBuilder.build()).startX(docContentStartX())
            .startY(docContentStartY()).endY(documentPadding).build();

    if (vLogoPath != null) {
        image = PDImageXObject.createFromFile(vLogoPath, document);
        imgSize = getImageSizePdf(image);
        imageStartX = imgEndX() - imgSize.getWidth();
        imageStartY = imgEndY() - imgSize.getHeight();
    }

    do {
        PDPage page = new PDPage(pageFormat);
        document.addPage(page);
        try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
            if (firstPage) {
                contentStream.beginText();
                contentStream.setFont(STANDART_FONT_BOLD, 14);
                contentStream.newLineAtOffset(docTitelStartX(), docTitelStartY());
                contentStream.showText(title);
                contentStream.endText();
                if (image != null) {
                    contentStream.drawImage(image, imageStartX, imageStartY, imgSize.getWidth(),
                            imgSize.getHeight());
                }
                drawer.startY(docContentSartFirsPageY());
                firstPage = false;
            }
            drawer.contentStream(contentStream).draw();
        }
        drawer.startY(docContentStartY());
    } while (!drawer.isFinished());

    document.save(fileName);
    document.close();
}

From source file:ch.dowa.jassturnier.pdf.PdfGenerator.java

public void exportTemplateWithTableMultiPage(HashMap<String, Table.TableBuilder> tableBuildersMap,
        String fileName) throws IOException {
    String vLogoPath = !ResourceLoader.readProperty("LOGOPATH").isEmpty()
            ? ResourceLoader.readProperty("LOGOPATH")
            : null;/*from w w w. j  a v a 2s .c  o m*/
    PDDocument mainDoc = new PDDocument();
    PDFMergerUtility merger = new PDFMergerUtility();
    for (Map.Entry<String, TableBuilder> entry : tableBuildersMap.entrySet()) {
        String title = entry.getKey();
        TableBuilder tableBuilder = entry.getValue();

        final PDDocument documentPage = new PDDocument();
        boolean firstPage = true;
        PDImageXObject image = null;
        float imageStartX = 0F;
        float imageStartY = 0F;
        PDRectangle imgSize = null;

        TableDrawer drawer = TableDrawer.builder().table(tableBuilder.build()).startX(docContentStartX())
                .startY(docContentStartY()).endY(documentPadding).build();

        if (vLogoPath != null) {
            image = PDImageXObject.createFromFile(vLogoPath, documentPage);
            imgSize = getImageSizePdf(image);
            imageStartX = imgEndX() - imgSize.getWidth();
            imageStartY = imgEndY() - imgSize.getHeight();
        }

        do {
            PDPage page = new PDPage(pageFormat);
            documentPage.addPage(page);
            try (PDPageContentStream contentStream = new PDPageContentStream(documentPage, page)) {
                if (firstPage) {
                    contentStream.beginText();
                    contentStream.setFont(STANDART_FONT_BOLD, 14);
                    contentStream.newLineAtOffset(docTitelStartX(), docTitelStartY());
                    contentStream.showText(title);
                    contentStream.endText();
                    if (image != null) {
                        contentStream.drawImage(image, imageStartX, imageStartY, imgSize.getWidth(),
                                imgSize.getHeight());
                    }
                    drawer.startY(docContentSartFirsPageY());
                    firstPage = false;
                }
                drawer.contentStream(contentStream).draw();
            }
            drawer.startY(docContentStartY());
        } while (!drawer.isFinished());

        merger.appendDocument(mainDoc, documentPage);
    }

    mainDoc.save(fileName);
    mainDoc.close();
}

From source file:ch.dowa.jassturnier.pdf.TabelSheetsPdf.java

public static void exportTabelSheets(Gang actGang, String turnierTitel, int numberOfGames) throws IOException {
    PdfGenerator gen = new PdfGenerator(PDRectangle.A5);
    String outputFileName;//from w w  w . j  ava2  s.  c o m
    outputFileName = turnierTitel.replace(' ', '_') + "_Spielblaetter_Gang_" + actGang.getGangNr() + ".pdf";
    HashMap<String, Table.TableBuilder> tableBuildersMap = new HashMap();
    byte[] arrowIconBytes = ResourceLoader.getIcon("arrowIcon.png");
    PDImageXObject arrowIcon = PDImageXObject.createFromByteArray(new PDDocument(), arrowIconBytes,
            "arrowIcon");

    float thinBorderWidth = 0.3f;
    float thickBorderWidth = 1.3f;
    float rowHeight = 20f;

    String[] placeLables = new String[4];
    placeLables[0] = ResourceLoader.readProperty("PLACE1");
    placeLables[1] = ResourceLoader.readProperty("PLACE2");
    placeLables[2] = ResourceLoader.readProperty("PLACE3");
    placeLables[3] = ResourceLoader.readProperty("PLACE4");

    for (Spiel s : actGang.getGames()) {
        String titel = "Gang " + String.valueOf(actGang.getGangNr()) + " - Tischnummer "
                + String.valueOf(s.getTischNr());
        final Table.TableBuilder tableBuilder = Table.builder()
                .addColumnsOfWidth(gen.tabelWidth() / 10f, gen.tabelWidth() / 10f, gen.tabelWidth() / 10f,
                        gen.tabelWidth() / 10f, gen.tabelWidth() / 10f, gen.tabelWidth() / 10f,
                        gen.tabelWidth() / 10f, gen.tabelWidth() / 10f, gen.tabelWidth() / 10f,
                        gen.tabelWidth() / 10f)
                .fontSize(10).font(STANDART_FONT).borderColor(Color.BLACK).borderWidth(0)
                .textColor(Color.BLACK);

        // 1. Zeile
        tableBuilder.addRow(Row.builder().add(CellText.builder().text("").build())
                .add(CellText.builder()
                        .text(placeLables[0] + ": " + s.getTeam1().getSpieler1().getVorname() + " "
                                + s.getTeam1().getSpieler1().getNachname())
                        .borderWidth(thickBorderWidth).borderWidthBottom(thinBorderWidth).span(4).build())
                .add(CellText.builder()
                        .text(placeLables[1] + ": " + s.getTeam2().getSpieler1().getVorname() + " "
                                + s.getTeam2().getSpieler1().getNachname())
                        .borderWidth(thickBorderWidth).borderWidthBottom(thinBorderWidth).span(4).build())
                .add(CellText.builder().text("").build()).font(STANDART_FONT).height(rowHeight).build());
        // 2. Zeile
        tableBuilder.addRow(Row.builder().add(CellText.builder().text("").build())
                .add(CellText.builder()
                        .text(placeLables[2] + ": " + s.getTeam1().getSpieler2().getVorname() + " "
                                + s.getTeam1().getSpieler2().getNachname())
                        .borderWidth(thickBorderWidth).borderWidthBottom(thinBorderWidth)
                        .borderWidthTop(thinBorderWidth).span(4).build())
                .add(CellText.builder()
                        .text(placeLables[3] + ": " + s.getTeam2().getSpieler2().getVorname() + " "
                                + s.getTeam2().getSpieler2().getNachname())
                        .borderWidth(thickBorderWidth).borderWidthTop(thinBorderWidth)
                        .borderWidthBottom(thinBorderWidth).span(4).build())
                .add(CellText.builder().text("").build()).font(STANDART_FONT).height(rowHeight).build());

        for (int i = 1; i <= numberOfGames; i++) {
            if (i == 1) {
                tableBuilder.addRow(Row.builder()
                        .add(CellText.builder().text(String.valueOf(i) + ".").borderWidth(thickBorderWidth)
                                .borderWidthBottom(thinBorderWidth).horizontalAlignment(CENTER)
                                .verticalAlignment(MIDDLE).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthLeft(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthRight(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthLeft(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthRight(thickBorderWidth).build())
                        .add(CellText.builder().text("157").borderWidth(thickBorderWidth)
                                .borderWidthBottom(thinBorderWidth).horizontalAlignment(CENTER)
                                .verticalAlignment(MIDDLE).build())
                        .font(STANDART_FONT).height(rowHeight).build());
            } else if (i == numberOfGames) {
                tableBuilder.addRow(Row.builder()
                        .add(CellText.builder().text(String.valueOf(i) + ".").borderWidth(thickBorderWidth)
                                .borderWidthTop(thinBorderWidth).horizontalAlignment(CENTER)
                                .verticalAlignment(MIDDLE).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthLeft(thickBorderWidth).borderWidthBottom(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthBottom(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthBottom(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthRight(thickBorderWidth).borderWidthBottom(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthLeft(thickBorderWidth).borderWidthBottom(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthBottom(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthBottom(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthRight(thickBorderWidth).borderWidthBottom(thickBorderWidth).build())
                        .add(CellText.builder().text("157").borderWidth(thickBorderWidth)
                                .borderWidthTop(thinBorderWidth).horizontalAlignment(CENTER)
                                .verticalAlignment(MIDDLE).build())
                        .font(STANDART_FONT).height(rowHeight).build());
            } else {
                tableBuilder.addRow(Row.builder()
                        .add(CellText.builder().text(String.valueOf(i) + ".").borderWidth(thickBorderWidth)
                                .borderWidthTop(thinBorderWidth).borderWidthBottom(thinBorderWidth)
                                .horizontalAlignment(CENTER).verticalAlignment(MIDDLE).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthLeft(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthRight(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthLeft(thickBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth).build())
                        .add(CellText.builder().text("").borderWidth(thinBorderWidth)
                                .borderWidthRight(thickBorderWidth).build())
                        .add(CellText.builder().text("157").borderWidth(thickBorderWidth)
                                .borderWidthTop(thinBorderWidth).borderWidthBottom(thinBorderWidth)
                                .horizontalAlignment(CENTER).verticalAlignment(MIDDLE).build())
                        .font(STANDART_FONT).height(rowHeight).build());
            }
        }

        tableBuilder.addRow(Row.builder().add(CellText.builder().text("").build())
                .add(CellText.builder().text("").borderWidth(thickBorderWidth).borderWidthRight(thinBorderWidth)
                        .build())
                .add(CellText.builder().text("").borderWidth(thickBorderWidth).borderWidthRight(thinBorderWidth)
                        .borderWidthLeft(thinBorderWidth).build())
                .add(CellText.builder().text("").borderWidth(thickBorderWidth).borderWidthRight(thinBorderWidth)
                        .borderWidthLeft(thinBorderWidth).build())
                .add(CellText.builder().text("").borderWidth(thickBorderWidth).borderWidthLeft(thinBorderWidth)
                        .build())
                .add(CellText.builder().text("").borderWidth(thickBorderWidth).borderWidthRight(thinBorderWidth)
                        .build())
                .add(CellText.builder().text("").borderWidth(thickBorderWidth).borderWidthRight(thinBorderWidth)
                        .borderWidthLeft(thinBorderWidth).build())
                .add(CellText.builder().text("").borderWidth(thickBorderWidth).borderWidthRight(thinBorderWidth)
                        .borderWidthLeft(thinBorderWidth).build())
                .add(CellText.builder().text("").borderWidth(thickBorderWidth).borderWidthLeft(thinBorderWidth)
                        .build())
                .add(CellText.builder().text("").build()).font(STANDART_FONT).height(rowHeight).build());

        tableBuilder.addRow(Row.builder()
                .add(CellText.builder().text("+").horizontalAlignment(CENTER).verticalAlignment(MIDDLE).build())
                .add(CellText.builder().text("").borderWidth(thickBorderWidth).borderWidthRight(thinBorderWidth)
                        .build())
                .add(CellText.builder().text("").borderWidth(thickBorderWidth).borderWidthRight(thinBorderWidth)
                        .borderWidthLeft(thinBorderWidth).build())
                .add(CellText.builder().text("").borderWidth(thickBorderWidth).borderWidthRight(thinBorderWidth)
                        .borderWidthLeft(thinBorderWidth).build())
                .add(CellText.builder().text("").borderWidth(thickBorderWidth).borderWidthLeft(thinBorderWidth)
                        .build())
                .add(CellText.builder().text("").build())
                .add(CellImage.builder().image(arrowIcon).horizontalAlignment(CENTER).verticalAlignment(MIDDLE)
                        .maxHeight(rowHeight * 0.6f).span(2).build())
                .add(CellText.builder().text("").build()).add(CellText.builder().text("").build())
                .font(STANDART_FONT).height(rowHeight).build());

        String pointsTotal = "   " + String.valueOf(numberOfGames * 157);

        tableBuilder.addRow(Row.builder()
                .add(CellText.builder().text("=").horizontalAlignment(CENTER).verticalAlignment(MIDDLE).build())
                .add(CellText.builder()
                        .text(pointsTotal.substring(pointsTotal.length() - 4, pointsTotal.length() - 3))
                        .borderWidth(thickBorderWidth).borderWidthRight(thinBorderWidth)
                        .horizontalAlignment(CENTER).verticalAlignment(MIDDLE).build())
                .add(CellText.builder()
                        .text(pointsTotal.substring(pointsTotal.length() - 3, pointsTotal.length() - 2))
                        .borderWidth(thickBorderWidth).borderWidthRight(thinBorderWidth)
                        .borderWidthLeft(thinBorderWidth).horizontalAlignment(CENTER).verticalAlignment(MIDDLE)
                        .build())
                .add(CellText.builder()
                        .text(pointsTotal.substring(pointsTotal.length() - 2, pointsTotal.length() - 1))
                        .borderWidth(thickBorderWidth).borderWidthRight(thinBorderWidth)
                        .borderWidthLeft(thinBorderWidth).horizontalAlignment(CENTER).verticalAlignment(MIDDLE)
                        .build())
                .add(CellText.builder()
                        .text(pointsTotal.substring(pointsTotal.length() - 1, pointsTotal.length()))
                        .borderWidth(thickBorderWidth).horizontalAlignment(CENTER).verticalAlignment(MIDDLE)
                        .borderWidthLeft(thinBorderWidth).build())
                .add(CellText.builder().text("").build()).add(CellText.builder().text("").build())
                .add(CellText.builder().text("").build()).add(CellText.builder().text("").build())
                .add(CellText.builder().text("").build()).font(STANDART_FONT_BOLD).height(rowHeight).build());

        tableBuildersMap.put(titel, tableBuilder);
    }

    gen.exportTemplateWithTableMultiPage(tableBuildersMap, outputFileName);
}

From source file:ch.eggbacon.app.pdf.PDFCreator.java

License:Open Source License

public PDFCreator() {
    doc = new PDDocument();
}

From source file:ch.rasc.downloadchart.DownloadChartServlet.java

License:Apache License

private static void handlePdf(HttpServletResponse response, byte[] imageData, Integer width, Integer height,
        String filename, PdfOptions options) throws IOException {

    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + ".pdf\";");

    try (PDDocument document = new PDDocument()) {
        PDRectangle format = PDPage.PAGE_SIZE_A4;
        if (options != null) {
            if ("A3".equals(options.format)) {
                format = PDPage.PAGE_SIZE_A3;
            } else if ("A5".equals(options.format)) {
                format = PDPage.PAGE_SIZE_A5;
            } else if ("Letter".equals(options.format)) {
                format = PDPage.PAGE_SIZE_LETTER;
            } else if ("Legal".equals(options.format)) {
                format = new PDRectangle(215.9f * MM_TO_UNITS, 355.6f * MM_TO_UNITS);
            } else if ("Tabloid".equals(options.format)) {
                format = new PDRectangle(279 * MM_TO_UNITS, 432 * MM_TO_UNITS);
            } else if (options.width != null && options.height != null) {
                Float pdfWidth = convertToPixel(options.width);
                Float pdfHeight = convertToPixel(options.height);
                if (pdfWidth != null && pdfHeight != null) {
                    format = new PDRectangle(pdfWidth, pdfHeight);
                }//from w ww  . j  av a 2  s . co m
            }
        }

        PDPage page = new PDPage(format);

        if (options != null && "landscape".equals(options.orientation)) {
            page.setRotation(90);
        }

        document.addPage(page);

        BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData));

        try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {

            PDPixelMap ximage = new PDPixelMap(document, originalImage);

            Dimension newDimension = calculateDimension(originalImage, width, height);
            int imgWidth = ximage.getWidth();
            int imgHeight = ximage.getHeight();
            if (newDimension != null) {
                imgWidth = newDimension.width;
                imgHeight = newDimension.height;
            }

            Float border = options != null ? convertToPixel(options.border) : null;
            if (border == null) {
                border = 0.0f;
            }

            AffineTransform transform;

            if (page.getRotation() == null) {
                float scale = (page.getMediaBox().getWidth() - border * 2) / imgWidth;
                if (scale < 1.0) {
                    transform = new AffineTransform(imgWidth, 0, 0, imgHeight, border,
                            page.getMediaBox().getHeight() - border - imgHeight * scale);

                    transform.scale(scale, scale);
                } else {
                    transform = new AffineTransform(imgWidth, 0, 0, imgHeight, border,
                            page.getMediaBox().getHeight() - border - imgHeight);
                }

            } else {
                float scale = (page.getMediaBox().getHeight() - border * 2) / imgWidth;
                if (scale < 1.0) {
                    transform = new AffineTransform(imgHeight, 0, 0, imgWidth, imgHeight * scale + border,
                            border);

                    transform.scale(scale, scale);
                } else {
                    transform = new AffineTransform(imgHeight, 0, 0, imgWidth, imgHeight + border, border);
                }

                transform.rotate(1.0 * Math.PI / 2.0);
            }

            contentStream.drawXObject(ximage, transform);

        }

        try {
            document.save(response.getOutputStream());
        } catch (COSVisitorException e) {
            throw new IOException(e);
        }
    }
}