gpt4 book ai didi

java - 如何使用 pdfbox 2 设置复选框外观流的边框

转载 作者:行者123 更新时间:2023-12-02 09:53:07 24 4
gpt4 key购买 nike

我正在使用 PDFBox v2 从头开始​​创建 pdf,我遇到了复选框外观的问题,当单击复选框(并且未单击鼠标)时,复选框的边框不会出现。

我使用 tilman 官方文档中提供的代码示例来创建单选按钮并对其进行调整以创建复选框:

public void drawCheckBox() throws IOException {
for (Entry<String, List<InputCheckBox>> entry : myHash.entrySet()) {
String checkBoxKey = entry.getKey(); // radio buton key
List<InputCheckBox> checkBoxValue = entry.getValue(); // checkbox list(s)
PDCheckBox checkBox = new PDCheckBox(checkBoxValue.get(0).getAcroForm());
checkBox.setPartialName(checkBoxKey);
checkBox.setExportValues(Arrays.asList(checkBoxKey));

// couleur de la checkbox
PDAppearanceCharacteristicsDictionary appearanceCharacteristics = new PDAppearanceCharacteristicsDictionary(
new COSDictionary());
appearanceCharacteristics.setBorderColour(new PDColor(new float[] { 0, 0, 0 }, PDDeviceRGB.INSTANCE));
appearanceCharacteristics.setBackground(new PDColor(new float[] { 1, 1, 1 }, PDDeviceRGB.INSTANCE));

checkBoxValue.get(0).getAcroForm().getFields().add(checkBox);
List<PDAnnotationWidget> widgets = new ArrayList<>();
for (int i = 0; i < checkBoxValue.size(); i++) {
PDAnnotationWidget widget = new PDAnnotationWidget();
widget.setRectangle(new PDRectangle(checkBoxValue.get(i).getLeft(),
checkBoxValue.get(i).getPage().getMediaBox().getHeight()
- (checkBoxValue.get(i).getTop() + checkBoxValue.get(i).getHeight()),
checkBoxValue.get(i).getWidth(), checkBoxValue.get(i).getHeight()));

// border du checkbox
widget.setAppearanceCharacteristics(appearanceCharacteristics);
PDBorderStyleDictionary borderStyleDictionary = new PDBorderStyleDictionary();
borderStyleDictionary.setWidth(1);
borderStyleDictionary.setStyle(PDBorderStyleDictionary.STYLE_SOLID);
// creer les apparence de radio button pour l'état off et l'état activé
COSDictionary apNDict = new COSDictionary();
apNDict.setItem(COSName.Off,
createCheckBoxAppearanceStream(checkBoxValue.get(i).getDocument(), widget, false));
apNDict.setItem(COSName.ON,
createCheckBoxAppearanceStream(checkBoxValue.get(i).getDocument(), widget, true));

PDAppearanceDictionary appearance = new PDAppearanceDictionary();
PDAppearanceEntry appearanceNEntry = new PDAppearanceEntry(apNDict);
appearance.setNormalAppearance(appearanceNEntry);
// appliquer l'apparence dans le widget
widget.setBorderStyle(borderStyleDictionary);
widget.setPage(checkBoxValue.get(i).getPage());
widget.setAppearance(appearance);
widget.setParent(checkBox);
widget.setAppearanceState("Off");
// widget.setAnnotationName(key);
widget.setPrinted(true);
checkBoxValue.get(i).getPage().getAnnotations().add(widget);
widgets.add(widget);
checkBox.setWidgets(widgets);
}
}
}

// les methodes ci_dessous sert a creer l'apparence des checkBox selon leur
// état coché ou non
private static PDAppearanceStream createCheckBoxAppearanceStream(final PDDocument document,
PDAnnotationWidget widget, boolean on) throws IOException {
PDRectangle rect = widget.getRectangle();
PDAppearanceStream onAP = new PDAppearanceStream(document);
onAP.setResources(new PDResources());
onAP.setBBox(new PDRectangle(rect.getWidth(), rect.getHeight()));
PDPageContentStream onAPCS = new PDPageContentStream(document, onAP);

PDAppearanceCharacteristicsDictionary appearanceCharacteristics = widget.getAppearanceCharacteristics();
PDColor backgroundColor = appearanceCharacteristics.getBackground();
PDColor borderColor = appearanceCharacteristics.getBorderColour();
float lineWidth = getLineWidth(widget);
onAPCS.setLineWidth(lineWidth);
onAPCS.setNonStrokingColor(backgroundColor);
onAPCS.fill();
onAPCS.setStrokingColor(borderColor);
onAPCS.stroke();

if (on) {
onAPCS.setFont(PDType1Font.ZAPF_DINGBATS, 14.5f);
onAPCS.beginText();
onAPCS.newLineAtOffset(0, 0);
onAPCS.showText("\u2714");
onAPCS.endText();
onAPCS.fill();
}

onAPCS.close();
return onAP;
}

static float getLineWidth(PDAnnotationWidget widget) {
PDBorderStyleDictionary bs = widget.getBorderStyle();
if (bs != null) {
return bs.getWidth();
}
return 1;
}

这是我得到的结果:

here is the result that i'am getting

这是我应该拥有的:

and here is what i am supposed to have

最佳答案

您可以执行路径填充和路径描边,而无需先定义路径:

onAPCS.setNonStrokingColor(backgroundColor);
onAPCS.fill();
onAPCS.setStrokingColor(borderColor);
onAPCS.stroke();

尝试像这样定义路径(作为矩形):

onAPCS.setNonStrokingColor(backgroundColor);
onAPCS.setStrokingColor(borderColor);
onAPCS.addRect(0, 0, rect.getWidth(), rect.getHeight());
onAPCS.fillAndStroke();

(或者您可能想使用稍小的矩形,例如 onAPCS.addRect(1, 1, rect.getWidth() - 2, rect.getHeight() - 2)。)

<小时/>

顺便说一句,再往下你再次使用 fill,这次没有任何明显的原因:

if (on) {
...
onAPCS.endText();
onAPCS.fill();
}

您应该删除该 fill,因为严格来说它甚至是无效的:filllines 仅在路径定义之后才允许!

关于java - 如何使用 pdfbox 2 设置复选框外观流的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56171289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com