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:au.org.alfred.icu.pdf.services.factories.ICUDischargeSummaryFactory.java

public static PDPage newPageBuilder(PDDocument pdf, PDPage pg_content, PDPageContentStream cs,
        DischargeSummaryData data, int yCursor) throws IOException {
    pdf.addPage(pg_content);//from   ww w  .j a  v  a 2 s.  com
    cs.close();
    PDPage pg = new PDPage(new PDRectangle(pageWidth, pageHeight));
    return pg;
}

From source file:boxtable.table.Table.java

License:Apache License

/**
 * Starts a new page with the same size as the last one
 * /*from   w  ww . ja  v a  2  s  .com*/
 * @param document
 *            The document the table is rendered to
 * @param stream
 *            The PDPageContentStream used to render the table up to now (will be closed after calling this method)
 * @return A new PDPageContentStream for rendering to the new page
 * @throws IOException
 *             If writing to the streams fails
 */
private PDPageContentStream newPage(final PDDocument document, final PDPageContentStream stream)
        throws IOException {
    final PDRectangle pageSize = document.getPage(document.getNumberOfPages() - 1).getMediaBox();
    handleEvent(EventType.END_PAGE, document, stream, 0, pageSize.getHeight(), pageSize.getWidth(),
            pageSize.getHeight());
    stream.close();
    final PDPage page = new PDPage(pageSize);
    document.addPage(page);
    PDPageContentStream newStream = new PDPageContentStream(document, page, AppendMode.APPEND, true);
    handleEvent(EventType.BEGIN_PAGE, document, newStream, 0, pageSize.getHeight(), pageSize.getWidth(),
            pageSize.getHeight());
    return newStream;
}

From source file:br.com.techne.gluonsoft.pdfgenerator.PDFGenerator.java

License:Apache License

/**
 * Add a new page to the PDF document/*  w  ww.j  a va 2s  . c o m*/
 * @param title
 * @param strLineContent Text to be added to the page
 * @throws IOException 
 */
public void addNewPage(String pageTitle, String[] strLineContent) throws IOException {
    PDPage page = new PDPage(PDRectangle.A4);
    this.addNewPage(page);

    PDRectangle rect = new PDRectangle();
    rect = page.getMediaBox();

    final float PAGE_MAX_WIDTH = rect.getWidth() - (2 * PAGE_MARGIN);
    final float PAGE_MAX_HEIGHT = rect.getHeight();

    final float PAGE_X_ALIGN_CENTER = PAGE_MAX_WIDTH / 2; // (PAGE_MAX_WIDTH + (2 * PAGE_MARGIN)) / 2 ;

    PDPageContentStream pageContent = new PDPageContentStream(this.getPdfDocument(), page); // Page's Stream

    int line = 1;

    // Add the page's title
    if (pageTitle != null) {
        pageContent.beginText();
        pageContent.setFont(FONT_BOLD, FONT_SIZE_DEFAULT);
        pageContent.newLineAtOffset(PAGE_X_ALIGN_CENTER, PAGE_INITIAL_Y_POSITION); // CENTER
        pageContent.showText(pageTitle); // Title
        pageContent.endText();

        pageContent.beginText();
        pageContent.setFont(FONT_BOLD, FONT_SIZE_DEFAULT);
        pageContent.newLineAtOffset(PAGE_MARGIN, PAGE_INITIAL_Y_POSITION - 10 * (line++)); // pageContent.newLineAtOffset(PAGE_MARGIN, PAGE_INITIAL_Y_POSITION);
        pageContent.showText(""); // Line after title
        pageContent.endText();
    }

    // Add the page's content
    if (strLineContent != null && strLineContent.length > 0) {
        for (String strLine : strLineContent) {
            ArrayList<String> newLines = autoBreakLineIntoOthers(strLine, _MAX_LINE_CHARACTERS); // Break a text line into others lines to fit into page width.

            for (String str : newLines) {
                pageContent.beginText();
                pageContent.setFont(FONT_PLAIN, FONT_SIZE_DEFAULT);
                pageContent.newLineAtOffset(PAGE_MARGIN, PAGE_INITIAL_Y_POSITION - 10 * (line++));
                pageContent.showText(str);
                pageContent.endText();
            }
        }
    }

    pageContent.close();
}

From source file:br.com.techne.gluonsoft.pdfgenerator.PDFGenerator.java

License:Apache License

