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

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

Introduction

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

Prototype

public PDPage(COSDictionary pageDictionary) 

Source Link

Document

Creates a new instance of PDPage for reading.

Usage

From source file:cdiscisa.StreamUtil.java

private static void imprimirDiplomaDoble(Participante p1, Participante p2, Curso c, Directorio d,
            PDDocument document, PDPage page, PDFont calibri, PDFont calibriBold, PDFont pristina,
            PDImageXObject logoObject, PDImageXObject firmaObject, String instructor) throws IOException {

        COSDictionary pageDict = page.getCOSObject();
        COSDictionary newPageDict = new COSDictionary(pageDict);

        PDPage newPage = new PDPage(newPageDict);
        document.addPage(newPage);//from w  w  w . jav a  2  s . c  om

        // Start a new content stream which will "hold" the to be created content
        PDPageContentStream contentStream = new PDPageContentStream(document, newPage, true, true);

        float pageWidth = newPage.getMediaBox().getWidth();
        //float pageHeight = newPage.getMediaBox().getHeight();

        //System.out.println("pageWidth: " + pageWidth + "\npageHeight: " + pageHeight);

        // Print UP Side

        contentStream.beginText();
        contentStream.setFont(pristina, 28);
        //contentStream.setNonStrokingColor(0,112,192);
        contentStream.setNonStrokingColor(0, 128, 0);

        float nameWidth = pristina.getStringWidth(p1.nombre + " " + p1.apellidos) / 1000 * 28;

        //System.out.println(p1.nombre + " " + p1.apellidos + "::" + nameWidth);

        float xPosition;
        if (nameWidth < 470) {
            xPosition = (pageWidth - nameWidth) / 2;
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 641));
            contentStream.showText(p1.nombre + " " + p1.apellidos);
        } else {
            contentStream.setFont(pristina, 22);
            nameWidth = pristina.getStringWidth(p1.nombre + " " + p1.apellidos) / 1000 * 22;
            xPosition = (pageWidth - nameWidth) / 2;
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 641));
            contentStream.showText(p1.nombre + " " + p1.apellidos);
        }

        contentStream.setFont(calibri, 15);
        contentStream.setNonStrokingColor(Color.BLACK);

        nameWidth = calibri.getStringWidth(c.razon_social) / 1000 * 15;
        //System.out.println("nameWidth: " + nameWidth);
        xPosition = (pageWidth - nameWidth) / 2;

        contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 615));
        contentStream.showText(c.razon_social);

        contentStream.setFont(calibri, 11);
        contentStream.setNonStrokingColor(Color.BLACK);

        if (c.walmart) {
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 275, 593));
            contentStream.showText(p1.determinante + " " + d.unidad);
        } else if (d.sucursal.isEmpty() || d.sucursal.equalsIgnoreCase("")) {
            contentStream.endText();
            contentStream.addRect(100, 590, 400, 20);
            contentStream.setNonStrokingColor(Color.WHITE);
            contentStream.fill();
            contentStream.beginText();
            contentStream.setNonStrokingColor(Color.BLACK);
        } else {
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 275, 593));
            contentStream.showText(d.sucursal);
        }

        contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 210, (float) 538.5));
        contentStream.showText(c.horas_texto);

        contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 394, (float) 538.5));
        contentStream.showText(c.fecha_texto_diploma);

        contentStream.setFont(calibriBold, 10);

        float nameWidthStroked = calibri.getStringWidth(c.nombre_curso) / 1000 * 10;
        //System.out.println("nameWidth: " + nameWidthStroked);
        float strokePosition = (pageWidth - nameWidthStroked) / 2;
        contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, strokePosition, 554));
        contentStream.showText(c.nombre_curso);

        contentStream.setFont(calibri, 8);
        contentStream.setNonStrokingColor(Color.GRAY);

        if (instructor.equalsIgnoreCase("Manuel Anguiano Razn")) {
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 170, 457));
            contentStream.showText("Registro STPS: GIS100219KK8003");
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 170, 447));
            contentStream.showText("Registro PC: " + c.registro_manuel);
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 170, 437));
            contentStream.showText("Registro PC: " + c.registro_jorge);

        } else if (instructor.equalsIgnoreCase("Ing. Jorge Antonio Razn Gutierrez")) {
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 170, 457));
            contentStream.showText("Registro STPS: GIS100219KK8003");
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 170, 447));
            contentStream.showText("Registro PC: " + c.registro_coco);
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 170, 437));
            contentStream.showText("Registro PC: " + c.registro_jorge);
        } else {
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 150, 457));
            contentStream.showText("Registro STPS: GIS100219KK8003");
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 150, 447));
            contentStream.showText("Registro STPS: RAGJ610813BIA005");
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 150, 437));
            contentStream.showText("Registro PC: " + c.registro_jorge);
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 270, 447));
            contentStream.showText("Registro PC: SPC-COAH-056-2015");
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 270, 437));
            contentStream.showText("Registro PC: CGPC-28/6016/026/NL-PS14");
        }

        contentStream.endText();

        contentStream.setStrokingColor(Color.BLACK);
        contentStream.setLineWidth(1);
        contentStream.moveTo(strokePosition, 552);
        contentStream.lineTo(strokePosition + nameWidthStroked + 6, 552);
        contentStream.stroke();

        if (logoObject != null && !logoObject.isEmpty()) {
            contentStream.drawImage(logoObject, 451, 700, 130, 65);
        }
        if (firmaObject != null && !firmaObject.isEmpty()) {
            contentStream.setStrokingColor(Color.BLACK);
            contentStream.drawImage(firmaObject, 452, 440, 110, 42);
            contentStream.beginText();
            contentStream.setFont(calibri, 10);
            nameWidth = calibri.getStringWidth(instructor) / 1000 * 10;
            xPosition = 505 - nameWidth / 2;

            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 445));
            contentStream.showText(instructor);
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 485, 435));
            contentStream.showText("Instructor");
            contentStream.endText();
        }

        // Print DOWN Side

        contentStream.beginText();
        contentStream.setFont(pristina, 28);
        //contentStream.setNonStrokingColor(0,112,192);
        contentStream.setNonStrokingColor(0, 128, 0);

        nameWidth = pristina.getStringWidth(p2.nombre + " " + p2.apellidos) / 1000 * 28;

        //System.out.println(p2.nombre + " " + p2.apellidos + "::" + nameWidth);

        if (nameWidth < 470) {
            xPosition = (pageWidth - nameWidth) / 2;
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 262));
            contentStream.showText(p2.nombre + " " + p2.apellidos);
        } else {
            contentStream.setFont(pristina, 22);
            nameWidth = pristina.getStringWidth(p2.nombre + " " + p2.apellidos) / 1000 * 22;
            xPosition = (pageWidth - nameWidth) / 2;
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 262));
            contentStream.showText(p2.nombre + " " + p2.apellidos);
        }

        contentStream.setFont(calibri, 15);
        contentStream.setNonStrokingColor(Color.BLACK);

        nameWidth = calibri.getStringWidth(c.razon_social) / 1000 * 15;
        //System.out.println("nameWidth: " + nameWidth);
        xPosition = (pageWidth - nameWidth) / 2;

        contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 235));
        contentStream.showText(c.razon_social);

        contentStream.setFont(calibri, 11);
        contentStream.setNonStrokingColor(Color.BLACK);

        if (c.walmart) {
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 275, (float) 213.4));
            contentStream.showText(p2.determinante + " " + d.unidad);
        } else if (d.sucursal.isEmpty() || d.sucursal.equalsIgnoreCase("")) {
            contentStream.endText();
            contentStream.addRect(100, 211, 400, 20);
            contentStream.setNonStrokingColor(Color.WHITE);
            contentStream.fill();
            contentStream.beginText();
            contentStream.setNonStrokingColor(Color.BLACK);
        } else {
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 275, (float) 213.4));
            contentStream.showText(d.sucursal);
        }

        contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 210, 159));
        contentStream.showText(c.horas_texto);

        contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 394, 159));
        contentStream.showText(c.fecha_texto_diploma);

        contentStream.setFont(calibriBold, 10);

        nameWidthStroked = calibri.getStringWidth(c.nombre_curso) / 1000 * 10;
        //System.out.println("nameWidth: " + nameWidthStroked);
        strokePosition = (pageWidth - nameWidthStroked) / 2;
        contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, strokePosition, 174));
        contentStream.showText(c.nombre_curso);

        contentStream.setFont(calibri, 8);
        contentStream.setNonStrokingColor(Color.GRAY);

        if (instructor.equalsIgnoreCase("Manuel Anguiano Razn")) {
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 170, 74));
            contentStream.showText("Registro STPS: GIS100219KK8003");
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 170, 64));
            contentStream.showText("Registro PC: " + c.registro_manuel);
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 170, 54));
            contentStream.showText("Registro PC: " + c.registro_jorge);

        } else if (instructor.equalsIgnoreCase("Ing. Jorge Antonio Razn Gutierrez")) {
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 170, 74));
            contentStream.showText("Registro STPS: GIS100219KK8003");
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 170, 64));
            contentStream.showText("Registro PC: " + c.registro_coco);
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 170, 54));
            contentStream.showText("Registro PC: " + c.registro_jorge);
        } else {
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 150, 74));
            contentStream.showText("Registro STPS: GIS100219KK8003");
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 150, 64));
            contentStream.showText("Registro STPS: RAGJ610813BIA005");
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 150, 54));
            contentStream.showText("Registro PC: " + c.registro_jorge);
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 270, 64));
            contentStream.showText("Registro PC: SPC-COAH-056-2015");
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 270, 54));
            contentStream.showText("Registro PC: CGPC-28/6016/026/NL-PS14");
        }

        contentStream.endText();

        contentStream.setStrokingColor(Color.BLACK);
        contentStream.moveTo(strokePosition, 172);
        contentStream.lineTo(strokePosition + nameWidthStroked + 6, 172);
        contentStream.stroke();

        if (logoObject != null && !logoObject.isEmpty()) {
            contentStream.drawImage(logoObject, 451, 320, 130, 65);
        }
        if (firmaObject != null && !firmaObject.isEmpty()) {
            contentStream.drawImage(firmaObject, 452, 62, 110, 42);

            contentStream.beginText();
            contentStream.setFont(calibri, 10);
            contentStream.setStrokingColor(Color.BLACK);
            nameWidth = calibri.getStringWidth(instructor) / 1000 * 10;

            xPosition = 505 - nameWidth / 2;

            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, xPosition, 67));

            contentStream.showText(instructor);
            contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 485, 57));
            contentStream.showText("Instructor");
            contentStream.endText();
        }
        // Make sure that the content stream is closed:
        contentStream.close();

        // Save the results and ensure that the document is properly closed:
        //document.save( "DiplomaSoloTest.pdf");
        //document.close();
    }

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;// w  ww.  j a va2 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   ww w .ja  v a  2 s . 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.eggbacon.app.pdf.PDFCreator.java

