在itext7中获取表单字段字体信息 [英] Getting form field font information in itext7

查看:422
本文介绍了在itext7中获取表单字段字体信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用itext7解析PDF文档.我已经使用AcroForm从文档中获取了所有表单字段,但是我无法使用GetFont方法获取与该字段关联的字体.我也尝试解析/DA词典,但它以PDFString的形式返回.有什么办法可以获取字体信息,或者我必须解析/DA词典

I am parsing a PDF document using itext7. I have fetched all the form fields from the document using AcroForm, but I am unable to get font associated with the field using GetFont method. I also tried to parse /DA dictionary but it returns as a PDFString. Is there any way around to get font information or I have to parse /DA dictionary

推荐答案

实际上,iText 7确实具有一种确定表单字段字体信息的方法,毕竟生成表单字段外观是必需的:PdfFormField.getFontAndSize(PdfDictionary).

Actually iText 7 does have a method to determine form field font information, it's needed for generating form field appearances after all: PdfFormField.getFontAndSize(PdfDictionary).

不幸的是,此方法为protected,因此您必须作弊才能访问它,例如可以从中派生自己的表单字段类,并在其中公开该方法:

Unfortunately this method is protected, so one has to cheat a bit to access it, e.g. one can derive one's own form field class from it and make the method public therein:

class PdfFormFieldExt extends PdfFormField {
    public PdfFormFieldExt(PdfDictionary pdfObject) {
        super(pdfObject);
    }

    public Object[] getFontAndSize(PdfDictionary asNormal) throws IOException {
        return super.getFontAndSize(asNormal);
    }
}

(来自测试类使用此类,我们可以像这样提取字体信息:

Using this class we can extract font information like this:

try (   PdfReader pdfReader = new PdfReader(PDF_SOURCE);
        PdfDocument pdfDocument = new PdfDocument(pdfReader)    ) {
    PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDocument, false);
    for (Entry<String, PdfFormField> entry : form.getFormFields().entrySet()) {
        String fieldName = entry.getKey();
        PdfFormField field = entry.getValue();
        System.out.printf("%s - %s\n", fieldName, field.getFont());

        PdfFormFieldExt extField = new PdfFormFieldExt(field.getPdfObject());
        Object[] fontAndSize = extField.getFontAndSize(field.getWidgets().get(0).getNormalAppearanceObject());
        PdfFont font = (PdfFont) fontAndSize[0];
        Float size = (Float) fontAndSize[1];
        PdfName resourceName = (PdfName) fontAndSize[2];
        System.out.printf("%s - %s - %s - %s\n", Strings.repeat(" ", fieldName.length()),
                font.getFontProgram().getFontNames(), size, resourceName);
    }
}

((应用于

Applied to this sample document with some text fields, one gets:

TextAdobeThai - null
              - AdobeThai-Regular - 12.0 - /AdobeThai-Regular
TextArial - null
          - Arial - 12.0 - /Arial
TextHelvetica - null
              - Helvetica - 12.0 - /Helv
TextWingdings - null
              - Wingdings - 12.0 - /Wingdings

如您所见,虽然PdfFormField.getFont()始终返回null,但是PdfFormField.getFontAndSize(PdfDictionary)返回明智的信息.

As you can see, while PdfFormField.getFont() always returns null, PdfFormField.getFontAndSize(PdfDictionary) returns sensible information.

使用当前的iText for Java开发分支7.1.5-SNAPSHOT

这篇关于在itext7中获取表单字段字体信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