PDFBox - 如果设置只读,则不显示复选框

时间:2015-09-04 13:57:53

标签: java pdf pdfbox

我正在使用PDFBox生成PDF,我需要添加一个复选框,需要将其预设为已检查和只读。但是有些它不起作用。

请找到下面的代码,它会在PDF上添加复选框:

DataLoader

现在,如果您取消注释该行:

import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSFloat;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckbox;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;

public class MyTest {
    public static void main(String arg[])  throws IOException, COSVisitorException
    {
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(PDPage.PAGE_SIZE_LETTER);
        document.addPage(page);

        COSDictionary acroFormDict = new COSDictionary();
        acroFormDict.setItem(COSName.getPDFName("Fields"), new COSArray()); // Added this line for Tilman's comment
        PDAcroForm acroForm = new PDAcroForm(document, acroFormDict);
        document.getDocumentCatalog().setAcroForm(acroForm);


        float x = 10f;
        float y = page.getMediaBox().getHeight();  
        float yOffset = 15f;
        float yCurrent = y - yOffset;  

        COSDictionary cosDict = new COSDictionary();
        COSArray rect = new COSArray();
        rect.add(new COSFloat(x));
        rect.add(new COSFloat(yCurrent));
        rect.add(new COSFloat(x+20));
        rect.add(new COSFloat(yCurrent-20));

        cosDict.setItem(COSName.RECT, rect);
        cosDict.setItem(COSName.FT, COSName.getPDFName("Btn")); // Field Type
        cosDict.setItem(COSName.TYPE, COSName.ANNOT);
        cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
        cosDict.setItem(COSName.T, new COSString("testChk"));
        cosDict.setItem(COSName.DA, new COSString("/Helv 7 Tf 0 g"));

        PDCheckbox checkbox = new PDCheckbox(acroForm, cosDict);
//      checkbox.setReadonly(true);
//      checkbox.setFieldFlags(PDField.FLAG_READ_ONLY);
        checkbox.setValue("Yes");
//      checkbox.check();
        page.getAnnotations().add(checkbox.getWidget());                          
        acroForm.getFields().add(checkbox); // Added this line for Tilman's comment

        yCurrent = yCurrent - 20 - yOffset;

        File file = new File("C:\\pdf\\CheckBox\\CheckBoxSample1.pdf");
        System.out.println("Sample file saved at : " + file.getAbsolutePath());
        document.save(file);
        document.close();
    }

}

或者

checkbox.setReadonly(true);

此复选框将不再显示(或可能存在,但未选中值)。 我正在使用PDFBox 1.8.10

Adob​​e Reader 11和Foxit中的行为类似。

此外,如果我在没有设置框的情况下生成PDF,在Adobe中,我会看到显示的值带有值设置,但是当我使用选项卡将焦点放在它上面时,它会消失。再次关注焦点(我点击复选框以外的其他地方),它再次出现。

似乎我错过了一件非常小的事情,但却找不到。 有帮助吗? 提前谢谢。

1 个答案:

答案 0 :(得分:2)

您需要使用PDAppearanceCharacteristicsDictionary类提供字段外观。这将解决上述问题。

请检查以下示例代码:

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSFloat;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.color.PDGamma;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDCheckbox;

public class MyCheckBox {
    public static void main(String arg[])  throws IOException, COSVisitorException
    {
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(PDPage.PAGE_SIZE_LETTER);
        document.addPage(page);

        PDFont font = PDType1Font.HELVETICA;
        PDResources res = new PDResources();
        String fontName = res.addFont(font);
        String da = "/" + fontName + " 10 Tf 0 0.4 0 rg";


        COSDictionary acroFormDict = new COSDictionary(); 
        acroFormDict.setBoolean(COSName.getPDFName("NeedAppearances"), true);
        acroFormDict.setItem(COSName.FIELDS, new COSArray());
        acroFormDict.setItem(COSName.DA, new COSString(da));

        PDAcroForm acroForm =  new PDAcroForm(document, acroFormDict);
        acroForm.setDefaultResources(res);
        document.getDocumentCatalog().setAcroForm(acroForm);


        float x = 10f;
        float y = page.getMediaBox().getHeight();  
        float yOffset = 15f;
        float yCurrent = y - yOffset;  

        PDGamma colourBlack = new PDGamma();
        PDAppearanceCharacteristicsDictionary fieldAppearance = 
                new PDAppearanceCharacteristicsDictionary(new COSDictionary());
        fieldAppearance.setBorderColour(colourBlack);
        COSArray arr = new COSArray();
        arr.add(new COSFloat(227/255f));
        arr.add(new COSFloat(239/255f));
        arr.add(new COSFloat(1f));
        fieldAppearance.setBackground(new PDGamma(arr));

        COSDictionary cosDict = new COSDictionary();
        COSArray rect = new COSArray();
        rect.add(new COSFloat(x));
        rect.add(new COSFloat(yCurrent));
        rect.add(new COSFloat(x+20));
        rect.add(new COSFloat(yCurrent-20));

        cosDict.setItem(COSName.RECT, rect);
        cosDict.setItem(COSName.FT, COSName.getPDFName("Btn")); // Field Type
        cosDict.setItem(COSName.TYPE, COSName.ANNOT);
        cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
        cosDict.setItem(COSName.TU, new COSString("Test Checkbox with PDFBox"));
        cosDict.setItem(COSName.T, new COSString("testChk"));
        cosDict.setItem(COSName.DA, new COSString("/F0 10 Tf 0 0.4 0 rg"));
//        cosDict.setInt(COSName.FF, 49152);        //Radio Button Symbol
        cosDict.setInt(COSName.F, 4);
//        cosDict.setInt(COSName.FF, 16384);

        PDCheckbox checkbox = new PDCheckbox(acroForm, cosDict);
        checkbox.setFieldFlags(PDCheckbox.FLAG_READ_ONLY);
        checkbox.setValue("Yes");
//        checkbox.setValue("Off");

        checkbox.getWidget().setAppearanceCharacteristics(fieldAppearance);

        page.getAnnotations().add(checkbox.getWidget());                          
        acroForm.getFields().add(checkbox); 

        yCurrent = yCurrent - 20 - yOffset;

        File file = new File("C:\\pdf\\CheckBoxSample.pdf");
        System.out.println("Sample file saved at : " + file.getAbsolutePath());
        document.save(file);
        document.close();
    }

}