License:Open Source License

public PDPageContentStream addPage(int pageNumber) throws IOException {
    PDPage page = new PDPage(PDFConstants.PAPER_SIZE);
    doc.addPage(page);/*  ww w.  j  ava 2  s  .co m*/
    PDPageContentStream contentStream = new PDPageContentStream(doc, page);
    //drawHeader(contentStream);
    drawFooter(pageNumber, contentStream);

    return contentStream;
}

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);
                }/* w w w  .  ja v a2 s .  c om*/
            }
        }

        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);
        }
    }
}

From source file:com.baseprogramming.pdwriter.PdWriter.java

License:Apache License

protected void createNewPage() {
    currentPage = new PDPage(meta.getPageFormat());
    document.addPage(currentPage);
    yPosition = meta.getUpperRightY();
}

From source file:com.cdd.bao.template.RenderSchema.java

License:Open Source License

public void createPageTemplate() throws IOException {
    RenderContext ctx = new RenderContext();

    renderPageTemplate(ctx);//from   ww w .  j  a  v a2  s  .c o m
    if (ctx.width == 0 || ctx.height == 0)
        return;

    PDPage page = new PDPage(new PDRectangle(ctx.width, ctx.height));
    document.addPage(page);

    ctx.stream = new PDPageContentStream(document, page);
    renderPageTemplate(ctx);
    ctx.stream.close();
}

