I want to change my background color and fill color of my checkboxes into no color option,
How to change it on below code, Tried with multiple combinations but saving the file with alternative colors. I am new to PDF Box. If I remove these two values my check box itself not loading on the PDF.
Kindly help me out for changing the property value into no color
Here is my sample code,
private static void addCBField(PDDocument document, PDAcroForm acroForm, PDPage page, String name, boolean checked,
float x, float y, float width, float height, String toolTip, List<String> exportList) {
try {
PDRectangle rect = new PDRectangle(x, y, width, height);
PDCheckBox checkbox = new PDCheckBox(acroForm);
checkbox.setPartialName(name);
PDAnnotationWidget widget = checkbox.getWidgets().get(0);
widget.setPage(page);
widget.setRectangle(rect);
widget.setPrinted(true);
PDAppearanceCharacteristicsDictionary appearanceCharacteristics = new PDAppearanceCharacteristicsDictionary(
new COSDictionary());
appearanceCharacteristics.setBorderColour(new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE));
appearanceCharacteristics.setBackground(new PDColor(new float[] { 1, 1, 0 }, PDDeviceRGB.INSTANCE));
// 8 = cross; 4 = checkmark; H = star; u = diamond; n = square, l = dot
appearanceCharacteristics.setNormalCaption("4");
widget.setAppearanceCharacteristics(appearanceCharacteristics);
/*
* PDBorderStyleDictionary borderStyleDictionary = new
* PDBorderStyleDictionary(); borderStyleDictionary.setWidth(1);
* borderStyleDictionary.setStyle(PDBorderStyleDictionary.STYLE_SOLID);
* widget.setBorderStyle(borderStyleDictionary);
*/
PDAppearanceDictionary ap = new PDAppearanceDictionary();
widget.setAppearance(ap);
PDAppearanceEntry normalAppearance = ap.getNormalAppearance();
COSDictionary normalAppearanceDict = (COSDictionary) normalAppearance.getCOSObject();
normalAppearanceDict.setItem(COSName.Off, createCheckBoxAppearanceStream(document, widget, false));
normalAppearanceDict.setItem(COSName.YES, createCheckBoxAppearanceStream(document, widget, true));
page.getAnnotations().add(checkbox.getWidgets().get(0));
acroForm.getFields().add(checkbox);
checkbox.setAlternateFieldName(toolTip);
checkbox.setExportValues(exportList);
if (checked) {
checkbox.check();
} else {
checkbox.unCheck();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static PDAppearanceStream createCheckBoxAppearanceStream(final PDDocument document,
PDAnnotationWidget widget, boolean on) throws IOException {
PDRectangle rect = widget.getRectangle();
PDAppearanceCharacteristicsDictionary appearanceCharacteristics;
PDAppearanceStream yesAP = new PDAppearanceStream(document);
yesAP.setBBox(new PDRectangle(rect.getWidth(), rect.getHeight()));
yesAP.setResources(new PDResources());
PDPageContentStream yesAPCS = new PDPageContentStream(document, yesAP);
appearanceCharacteristics = widget.getAppearanceCharacteristics();
PDColor backgroundColor = appearanceCharacteristics.getBackground();
PDColor borderColor = appearanceCharacteristics.getBorderColour();
float lineWidth = getLineWidth(widget);
yesAPCS.setLineWidth(lineWidth); // border style (dash) ignored
yesAPCS.setNonStrokingColor(backgroundColor);
yesAPCS.addRect(0, 0, rect.getWidth(), rect.getHeight());
yesAPCS.fill();
yesAPCS.setStrokingColor(borderColor);
yesAPCS.addRect(lineWidth / 2, lineWidth / 2, rect.getWidth() - lineWidth, rect.getHeight() - lineWidth);
yesAPCS.stroke();
if (!on) {
yesAPCS.close();
return yesAP;
}
yesAPCS.addRect(lineWidth, lineWidth, rect.getWidth() - lineWidth * 2, rect.getHeight() - lineWidth * 2);
yesAPCS.clip();
String normalCaption = appearanceCharacteristics.getNormalCaption();
if (normalCaption == null) {
normalCaption = "4"; // Adobe behaviour
}
if ("8".equals(normalCaption)) {
// Adobe paints a cross instead of using the Zapf Dingbats cross symbol
yesAPCS.setStrokingColor(0f);
yesAPCS.moveTo(lineWidth * 2, rect.getHeight() - lineWidth * 2);
yesAPCS.lineTo(rect.getWidth() - lineWidth * 2, lineWidth * 2);
yesAPCS.moveTo(rect.getWidth() - lineWidth * 2, rect.getHeight() - lineWidth * 2);
yesAPCS.lineTo(lineWidth * 2, lineWidth * 2);
yesAPCS.stroke();
} else {
// The caption is not unicode, but the Zapf Dingbats code in the PDF
// Thus convert it back to unicode
// Assume that only the first character is used.
String name = PDType1Font.ZAPF_DINGBATS.codeToName(normalCaption.codePointAt(0));
String unicode = PDType1Font.ZAPF_DINGBATS.getGlyphList().toUnicode(name);
Rectangle2D bounds = PDType1Font.ZAPF_DINGBATS.getPath(name).getBounds2D();
float size = (float) Math.min(bounds.getWidth(), bounds.getHeight()) / 1000;
// assume that checkmark has square size
// the calculations approximate what Adobe is doing, i.e. put the glyph in the
// middle
float fontSize = (rect.getWidth() - lineWidth * 2) / size * 0.6666f;
float xOffset = (float) (rect.getWidth() - (bounds.getWidth()) / 1000 * fontSize) / 2;
xOffset -= bounds.getX() / 1000 * fontSize;
float yOffset = (float) (rect.getHeight() - (bounds.getHeight()) / 1000 * fontSize) / 2;
yOffset -= bounds.getY() / 1000 * fontSize;
yesAPCS.setNonStrokingColor(0f);
yesAPCS.beginText();
yesAPCS.setFont(PDType1Font.ZAPF_DINGBATS, fontSize);
yesAPCS.newLineAtOffset(xOffset, yOffset);
yesAPCS.showText(unicode);
yesAPCS.endText();
}
yesAPCS.close();
return yesAP;
}
private static float getLineWidth(PDAnnotationWidget widget) {
PDBorderStyleDictionary bs = widget.getBorderStyle();
if (bs != null) {
return bs.getWidth();
}
return 1;
}
After commenting out below mentioned lines it's working as expected,
appearanceCharacteristics.setBorderColour(new PDColor(new float[] { 0.75f }, PDDeviceRGB.INSTANCE));
appearanceCharacteristics.setBackground(new PDColor(new float[] {0.75f }, PDDeviceRGB.INSTANCE));
PDColor backgroundColor = appearanceCharacteristics.getBackground();
PDColor borderColor = appearanceCharacteristics.getBorderColour();
yesAPCS.setNonStrokingColor(backgroundColor);
yesAPCS.fill();
yesAPCS.setStrokingColor(borderColor);
yesAPCS.stroke();
yesAPCS.setNonStrokingColor(0f);
Thanks for your kind support @Tilman Hausherr