使用PDFBox将Type3字体字符渲染为图像

时间:2017-02-03 20:44:23

标签: java pdf pdfbox

在我的项目中,我不得不解析PDF文件,其中包含由Type3字体呈现的一些字符。所以,我需要做的是将这些字符渲染到BufferedImage中以便进一步处理。

我不确定我是否以正确的方式查看,但我正在尝试为这些字符获取PDType3CharProc:

PDType3Font font = (PDType3Font)textPosition.getFont();
PDType3CharProc charProc = font.getCharProc(textPosition.getCharacterCodes()[0]);

并且此过程的输入流包含以下数据:

54 0 1 -1 50 43 d1
q
49 0 0 44 1.1 -1.1 cm
BI
/W 49
/H 44
/BPC 1
/IM true
ID
<some binary data here>
EI
Q

但遗憾的是,我不知道如何使用PDFBox(或任何其他Java库)将这些数据渲染成图像。

我正在寻找正确的方向,我该如何处理这些数据? 如果没有,是否有其他工具可以解决这个问题?

2 个答案:

答案 0 :(得分:1)

不幸的是,开箱即用的PDFBox并没有提供一个类来呈现任意XObject的内容(比如类型3字体char procs),至少就我所见。

但它确实提供了一个用于呈现完整PDF页面的类;因此,要呈现给定的类型3字体字形,可以简单地创建一个仅包含该字形的页面并呈现此临时页面!

假设,例如,类型3字体在PDDocument document的第一页上定义并且名称为F1,其所有char proc都可以像这样呈现:

PDPage page = document.getPage(0);
PDResources pageResources = page.getResources();
COSName f1Name = COSName.getPDFName("F1");
PDType3Font fontF1 = (PDType3Font) pageResources.getFont(f1Name);
Map<String, Integer> f1NameToCode = fontF1.getEncoding().getNameToCodeMap();

COSDictionary charProcsDictionary = fontF1.getCharProcs();
for (COSName key : charProcsDictionary.keySet())
{
    COSStream stream = (COSStream) charProcsDictionary.getDictionaryObject(key);
    PDType3CharProc charProc = new PDType3CharProc(fontF1, stream);
    PDRectangle bbox = charProc.getGlyphBBox();
    if (bbox == null)
        bbox = charProc.getBBox();
    Integer code = f1NameToCode.get(key.getName());

    if (code != null)
    {
        PDDocument charDocument = new PDDocument();
        PDPage charPage = new PDPage(bbox);
        charDocument.addPage(charPage);
        charPage.setResources(pageResources);
        PDPageContentStream charContentStream = new PDPageContentStream(charDocument, charPage);
        charContentStream.beginText();
        charContentStream.setFont(fontF1, bbox.getHeight());
        charContentStream.getOutput().write(String.format("<%2X> Tj\n", code).getBytes());
        charContentStream.endText();
        charContentStream.close();

        File result = new File(RESULT_FOLDER, String.format("4700198773-%s-%s.png", key.getName(), code));
        PDFRenderer renderer = new PDFRenderer(charDocument);
        BufferedImage image = renderer.renderImageWithDPI(0, 96);
        ImageIO.write(image, "PNG", result);
        charDocument.close();
    }
}

RenderType3Character.java测试方法testRender4700198773

考虑到OP代码中的textPosition变量,他很可能会从文本提取用例中尝试这一点。因此,他必须如上所述预先生成位图并简单地按名称查找它们或调整代码以匹配其用例中的可用信息(例如,他可能没有手头的原始页面,仅字体对象;在这种情况下,他不能复制原始页面的资源,而是可以创建一个新的资源对象并将字体对象添加到其中。)

不幸的是,OP没有提供PDF样本。因此,我使用来自4700198773.pdf的另一个堆栈溢出问题extract text with custom font result non readble进行测试。 OP显然可能仍有问题。

答案 1 :(得分:0)

我偶然发现了同样的问题,我通过修改PDFRenderer和基础PageDrawer来呈现Type3字体:

class Type3PDFRenderer extends PDFRenderer
{

    private PDFont font;

    public Type3PDFRenderer(PDDocument document, PDFont font)
    {
        super(document);
        this.font = font;
    }

    @Override
    protected PageDrawer createPageDrawer(PageDrawerParameters parameters) throws IOException
    {
        FontType3PageDrawer pd = new FontType3PageDrawer(parameters, this.font);
        pd.setAnnotationFilter(super.getAnnotationsFilter());//as done in the super class
        return pd;
    }       
}

class FontType3PageDrawer extends PageDrawer
{

    private PDFont font;

    public FontType3PageDrawer(PageDrawerParameters parameters, PDFont font) throws IOException
    {
        super(parameters);
        this.font = font;
    }

    @Override
    public PDGraphicsState getGraphicsState()
    {
        PDGraphicsState gs = super.getGraphicsState();
        gs.getTextState().setFont(this.font);
        return gs;
    }       
}

只需使用Type3PDFRenderer代替PDFRendered。当然,如果您有多种字体,则需要进行一些修改才能处理它们。

编辑:使用pdfbox 2.0.9进行测试