首页 > 解决方案 > 如何覆盖 itext 复选框检查的默认黑色?

问题描述

目前,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(...)方法只是用setFillColor(ColorConstants.BLUE)替换resetFillColorRgb()调用?

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

任何提示将不胜感激:)


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

    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);
    }
}

标签: itext7

解决方案


推荐阅读