Search in sources :

Example 6 with PDType0Font

use of org.apache.pdfbox.pdmodel.font.PDType0Font in project pdfbox by apache.

the class GlyphCache method getPathForCharacterCode.

public GeneralPath getPathForCharacterCode(int code) {
    GeneralPath path = cache.get(code);
    if (path != null) {
        return path;
    }
    try {
        if (!font.hasGlyph(code)) {
            String fontName = ((PDFontLike) font).getName();
            if (font instanceof PDType0Font) {
                int cid = ((PDType0Font) font).codeToCID(code);
                String cidHex = String.format("%04x", cid);
                LOG.warn("No glyph for code " + code + " (CID " + cidHex + ") in font " + fontName);
            } else if (font instanceof PDSimpleFont) {
                LOG.warn("No glyph for code " + code + " in " + font.getClass().getSimpleName() + " " + fontName + " (embedded or system font used: " + ((PDSimpleFont) font).getFontBoxFont().getName() + ")");
                PDSimpleFont simpleFont = (PDSimpleFont) font;
                if (code == 10 && simpleFont.isStandard14()) {
                    // PDFBOX-4001 return empty path for line feed on std14
                    path = new GeneralPath();
                    cache.put(code, path);
                    return path;
                }
            } else {
                LOG.warn("No glyph for code " + code + " in font " + fontName);
            }
        }
        path = font.getNormalizedPath(code);
        cache.put(code, path);
        return path;
    } catch (IOException e) {
        // todo: escalate this error?
        LOG.error("Glyph rendering failed", e);
        return new GeneralPath();
    }
}
Also used : GeneralPath(java.awt.geom.GeneralPath) PDType0Font(org.apache.pdfbox.pdmodel.font.PDType0Font) PDFontLike(org.apache.pdfbox.pdmodel.font.PDFontLike) IOException(java.io.IOException) PDSimpleFont(org.apache.pdfbox.pdmodel.font.PDSimpleFont)

Example 7 with PDType0Font

use of org.apache.pdfbox.pdmodel.font.PDType0Font in project pdfbox by apache.

the class DrawPrintTextLocations method calculateGlyphBounds.

// this calculates the real (except for type 3 fonts) individual glyph bounds
private Shape calculateGlyphBounds(Matrix textRenderingMatrix, PDFont font, int code) throws IOException {
    GeneralPath path = null;
    AffineTransform at = textRenderingMatrix.createAffineTransform();
    at.concatenate(font.getFontMatrix().createAffineTransform());
    if (font instanceof PDType3Font) {
        // It is difficult to calculate the real individual glyph bounds for type 3 fonts
        // because these are not vector fonts, the content stream could contain almost anything
        // that is found in page content streams.
        PDType3Font t3Font = (PDType3Font) font;
        PDType3CharProc charProc = t3Font.getCharProc(code);
        if (charProc != null) {
            BoundingBox fontBBox = t3Font.getBoundingBox();
            PDRectangle glyphBBox = charProc.getGlyphBBox();
            if (glyphBBox != null) {
                // PDFBOX-3850: glyph bbox could be larger than the font bbox
                glyphBBox.setLowerLeftX(Math.max(fontBBox.getLowerLeftX(), glyphBBox.getLowerLeftX()));
                glyphBBox.setLowerLeftY(Math.max(fontBBox.getLowerLeftY(), glyphBBox.getLowerLeftY()));
                glyphBBox.setUpperRightX(Math.min(fontBBox.getUpperRightX(), glyphBBox.getUpperRightX()));
                glyphBBox.setUpperRightY(Math.min(fontBBox.getUpperRightY(), glyphBBox.getUpperRightY()));
                path = glyphBBox.toGeneralPath();
            }
        }
    } else if (font instanceof PDVectorFont) {
        PDVectorFont vectorFont = (PDVectorFont) font;
        path = vectorFont.getPath(code);
        if (font instanceof PDTrueTypeFont) {
            PDTrueTypeFont ttFont = (PDTrueTypeFont) font;
            int unitsPerEm = ttFont.getTrueTypeFont().getHeader().getUnitsPerEm();
            at.scale(1000d / unitsPerEm, 1000d / unitsPerEm);
        }
        if (font instanceof PDType0Font) {
            PDType0Font t0font = (PDType0Font) font;
            if (t0font.getDescendantFont() instanceof PDCIDFontType2) {
                int unitsPerEm = ((PDCIDFontType2) t0font.getDescendantFont()).getTrueTypeFont().getHeader().getUnitsPerEm();
                at.scale(1000d / unitsPerEm, 1000d / unitsPerEm);
            }
        }
    } else if (font instanceof PDSimpleFont) {
        PDSimpleFont simpleFont = (PDSimpleFont) font;
        // these two lines do not always work, e.g. for the TT fonts in file 032431.pdf
        // which is why PDVectorFont is tried first.
        String name = simpleFont.getEncoding().getName(code);
        path = simpleFont.getPath(name);
    } else {
        // shouldn't happen, please open issue in JIRA
        System.out.println("Unknown font class: " + font.getClass());
    }
    if (path == null) {
        return null;
    }
    return at.createTransformedShape(path.getBounds2D());
}
Also used : PDType3CharProc(org.apache.pdfbox.pdmodel.font.PDType3CharProc) GeneralPath(java.awt.geom.GeneralPath) PDType3Font(org.apache.pdfbox.pdmodel.font.PDType3Font) PDType0Font(org.apache.pdfbox.pdmodel.font.PDType0Font) BoundingBox(org.apache.fontbox.util.BoundingBox) PDTrueTypeFont(org.apache.pdfbox.pdmodel.font.PDTrueTypeFont) AffineTransform(java.awt.geom.AffineTransform) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) PDCIDFontType2(org.apache.pdfbox.pdmodel.font.PDCIDFontType2) PDVectorFont(org.apache.pdfbox.pdmodel.font.PDVectorFont) PDSimpleFont(org.apache.pdfbox.pdmodel.font.PDSimpleFont)

