PDF-Checkbox - check one and uncheck another without JavaScript

混江龙づ霸主 提交于 2019-12-24 15:33:19

问题


in my project I need 3 checkboxes which should work like radiobuttons. But radiobuttons have some drawbacks with appearance in Adobe Reader. For this reason I have to use checkboxes. There is a very interesting answer, but I don't understand how this can be done with iText:

Java iText and custom Radiobutton behaviour

Can anybody -- especially Lonzak, the author of this posting -- help me in this case. Thanks and Kind regards, Dirk


回答1:


I think the solution is to fix your current code. The following example creates 3 radiobuttons and all contain a cross. It was based on itext 5 and you may have to adapt it a bit for itext 7.

Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
        document.open();

        RadioCheckField bt = new RadioCheckField(writer, new Rectangle(261, 576, 279, 594), "radio", "value1");
        bt.setCheckType(RadioCheckField.TYPE_CROSS);
        bt.setBackgroundColor(BaseColor.WHITE);
        //bt.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
        bt.setBorderColor(BaseColor.BLACK);
        bt.setTextColor(BaseColor.BLACK);
        bt.setBorderWidth(BaseField.BORDER_WIDTH_THIN);
        bt.setChecked(false);
        PdfFormField f1 = bt.getRadioField();
        bt.setOnValue("value2");
        bt.setChecked(true);
        bt.setBox(new Rectangle(287, 577, 305, 595));
        PdfFormField f2 = bt.getRadioField();
        bt.setChecked(false);
        PdfFormField top = bt.getRadioGroup(true, false);
        bt.setOnValue("value3");
        bt.setBox(new Rectangle(314, 578, 332, 596));
        PdfFormField f3 = bt.getRadioField();
        top.addKid(f1);
        top.addKid(f2);
        top.addKid(f3);
        writer.addAnnotation(top);
        document.close();

Update: Here is your code with iText7 however there seems to be a bug so that the check style is not changed for radiobuttons. Maybe the itext guys can say more...

PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output7.pdf"));
Document doc = new Document(pdfDoc);

PdfButtonFormField radioGroup = PdfFormField.createRadioGroup(pdfDoc, "ExampleGroup", "");
radioGroup.setPage(1);

pdfDoc.addNewPage();

PdfFormField field1 = PdfFormField.createRadioButton(pdfDoc, new Rectangle(261, 576, 18, 18), radioGroup, "value1");
field1.setCheckType(PdfFormField.TYPE_CROSS);
field1.setValue("value1");
field1.regenerateField();

PdfFormField field2 = PdfFormField.createRadioButton(pdfDoc, new Rectangle(287, 577, 18, 18), radioGroup, "value2");
field2.setCheckType(PdfFormField.TYPE_CROSS);
field2.regenerateField();

PdfFormField field3 = PdfFormField.createRadioButton(pdfDoc, new Rectangle(314, 578, 18, 18), radioGroup, "value3");
field3.setCheckType(PdfFormField.TYPE_CROSS);
field3.regenerateField();

PdfAcroForm.getAcroForm(pdfDoc, true).addField(radioGroup);
doc.close();



回答2:


Final solution with iText 7:

public static void main(String[] args) throws Exception {
    // RadioButton with cross instead of bullet.
    final String filename = "SampleRadioButton.pdf";
    final float boxLength = 20;
    final float crossWidth = 1;
    final String[] languages = { "Dutch", "English", "French" };
    try (PdfWriter writer = new PdfWriter(filename); PdfDocument pdfDoc = new PdfDocument(writer); Document doc = new Document(pdfDoc)) {
        pdfDoc.addNewPage();
        final PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
        final PdfButtonFormField radioGroup = PdfFormField.createRadioGroup(pdfDoc, "Language", languages[1]);
        int count = 0;
        for (final String language : languages) {
            count++;
            // button
            final Rectangle rect = new Rectangle(40, 800 - (count * 30), boxLength, boxLength);
            final PdfFormField radioButton = PdfFormField.createRadioButton(pdfDoc, rect, radioGroup, language);
            final PdfDictionary radioDict = radioButton.getPdfObject();
            final PdfDictionary mk = new PdfDictionary();
            mk.put(PdfName.CA, new PdfString("8")); // check=4, circle=1, cross=8, diamond=u, square=n, star=H
            radioDict.put(PdfName.MK, mk);
            radioButton.setVisibility(PdfFormField.VISIBLE);
            editAppearance(pdfDoc, radioButton, language, boxLength, crossWidth);
            // text
            final Paragraph para = new Paragraph(language).setFontSize(18);
            doc.showTextAligned(para, 70, 800 - (count * 30), TextAlignment.LEFT);
        }
        form.addField(radioGroup);
    }
    Desktop.getDesktop().open(new File(filename));
}

private static void editAppearance(PdfDocument pdfDoc, PdfFormField radioButton, String value, float length, float crossWidth) {
    final PdfStream streamOn = (PdfStream) new PdfStream().makeIndirect(pdfDoc);
    final PdfCanvas canvasOn = new PdfCanvas(streamOn, new PdfResources(), pdfDoc);
    final Rectangle rect = new Rectangle(0, 0, length, length);
    final PdfFormXObject xObjectOn = new PdfFormXObject(rect);
    canvasOn.saveState();
    canvasOn.setStrokeColor(ColorConstants.BLACK).setLineWidth(crossWidth);
    // bottom left to top right
    canvasOn.moveTo(0, 0).lineTo(length, length).stroke();
    // Top left to bottom right
    canvasOn.moveTo(0, length).lineTo(length, 0).stroke();
    canvasOn.restoreState();
    xObjectOn.getPdfObject().getOutputStream().writeBytes(streamOn.getBytes());
    final PdfWidgetAnnotation widget = radioButton.getWidgets().get(0);
    widget.setNormalAppearance(new PdfDictionary());
    widget.getNormalAppearanceObject().put(new PdfName(value), xObjectOn.getPdfObject());
}

}



来源:https://stackoverflow.com/questions/49931569/pdf-checkbox-check-one-and-uncheck-another-without-javascript

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