Example usage for org.apache.pdfbox.pdmodel.font PDFont getFontDescriptor

List of usage examples for org.apache.pdfbox.pdmodel.font PDFont getFontDescriptor

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.font PDFont getFontDescriptor.

Prototype

@Override
    public PDFontDescriptor getFontDescriptor() 

Source Link

Usage

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFBoxTable.java

License:EUPL

private float[] getStringHeights(String[] lines, PDFont c, float fontSize) {
    float[] heights = new float[lines.length];
    for (int i = 0; i < lines.length; i++) {
        float maxLineHeight = 0;
        try {//from w ww  . j av a 2  s .  c o m
            byte[] linebytes = StringUtils.applyWinAnsiEncoding(lines[i]);
            for (int j = 0; j < linebytes.length; j++) {
                float he = c.getFontHeight(linebytes, j, 1) / 1000 * fontSize;
                if (he > maxLineHeight) {
                    maxLineHeight = he;
                }
            }
        } catch (UnsupportedEncodingException e) {
            logger.warn("failed to determine String height", e);
            maxLineHeight = c.getFontDescriptor().getCapHeight() / 1000 * fontSize;
        } catch (IOException e) {
            logger.warn("failed to determine String height", e);
            maxLineHeight = c.getFontDescriptor().getCapHeight() / 1000 * fontSize;
        }

        heights[i] = maxLineHeight;
    }

    return heights;
}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.TableDrawUtils.java

License:EUPL

private static void drawString(PDPage page, PDPageContentStream contentStream, float contentx, float contenty,
        float width, float height, float padding, PDFBoxTable abstractTable, PDDocument doc, Entry cell,
        float fontSize, float textHeight, String valign, String halign, String[] tlines, PDFont textFont,
        PDResources formResources, ISettings settings) throws PdfAsException {
    try {/*from   w w  w.  jav a2  s . c  o  m*/
        float ty = contenty - padding;
        float tx = contentx + padding;
        float innerHeight = height - (2 * padding);
        float innerWidth = width - (2 * padding);
        if (Style.BOTTOM.equals(valign)) {
            float bottom_offset = innerHeight - textHeight;
            ty -= bottom_offset;
        } else if (Style.MIDDLE.equals(valign)) {
            float bottom_offset = innerHeight - textHeight;
            bottom_offset = bottom_offset / 2.0f;
            ty -= bottom_offset;
        }

        // calculate the max with of the text content
        float maxWidth = 0;
        for (int k = 0; k < tlines.length; k++) {
            float lineWidth;
            // if (textFont instanceof PDType1Font) {
            lineWidth = textFont.getStringWidth(tlines[k]) / 1000.0f * fontSize;
            /*
             * } else { float fwidth = textFont
             * .getStringWidth("abcdefghijklmnopqrstuvwxyz ") / 1000.0f *
             * fontSize; fwidth = fwidth / (float)
             * "abcdefghijklmnopqrstuvwxyz" .length(); lineWidth =
             * tlines[k].length() * fwidth; }
             */
            if (maxWidth < lineWidth) {
                maxWidth = lineWidth;
            }
        }

        if (Style.CENTER.equals(halign)) {
            float offset = innerWidth - maxWidth;
            if (offset > 0) {
                offset = offset / 2.0f;
                tx += offset;
            }
        } else if (Style.RIGHT.equals(halign)) {
            float offset = innerWidth - maxWidth;
            if (offset > 0) {
                tx += offset;
            }
        }
        float ascent = textFont.getFontDescriptor().getAscent();
        float descent = textFont.getFontDescriptor().getDescent();

        ascent = ascent / 1000.0f * fontSize;
        descent = descent / 1000.0f * fontSize;

        //ty = ty + (descent * (-1));

        logger.debug("Text tx {} ty {} maxWidth {} textHeight {}", tx, ty, maxWidth, textHeight);
        logger.debug("Text ASCENT {} DESCENT {}", ascent, descent);

        logger.debug("Text TRANSFORMED ASCENT {} DESCENT {}", ascent, descent);

        drawDebugLineString(contentStream, tx, ty, maxWidth, textHeight, descent, settings);

        contentStream.beginText();

        if (formResources.getFonts().containsValue(textFont)) {
            String fontID = getFontID(textFont, formResources);
            logger.debug("Using Font: " + fontID);
            contentStream.appendRawCommands("/" + fontID + " " + fontSize + " Tf\n");
        } else {
            contentStream.setFont(textFont, fontSize);
        }

        logger.debug("Writing: " + tx + " : " + (ty - fontSize + (descent * (-1))) + " as " + cell.getType());
        contentStream.moveTextPositionByAmount(tx, (ty - fontSize + (descent * (-1))));

        contentStream.appendRawCommands(fontSize + " TL\n");
        for (int k = 0; k < tlines.length; k++) {
            contentStream.drawString(tlines[k]);
            if (k < tlines.length - 1) {
                contentStream.appendRawCommands("T*\n");
            }
        }

        contentStream.endText();

    } catch (IOException e) {
        logger.warn("IO Exception", e);
        throw new PdfAsException("Error", e);
    }
}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.PDFBoxTable.java

