PDFBox中奇怪的组合框行为

时间:2017-02-08 13:58:23

标签: java pdfbox

我有这个代码在PDF文件中创建一个组合框。它有两个问题。

  1. 组合框打开时,特殊字符(如ö)会正确显示,但组合框关闭时无法显示。
  2. 当我在Acrobat中打开PDF时,更改值并保存PDF,组合框就不见了。当我再次打开PDF时,它不再显示。
  3. 我是否搞砸了PDFBox类或可能出现什么问题?

    这是处于打开状态的图片:

    opened

    这是一个处于关闭状态的人:

    closed

        public class ComboTest {
            public static void main(String[] args) {
    
            PDFont font = PDType1Font.HELVETICA;
            Color color = Color.BLACK;
            float fontSize = 12;
    
            PDDocument document = new PDDocument();
            PDPage page = new PDPage(PDRectangle.A4);
    
            document.addPage(page);
    
            PDAcroForm acroForm = new PDAcroForm(document);
            PDComboBox comboBox = new PDComboBox(acroForm);
            comboBox.setPartialName("test");
    
            String defaultAppearanceString = "/" + font.getName() + " " + fontSize + " Tf "
                    + 0 + " " + 0 + " " + 0 + " rg";
            comboBox.setDefaultAppearance(defaultAppearanceString);
    
            PDAnnotationWidget widget = new PDAnnotationWidget();
            widget.setRectangle(new PDRectangle(200, 200, 100, 20));
            widget.setAnnotationFlags(4);
            widget.setPage(page);
            widget.setParent(comboBox);
    
            List<String> exportValues = new ArrayList<>();
            List<String> displayValues = new ArrayList<>();
    
            displayValues.add("öne");
            displayValues.add("two");
            displayValues.add("thrée");
    
            exportValues.add("1");
            exportValues.add("2");
            exportValues.add("3");
    
    
            comboBox.setOptions(exportValues, displayValues);
    
            List<PDAnnotationWidget> widgets = new ArrayList<>();
            widgets.add(widget);
            try {
                page.getAnnotations().add(widget);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            comboBox.setWidgets(widgets);
    
    
            try {
                FileOutputStream output = new FileOutputStream("test.pdf");
                document.save(output);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
        }
    

1 个答案:

答案 0 :(得分:1)

在代码末尾附近添加:

acroForm.getFields().add(comboBox);
document.getDocumentCatalog().setAcroForm(acroForm);

这可以确保您的acroform及其字段为PDF所知。

重新使用特殊字符,用“Helv”替换Helvetica字体的名称,这是Adobe的标准名称。

更好,更清洁的解决方案:设置默认资源。

PDResources dr = new PDResources();
dr.put(COSName.getPDFName("Helv"), font);
acroForm.setDefaultResources(dr);

您也可以使用COSName.getPDFName(font.getName())代替“Helv”,但在默认外观字符串中必须相同。

现在完整的代码是:

public class ComboTest
{
    public static void main(String[] args)
    {

        PDFont font = PDType1Font.HELVETICA;
        Color color = Color.BLACK;
        float fontSize = 12;

        PDDocument document = new PDDocument();
        PDPage page = new PDPage(PDRectangle.A4);

        document.addPage(page);

        PDAcroForm acroForm = new PDAcroForm(document);
        PDComboBox comboBox = new PDComboBox(acroForm);
        comboBox.setPartialName("test");

        // Helv instead of Helvetica
        String defaultAppearanceString = "/Helv " + fontSize + " Tf "
                + 0 + " " + 0 + " " + 0 + " rg";
        comboBox.setDefaultAppearance(defaultAppearanceString);

        PDAnnotationWidget widget = new PDAnnotationWidget();
        widget.setRectangle(new PDRectangle(200, 200, 100, 20));
        widget.setAnnotationFlags(4);
        widget.setPage(page);
        widget.setParent(comboBox);

        List<String> exportValues = new ArrayList<>();
        List<String> displayValues = new ArrayList<>();

        displayValues.add("öne");
        displayValues.add("two");
        displayValues.add("thrée");

        exportValues.add("1");
        exportValues.add("2");
        exportValues.add("3");

        comboBox.setOptions(exportValues, displayValues);

        List<PDAnnotationWidget> widgets = new ArrayList<>();
        widgets.add(widget);
        try
        {
            page.getAnnotations().add(widget);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        comboBox.setWidgets(widgets);

        // new
        acroForm.getFields().add(comboBox);
        document.getDocumentCatalog().setAcroForm(acroForm);
        PDResources dr = new PDResources();
        dr.put(COSName.getPDFName("Helv"), font);
        acroForm.setDefaultResources(dr);

        try
        {
            FileOutputStream output = new FileOutputStream("test.pdf");
            document.save(output);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }
}
相关问题