/**
 * Create a page with table.//w w  w  .ja va 2s.com
 * @param tableTitle Table Title
 * @param tableColumnsName   Array of Strings with table columns titles.
 * @param strTableContent Array of Strings with the table data.
 * @throws IOException
 */
@SuppressWarnings("deprecation")
public void addNewTable(String tableTitle, ArrayList<String> tableColumnsName,
        ArrayList<String> strTableContent) throws IOException {
    PDRectangle rectLandscapeOrientation = new PDRectangle(PDRectangle.A4.getHeight(),
            PDRectangle.A4.getWidth()); // Changed width to height to get Landscape Orientation

    PDPage tablePage = new PDPage(PDRectangle.A4);
    PDRectangle rect = tablePage.getMediaBox();

    this.getPdfDocument().addPage(tablePage);

    PDPageContentStream pageContent = new PDPageContentStream(this.getPdfDocument(), tablePage);

    final float margin = 20f;
    final int tableRows = strTableContent != null ? strTableContent.size() : 0;
    final int tableColumns = tableColumnsName != null ? tableColumnsName.size() : 0;
    final float rowHeigth = 20f;
    final float tableWidth = rect.getWidth() - (2 * margin);
    final float tableHeight = rowHeigth * tableRows;
    final float tableColWidth = tableWidth / (float) tableColumns;
    final float tableCelMargin = 5f;

    // Draw the lines
    float nextY = PAGE_INITIAL_Y_POSITION;
    for (int r = 0; r <= tableRows; r++) {
        pageContent.drawLine(margin, nextY, margin + tableWidth, nextY);
        nextY -= rowHeigth;
    }

    // Draw the columns
    float nextX = margin;
    for (int i = 0; i <= tableColumns; i++) {
        pageContent.drawLine(nextX, PAGE_INITIAL_Y_POSITION, nextX, PAGE_INITIAL_Y_POSITION - tableHeight);
        nextX += tableColWidth;
    }

    pageContent.setFont(FONT_BOLD, FONT_SIZE_DEFAULT); // Initial Font for the columns' titles

    float textPosX = margin + tableCelMargin;
    float textPosY = PAGE_INITIAL_Y_POSITION - 15;

    // Title
    float centerX = tableWidth / 2 - (margin * 2);
    float xAlignLeft = margin;
    pageContent.beginText();
    pageContent.newLineAtOffset(xAlignLeft, PAGE_INITIAL_Y_POSITION + 5);
    pageContent.showText(tableTitle);
    pageContent.endText();

    // Columns' names
    for (int i = 0; i < tableColumnsName.size(); i++) {
        String columnName = tableColumnsName.get(i);
        System.out.println(columnName);

        pageContent.beginText();
        pageContent.newLineAtOffset(textPosX, textPosY);
        pageContent.showText(columnName);
        pageContent.endText();
        textPosX += tableColWidth;
    }

    //       textPosY -= rowHeigth;
    //       textPosX = margin + tableCelMargin;

    // Cels' content (Add the text)
    int actualCol = 0;
    pageContent.setFont(FONT_PLAIN, FONT_SIZE_DEFAULT);
    for (int i = 0; i < strTableContent.size(); i++) {
        if (actualCol % tableColumns == 0) {
            actualCol = 0;
            textPosY -= rowHeigth;
            textPosX = margin + tableCelMargin;
        }

        String celText = strTableContent.get(i);
        System.out.println(celText);
        pageContent.beginText();
        pageContent.newLineAtOffset(textPosX, textPosY);
        pageContent.showText(celText);
        pageContent.endText();
        textPosX += tableColWidth;

        actualCol++;
    }

    pageContent.close();
}

From source file:br.com.techne.gluonsoft.pdfgenerator.PDFGenerator.java

License:Apache License

/**
 * Create a page with table./*from w  w  w  .  j ava  2 s .  co m*/
 * @param tableTitle Table Title
 * @param strTableContent   Array of Strings, where the column's titles goes into the first array line and the data goes into others lines.
 * @throws IOException
 */