License:EUPL

private float[] getStringHeights(String[] lines, PDFont c, float fontSize) {
    float[] heights = new float[lines.length];
    for (int i = 0; i < lines.length; i++) {
        float maxLineHeight = 0;
        try {//from   ww w . ja  v  a2  s . co  m
            byte[] linebytes = StringUtils.applyWinAnsiEncoding(lines[i]);
            for (int j = 0; j < linebytes.length; j++) {
                float he = c.getHeight(linebytes[j]) / 1000 * fontSize;
                if (he > maxLineHeight) {
                    maxLineHeight = he;
                }
            }
        } catch (UnsupportedEncodingException e) {
            logger.warn("failed to determine String height", e);
            maxLineHeight = c.getFontDescriptor().getCapHeight() / 1000 * fontSize;
        } catch (IOException e) {
            logger.warn("failed to determine String height", e);
            maxLineHeight = c.getFontDescriptor().getCapHeight() / 1000 * fontSize;
        }

        heights[i] = maxLineHeight;
    }

    return heights;
}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.TableDrawUtils.java

License:EUPL

private static void drawString(PDPage page, PDPageContentStream contentStream, float contentx, float contenty,
        float width, float height, float padding, PDFBoxTable abstractTable, PDDocument doc, Entry cell,
        float fontSize, float textHeight, String valign, String halign, String[] tlines, PDFont textFont,
        PDResources formResources, ISettings settings) throws PdfAsException {
    try {/*from  w  w  w.  j  a va2s .  c o m*/
        float ty = contenty - padding;
        float tx = contentx + padding;
        float innerHeight = height - (2 * padding);
        float innerWidth = width - (2 * padding);
        if (Style.BOTTOM.equals(valign)) {
            float bottom_offset = innerHeight - textHeight;
            ty -= bottom_offset;
        } else if (Style.MIDDLE.equals(valign)) {
            float bottom_offset = innerHeight - textHeight;
            bottom_offset = bottom_offset / 2.0f;
            ty -= bottom_offset;
        }

        float descent = 0;
        float ascent;
        float lineWidth = 0;
        float txNew = 0;

        if (tlines.length > 1 && Style.LINECENTER.equals(halign)) {

            //Calculate TXs
            ArrayList<Float> calculatedTXs = new ArrayList<>();
            for (int k = 0; k < tlines.length; k++) {

                // if (textFont instanceof PDType1Font) {
                lineWidth = textFont.getStringWidth(tlines[k]) / 1000.0f * fontSize;
                txNew = (innerWidth - lineWidth) / 2.0f;
                logger.debug("calculatedTXNew in k-Loop: {} {}", k, txNew);
                calculatedTXs.add(tx + txNew);

                logger.debug("INNERWIDTH in drawString: " + innerWidth);
                logger.debug("TX in drawString: " + tx);

                ascent = textFont.getFontDescriptor().getAscent();
                descent = textFont.getFontDescriptor().getDescent();
                ascent = ascent / 1000.0f * fontSize;
                descent = descent / 1000.0f * fontSize;

                //ty = ty + (descent * (-1));

                logger.debug("Text txNew {} ty {} lineWidth {} textHeight {}", txNew, ty, lineWidth,
                        textHeight);
                logger.debug("Text ASCENT {} DESCENT {}", ascent, descent);

                logger.debug("Text TRANSFORMED ASCENT {} DESCENT {}", ascent, descent);
                drawDebugLineString(contentStream, txNew, ty, lineWidth, textHeight, descent, settings);
            }

            contentStream.beginText();
            contentStream.setFont(textFont, fontSize);
            txNew = tx + calculatedTXs.get(0);
            logger.debug("Calculated TX0: " + txNew);
            //contentStream.newLineAtOffset(txNew, (ty - fontSize + (descent * (-1))));

            /*
            if (formResources.getFont(COSName.getPDFName(textFont.getName())) != null) {
               String fontID = getFontID(textFont, formResources);
               logger.debug("Using Font: " + fontID);
               contentStream.appendRawCommands("/" + fontID + " " + fontSize
                     + " Tf\n");
            } else {
               contentStream.setFont(textFont, fontSize);
            }
                    
            logger.debug("Writing: " + tx + " : " + (ty - fontSize + (descent * (-1))) + " as "
                  + cell.getType());
            contentStream.moveTextPositionByAmount(tx, (ty - fontSize + (descent * (-1))));
                    
            contentStream.appendRawCommands(fontSize + " TL\n");
            */

            for (int k = 0; k < tlines.length; k++) {
                float offset = calculatedTXs.get(k);
                if (k == 0) {
                    contentStream.newLineAtOffset(offset, (ty - fontSize + (descent * (-1))));

                } else {
                    logger.debug("Calculated TX: {} {} ", k, offset);
                    contentStream.newLineAtOffset(offset, -1 * fontSize);
                } //contentStream.appendRawCommands("T*\n");

                contentStream.showText(tlines[k]);

                contentStream.newLineAtOffset(-1 * offset, 0);
            }
            contentStream.endText();

        } else {
            // calculate the max with of the text content
            float maxWidth = 0;
            for (int k = 0; k < tlines.length; k++) {

                // if (textFont instanceof PDType1Font) {
                lineWidth = textFont.getStringWidth(tlines[k]) / 1000.0f * fontSize;
                /*
                 * } else { float fwidth = textFont
                 * .getStringWidth("abcdefghijklmnopqrstuvwxyz ") / 1000.0f *
                 * fontSize; fwidth = fwidth / (float)
                 * "abcdefghijklmnopqrstuvwxyz" .length(); lineWidth =
                 * tlines[k].length() * fwidth; }
                 */
                if (maxWidth < lineWidth) {
                    maxWidth = lineWidth;
                }
            }

            if (Style.CENTER.equals(halign) || Style.LINECENTER.equals(halign)) {
                float offset = innerWidth - maxWidth;
                if (offset > 0) {
                    offset = offset / 2.0f;
                    tx += offset;
                }
            } else if (Style.RIGHT.equals(halign)) {
                float offset = innerWidth - maxWidth;
                if (offset > 0) {
                    tx += offset;
                }
            }
            ascent = textFont.getFontDescriptor().getAscent();
            descent = textFont.getFontDescriptor().getDescent();

            ascent = ascent / 1000.0f * fontSize;
            descent = descent / 1000.0f * fontSize;

            //ty = ty + (descent * (-1));

            logger.debug("Text tx {} ty {} maxWidth {} textHeight {}", tx, ty, maxWidth, textHeight);
            logger.debug("Text ASCENT {} DESCENT {}", ascent, descent);

            logger.debug("Text TRANSFORMED ASCENT {} DESCENT {}", ascent, descent);

            drawDebugLineString(contentStream, tx, ty, maxWidth, textHeight, descent, settings);

            contentStream.beginText();

            contentStream.setFont(textFont, fontSize);
            contentStream.newLineAtOffset(tx, (ty - fontSize + (descent * (-1))));
            /*
            if (formResources.getFont(COSName.getPDFName(textFont.getName())) != null) {
               String fontID = getFontID(textFont, formResources);
               logger.debug("Using Font: " + fontID);
               contentStream.appendRawCommands("/" + fontID + " " + fontSize
                     + " Tf\n");
            } else {
               contentStream.setFont(textFont, fontSize);
            }
                    
            logger.debug("Writing: " + tx + " : " + (ty - fontSize + (descent * (-1))) + " as "
                  + cell.getType());
            contentStream.moveTextPositionByAmount(tx, (ty - fontSize + (descent * (-1))));
                    
            contentStream.appendRawCommands(fontSize + " TL\n");
            */

            if (textFont.willBeSubset()) {
                logger.debug("Font will be subset!");
            }

            for (int k = 0; k < tlines.length; k++) {
                contentStream.showText(tlines[k]);
                if (k < tlines.length - 1) {
                    contentStream.newLineAtOffset(0, -1 * fontSize);
                    //contentStream.appendRawCommands("T*\n");
                }
            }

            contentStream.endText();
        }

    } catch (IOException e) {
        logger.warn("IO Exception", e);
        throw new PdfAsException("Error", e);
    }
}