Example 8 with PDType0Font

use of org.apache.pdfbox.pdmodel.font.PDType0Font in project pancm_project by xuwujing.

the class PdfBoxTest method main.

public static void main(String[] args) throws IOException {
    // String formTemplate = "E:/module.pdf";
    String formTemplate = "module.pdf";
    // 定义文档对象
    PDDocument document = new PDDocument();
    // 定义一页,大小A4
    PDPage page = new PDPage(PDRectangle.A4);
    document.addPage(page);
    // 获取字体
    PDType0Font font = PDType0Font.load(document, new File("C:\\Windows\\Fonts\\Arial.ttf"));
    // 定义页面内容流
    PDPageContentStream stream = new PDPageContentStream(document, page);
    // 设置字体及文字大小
    stream.setFont(font, 12);
    // 设置画笔颜色
    stream.setNonStrokingColor(Color.BLACK);
    // 添加矩形
    stream.addRect(29, 797, 100, 14);
    // 填充矩形
    stream.fill();
    stream.setNonStrokingColor(Color.BLACK);
    // 文本填充开始
    stream.beginText();
    // 设置行距
    stream.setLeading(18f);
    // 设置文字位置
    stream.newLineAtOffset(30, 800);
    // 填充文字
    stream.showText("11");
    // 换行
    stream.newLine();
    stream.showText("22");
    stream.newLine();
    stream.showText("33");
    // 文本填充结束
    stream.endText();
    // 关闭流
    stream.close();
    // 保存
    document.save(formTemplate);
    // 释放资源
    document.close();
}
Also used : PDPage(org.apache.pdfbox.pdmodel.PDPage) PDType0Font(org.apache.pdfbox.pdmodel.font.PDType0Font) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDPageContentStream(org.apache.pdfbox.pdmodel.PDPageContentStream) File(java.io.File)

Aggregations

PDType0Font (org.apache.pdfbox.pdmodel.font.PDType0Font)8 PDCIDFontType2 (org.apache.pdfbox.pdmodel.font.PDCIDFontType2)4 File (java.io.File)3 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)3 PDPage (org.apache.pdfbox.pdmodel.PDPage)3 PDPageContentStream (org.apache.pdfbox.pdmodel.PDPageContentStream)3 PDSimpleFont (org.apache.pdfbox.pdmodel.font.PDSimpleFont)3 PDTrueTypeFont (org.apache.pdfbox.pdmodel.font.PDTrueTypeFont)3 GeneralPath (java.awt.geom.GeneralPath)2 IOException (java.io.IOException)2 TrueTypeFont (org.apache.fontbox.ttf.TrueTypeFont)2 BoundingBox (org.apache.fontbox.util.BoundingBox)2 PDCIDFont (org.apache.pdfbox.pdmodel.font.PDCIDFont)2 PDFont (org.apache.pdfbox.pdmodel.font.PDFont)2 PDFontDescriptor (org.apache.pdfbox.pdmodel.font.PDFontDescriptor)2 PDType3Font (org.apache.pdfbox.pdmodel.font.PDType3Font)2 AffineTransform (java.awt.geom.AffineTransform)1 TTFParser (org.apache.fontbox.ttf.TTFParser)1 SetMatrix (org.apache.pdfbox.contentstream.operator.state.SetMatrix)1 COSName (org.apache.pdfbox.cos.COSName)1