@SuppressWarnings("deprecation")
public void addNewTable(String tableTitle, String[][] strTableContent) throws IOException {
    PDPage tablePage = new PDPage(PDRectangle.A4);

    PDRectangle rect = new PDRectangle();
    rect = tablePage.getMediaBox();

    this.getPdfDocument().addPage(tablePage);

    PDPageContentStream pageContent = new PDPageContentStream(this.getPdfDocument(), tablePage);

    final float margin = 20f;
    final int tableRows = strTableContent.length;
    final int tableColumns = strTableContent[0].length;
    final float rowHeigth = 20f;
    final float tableWidth = rect.getWidth() - (2 * margin);
    final float tableHeight = rowHeigth * tableRows;
    final float tableColWidth = tableWidth / (float) tableColumns;
    final float tableCelMargin = 5f;

    // Draw the lines
    float nextY = PAGE_INITIAL_Y_POSITION;
    for (int r = 0; r <= tableRows; r++) {
        pageContent.drawLine(margin, nextY, margin + tableWidth, nextY);
        nextY -= rowHeigth;
    }

    // Draw the columns
    float nextX = margin;
    for (int i = 0; i <= tableColumns; i++) {
        pageContent.drawLine(nextX, PAGE_INITIAL_Y_POSITION, nextX, PAGE_INITIAL_Y_POSITION - tableHeight);
        nextX += tableColWidth;
    }

    pageContent.setFont(FONT_BOLD, FONT_SIZE_DEFAULT); // Fonte inicial para o ttulo das colunas

    float textPosX = margin + tableCelMargin;
    float textPosY = PAGE_INITIAL_Y_POSITION - 15;

    // Title
    float centerX = tableWidth / 2 - (margin * 2);
    pageContent.beginText();
    pageContent.newLineAtOffset(centerX, PAGE_INITIAL_Y_POSITION + 5);
    pageContent.showText(tableTitle);
    pageContent.endText();

    // Cels' content (Add the text)
    for (int l = 0; l < strTableContent.length; l++) {
        for (int c = 0; c < strTableContent[l].length; c++) {
            String celText = strTableContent[l][c];

            if (l > 0) {
                pageContent.setFont(FONT_PLAIN, FONT_SIZE_DEFAULT);
            }

            pageContent.beginText();
            pageContent.newLineAtOffset(textPosX, textPosY);
            pageContent.showText(celText);
            pageContent.endText();
            textPosX += tableColWidth;
        }

        textPosY -= rowHeigth;
        textPosX = margin + tableCelMargin;
    }

    pageContent.close();
}

From source file:Bulletin.myPdf.java

public void newPage() {
    System.out.println("new page");
    this.page = new PDPage(PDRectangle.A4);
    this.rect = page.getMediaBox();
    this.left = leftMargin;
    this.right = rect.getWidth() - leftMargin;
    this.width = right - left;
    this.height = rect.getHeight();
    this.Y = rect.getHeight() - topMargin;
    this.top = this.Y;

    try {//from   w  w w  . j  a v  a2 s.  c  o  m
        this.cos = new PDPageContentStream(this.document, this.page);
    } catch (IOException ex) {
        Logger.getLogger(myPdf.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Bulletin.test.java

public static void test2() {
    try {//from   www . ja v  a2s.  c om
        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());/* w  w w.jav  a  2s  .  c om*/

    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: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.  j av  a  2  s.  c o  m
        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:cdiscisa.StreamUtil.java

private static void imprimirDiplomaArriba(Participante p1, 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);//www  . ja va 2s.co  m

        // 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 Name
        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);

        contentStream.setTextMatrix(new Matrix(1, 0, 0, 1, 255, 593));
        if (c.walmart) {
            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.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");
        }
        // "Registro PC: DPC-ENL-CE-002/2015"
        // DPC-ENL-I-103_2015 "Ing. Jorge Antonio Razn Gutierrez"
        // DPC-ENL-I-056_2015 "Manuel Anguiano Razn"
        // "TSI. Jorge Antonio Razn Gil"

        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.drawImage(firmaObject, 452, 440, 110, 42);

            contentStream.beginText();
            contentStream.setFont(calibri, 10);
            contentStream.setNonStrokingColor(Color.BLACK);
            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();
        }
        /*
        System.out.println("logoWidth: " + logoObject.getWidth());
        System.out.println("logoHeight: " + logoObject.getHeight());
        System.out.println("firmaWidth: " + firmaObject.getWidth());
        System.out.println("firmaHeight: " + firmaObject.getHeight());
        */

        /*
        contentStream.addRect(50, 750, 500, 100);
        contentStream.setNonStrokingColor(Color.WHITE);
        contentStream.fill();
        contentStream.drawImage(logo, 430,700,150,75);
        contentStream.drawImage(firma, 93,239,72,29);
        */

        // Make sure that the content stream is closed:
        contentStream.close();

        // Save the results and ensure that the document is properly closed:

    }