From source file:au.org.alfred.icu.pdf.services.factories.ICUDischargeSummaryFactory.java

public static int getTableHeight(PDPage page, PDPageContentStream contentStream, float y, float margin,
        String[][] content, PDFont bold, PDFont font, int fontSize) throws IOException {
    final int rows = content.length;
    final int cols = content[0].length;
    final float rowHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
    final float tableWidth = page.findMediaBox().getWidth() - (2 * margin);
    final float colWidth = tableWidth / (float) cols;
    final float cellMargin = 5f;
    int maxRowHeight = (int) rowHeight;
    float textx = margin + cellMargin;
    float texty = y - rowHeight;
    for (int i = 0; i < content.length; i++) {
        for (int j = 0; j < content[i].length; j++) {
            String text = content[i][j];
            Paragraph para = new Paragraph(textx, texty, text);
            para.withColor(Color.BLACK);
            para.withFont(font, fontSize);
            para.withWidth((int) colWidth);
            textx += colWidth;/*from   w  w w .jav a 2  s  .co  m*/
            if (para.getParaHeight() > maxRowHeight) {
                maxRowHeight = para.getParaHeight();
            }
        }
        texty -= maxRowHeight + 2;
        textx = margin + cellMargin;

    }
    final float tableHeight = maxRowHeight * rows;

    return Float.valueOf(tableHeight).intValue();
}

