Detecting text field overflow

谁说胖子不能爱 提交于 2019-12-05 19:00:45

This code recreates the appearance stream to be sure that it exists so that there is a bbox (which can be a little bit smaller than the rectangle).

public static void main(String[] args) throws IOException
{
    // file can be found at https://issues.apache.org/jira/browse/PDFBOX-142
    // https://issues.apache.org/jira/secure/attachment/12742551/Testformular1.pdf
    try (PDDocument doc = PDDocument.load(new File("Testformular1.pdf")))
    {
        PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
        PDTextField field = (PDTextField) acroForm.getField("Name");
        PDAnnotationWidget widget = field.getWidgets().get(0);
        // force generation of appearance stream
        field.setValue(field.getValue());
        PDRectangle rectangle = widget.getRectangle();
        PDAppearanceEntry ap = widget.getAppearance().getNormalAppearance();
        PDAppearanceStream appearanceStream = ap.getAppearanceStream();
        PDRectangle bbox = appearanceStream.getBBox();
        float fieldWidth = Math.min(bbox.getWidth(), rectangle.getWidth());
        String defaultAppearance = field.getDefaultAppearance();
        System.out.println(defaultAppearance);

        // Pattern must be improved, font may have numbers
        // /Helv 12 Tf 0 g
        final Pattern p = Pattern.compile("\\/([A-z]+) (\\d+).+");
        Matcher m = p.matcher(defaultAppearance);
        if (!m.find() || m.groupCount() != 2)
        {
            System.out.println("oh-oh");
            System.exit(-1);
        }
        String fontName = m.group(1);
        int fontSize = Integer.parseInt(m.group(2));
        PDResources resources = appearanceStream.getResources();
        if (resources == null)
        {
            resources = acroForm.getDefaultResources();
        }
        PDFont font = resources.getFont(COSName.getPDFName(fontName));
        float stringWidth = font.getStringWidth("Tilman Hausherr Tilman Hausherr");
        System.out.println("stringWidth: " + stringWidth * fontSize / 1000);
        System.out.println("field width: " + fieldWidth);
    }
}

The output is:

/Helv 12 Tf 0 g
stringWidth: 180.7207
field width: 169.29082
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!