itext7 - 如何覆盖 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);
}
}
解决方案
推荐阅读
- cuda - 为什么本地内存不适用于动态并行?
- postgresql - 升级到 Big Sur 后连接到本地 postgresql 数据库失败
- python - Python 3 - 在元组中打印出特定的键值
- geometry - 如何提取/生成与区域相交的线的交点坐标(在 fme-workbench 中)
- regex - 从字符串中排除字符不起作用
- swagger - 如何声明“列表”
' 在 OpenAPI/Swagger 文件中? - typescript - 打字稿,泛型函数可能返回回调函数
- javascript - 正则表达式中的有效格式问题
- typescript - amcharts 集成 SCRIPT1002 IE 11 中的语法错误
- php - 在 laravel 会话中使用它的键值忘记索引