From source file:au.org.alfred.icu.pdf.services.factories.ICUDischargeSummaryFactory.java

public static int drawTable(PDPage page, PDPageContentStream contentStream, float y, float margin,
        String[][] content, PDFont bold, PDFont font, int fontSize) throws IOException {
    final int rows = content.length;
    final int cols = content[0].length;
    final float rowHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
    final float tableWidth = page.findMediaBox().getWidth() - (2 * margin);
    final float colWidth = tableWidth / (float) cols;
    final float cellMargin = 5f;
    int maxRowHeight = (int) rowHeight;
    float textx = margin + cellMargin;
    float texty = y - rowHeight;
    for (int i = 0; i < content.length; i++) {
        for (int j = 0; j < content[i].length; j++) {
            String text = content[i][j];
            Paragraph para = new Paragraph(textx, texty, text);
            para.withColor(Color.BLACK);
            para.withFont(font, fontSize);
            para.withWidth((int) colWidth);
            textx += colWidth;/*  w  ww  . j a v a 2 s.co m*/
            if (para.getParaHeight() > maxRowHeight) {
                maxRowHeight = para.getParaHeight();
            }
        }
        texty -= maxRowHeight + 2;
        textx = margin + cellMargin;

    }
    final float tableHeight = maxRowHeight * rows;

    //draw the rows
    float nexty = y;
    for (int i = 0; i <= rows; i++) {
        contentStream.drawLine(margin, nexty, margin + tableWidth, nexty);
        nexty -= maxRowHeight + 2;
    }

    //draw the columns
    float nextx = margin;
    for (int i = 0; i <= cols; i++) {
        contentStream.drawLine(nextx, pageHeight - y, nextx, pageHeight - y);
        nextx += colWidth;
    }

    //now add the text
    contentStream.setNonStrokingColor(Color.BLACK);
    textx = margin + cellMargin;
    texty = y - maxRowHeight;
    for (int i = 0; i < content.length; i++) {
        for (int j = 0; j < content[i].length; j++) {
            String text = content[i][j];
            float textLeftX = 0;
            float textTopY = 0;
            textLeftX = textx;//+ (font.getStringWidth(text)/1000)*fontSize;
            textTopY = texty
                    + ((maxRowHeight) - ((font.getFontBoundingBox().getHeight() / 1000) * fontSize) + 1);
            Paragraph para = new Paragraph(textLeftX, textTopY, text);
            para.withColor(Color.BLACK);
            para.withWidth((int) colWidth);
            if (i == 0) {
                para.withFont(bold, fontSize);
            } else {
                para.withFont(font, fontSize);
            }
            write(contentStream, para);
            textx += colWidth;

        }
        texty -= maxRowHeight + 2;
        textx = margin + cellMargin;

    }
    //System.out.println("yCursor: " + tableHeight + " int " + Float.valueOf(tableHeight).intValue());
    return Float.valueOf(tableHeight).intValue();

}