From source file:com.cdd.bao.template.RenderSchema.java

License:Open Source License

public void createPageAssay(Schema.Assay assay) throws IOException {
    RenderContext ctx = new RenderContext();

    renderPageTemplate(ctx);/*from   w  w w.  j  av  a2s.com*/
    if (ctx.width == 0 || ctx.height == 0)
        return;

    PDPage page = new PDPage(new PDRectangle(ctx.width, ctx.height));
    document.addPage(page);

    ctx.stream = new PDPageContentStream(document, page);
    renderPageAssay(ctx, assay);
    ctx.stream.close();
}

From source file:com.cdd.bao.template.RenderSchema.java

License:Open Source License

public void createPageProperties() throws IOException {
    RenderContext ctx = new RenderContext();

    renderPageHierarchy(ctx, vocab.getPropertyHierarchy());
    if (ctx.width == 0 || ctx.height == 0)
        return;// w w w  . j  a v  a 2  s. c  om

    PDPage page = new PDPage(new PDRectangle(ctx.width, ctx.height));
    document.addPage(page);

    ctx.stream = new PDPageContentStream(document, page);
    renderPageHierarchy(ctx, vocab.getPropertyHierarchy());
    ctx.stream.close();
}

From source file:com.cdd.bao.template.RenderSchema.java

License:Open Source License

public void createPageValues() throws IOException {
    RenderContext ctx = new RenderContext();

    renderPageHierarchy(ctx, vocab.getValueHierarchy());
    if (ctx.width == 0 || ctx.height == 0)
        return;//from www . j  a v a  2s .  c om

    PDPage page = new PDPage(new PDRectangle(ctx.width, ctx.height));
    document.addPage(page);

    ctx.stream = new PDPageContentStream(document, page);
    renderPageHierarchy(ctx, vocab.getValueHierarchy());
    ctx.stream.close();
}