如何覆盖itext复选框的默认黑色?

时间:2018-12-10 15:50:07

标签: itext7

当前,iText 7不允许以简单方式/通过任何方式设置复选框的颜色,因为它在源代码中是固定的:

    /**
 * Performs the low-level drawing operations to draw a checkbox object.
 *
 * @param canvas   the {@link PdfCanvas} of the page to draw on.
 * @param width    the width of the button
 * @param height   the width of the button
 * @param fontSize the size of the font
 * @param on       the boolean value of the checkbox
 */
protected void drawCheckBox(PdfCanvas canvas, float width, float height, float fontSize, boolean on) {
    if (!on) {
        return;
    }

    if (checkType == TYPE_CROSS) {
        DrawingUtil.drawCross(canvas, width, height, borderWidth);
        return;
    }
    PdfFont ufont = getFont();
    if (fontSize <= 0) {
        fontSize = approximateFontSizeToFitBBox(ufont, new Rectangle(width, height), text);
    }
    // PdfFont gets all width in 1000 normalized units
    canvas.
            beginText().
            setFontAndSize(ufont, fontSize).
            resetFillColorRgb().
            setTextMatrix((width - ufont.getWidth(text, fontSize)) / 2, (height - ufont.getAscent(text, fontSize)) / 2).
            showText(text).
            endText();
}

是否存在一种无需更改 PdfFormField 类并覆盖 drawCheckBox(...)的明智的方式即可更改我们不知道的复选框选中的颜色方法只是将 resetFillColorRgb()调用替换为 setFillColor(ColorConstants.BLUE)

我们这种方法的问题在于,我们还需要替换pdf文档中的现有复选框,这意味着我们必须将所有属性从该复选框复制到PdfF​​ormField子类的新创建的复选框。

任何提示将不胜感激:)


编辑:这是临时解决方案,直到iText提供API或其他人找出更好的方法:

    private static void overrideCheckboxCheckedColor(PdfDocument pdfDocument, PdfFormField pdfFormField, Color color) {
    try {
        // Get "Yes" state appearance object
        PdfWidgetAnnotation pdfWidgetAnnotation = pdfFormField.getWidgets().get(0);
        PdfDictionary normalAppearanceObject = pdfWidgetAnnotation.getNormalAppearanceObject();
        PdfStream onStateObject = normalAppearanceObject.getAsStream(new PdfName("Yes"));

        // Extract font size
        ByteArrayOutputStream outputStream = (ByteArrayOutputStream) onStateObject.getOutputStream().getOutputStream();
        String outputStreamContent = outputStream.toString();
        Pattern fontSizePattern = Pattern.compile("(?s)^.*\\s+((\\d+\\.)?\\d+)\\s+Tf.*$");
        Matcher fontSizeMatcher = fontSizePattern.matcher(outputStreamContent);
        boolean fontSizeFound = fontSizeMatcher.find();
        float fontSize = fontSizeFound ? Float.parseFloat(fontSizeMatcher.group(1)) : 10F;

        // Get width, height, font & text
        PdfFormXObject xObjectOn = (PdfFormXObject) PdfXObject.makeXObject(onStateObject);
        float width = xObjectOn.getWidth();
        float height = xObjectOn.getHeight();
        PdfFont font = pdfFormField.getFont();
        String text = "4";

        // Override checked color
        PdfCanvas canvasOn = new PdfCanvas(onStateObject, new PdfResources(), pdfDocument);
        canvasOn.beginText()
                .setFontAndSize(font, fontSize)
                .setFillColor(color)
                .setTextMatrix((width - font.getWidth(text, fontSize)) / 2, (height - font.getAscent(text, fontSize)) / 2)
                .showText(text)
                .endText();
    } catch (Exception e) {
        Message message = new FormattedMessage("Could not override '{}' field checked color", pdfFormField.getFieldName());
        LOG.error(message, e);
    }
}

0 个答案:

没有答案