From source file:barcode.printer.view.PrintForm.java

License:Apache License

public void buttonPrintAction() {
    if (listModel.isEmpty()) {
        return;//w w w. j  ava2  s  . com
    }
    try (PDDocument document = new PDDocument()) {

        PDPage page = new PDPage();

        document.addPage(page);

        PDFont font = PDType1Font.COURIER_BOLD;

        float fontSize = 10f;
        float fontWidth = font.getStringWidth("W") / 1000 * fontSize;
        float fontHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
        float startXOrigin = fontWidth * 0.3f;
        float startYOrigin = page.findMediaBox().getUpperRightY();
        float startX = startXOrigin;
        float startY;

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

            int bcWidth = 80;
            int bcHeight = 20;

            int row = 1;

            for (int idx = 0; idx < listModel.getSize(); idx++) {
                BitMatrix bitMatrix;
                String strBarCode = listModel.get(idx);
                switch (comboCode.getSelectedIndex()) {
                case 0:
                    bitMatrix = new CodaBarWriter().encode(strBarCode, BarcodeFormat.CODABAR, bcWidth,
                            bcHeight);
                    break;
                case 1:
                    bitMatrix = new Code128Writer().encode(strBarCode, BarcodeFormat.CODE_128, bcWidth,
                            bcHeight);
                    break;
                case 2:
                    bitMatrix = new Code39Writer().encode(strBarCode, BarcodeFormat.CODE_39, bcWidth, bcHeight);
                    break;
                case 3:
                    bitMatrix = new EAN13Writer().encode(strBarCode, BarcodeFormat.EAN_13, bcWidth, bcHeight);
                    break;
                case 4:
                    bitMatrix = new EAN8Writer().encode(strBarCode, BarcodeFormat.EAN_8, bcWidth, bcHeight);
                    break;
                case 5:
                    bitMatrix = new ITFWriter().encode(strBarCode, BarcodeFormat.ITF, bcWidth, bcHeight);
                    break;
                default:
                    bitMatrix = new CodaBarWriter().encode(strBarCode, BarcodeFormat.CODABAR, bcWidth,
                            bcHeight);
                    break;
                }

                BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
                PDXObjectImage pdxoi = new PDJpeg(document, bufferedImage);

                startY = startYOrigin - (bitMatrix.getHeight() * row * 1.6f);
                row++;

                cs.beginText();
                cs.setFont(font, fontSize);
                cs.moveTextPositionByAmount(startX + fontWidth, startY + fontHeight);
                cs.drawString(strBarCode);
                cs.endText();
                cs.drawImage(pdxoi, startX, startY - fontHeight);
            }

        }

        document.silentPrint();

        listModel.clear();
        buttonDelete.setEnabled(false);

    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage());
        LOGGER.log(Level.SEVERE, null, ex);
    }

}

From source file:Bulletin.Bulletin2.java

private float getTextHeight(PDFont font, String text, int size) {
    return font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * Math.round(textScale * size);
}

From source file:Bulletin.myPdf.java

public float getFontHeight(PDFont font, int size) {
    return size * font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000;
}

From source file:Bulletin.test.java

static public float getFontHeight(PDFont font, int size) {
    return size * font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000;
}