use of com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary in project PdfBox-Android by TomRoush.
the class TestCheckBox method testCheckBoxNoAppearance.
/**
* PDFBOX-4366: Create and test a checkbox with no /AP. The created file works with Adobe Reader!
*
* @throws IOException
*/
public void testCheckBoxNoAppearance() throws IOException {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDAcroForm acroForm = new PDAcroForm(doc);
// need this or it won't appear on Adobe Reader
acroForm.setNeedAppearances(true);
doc.getDocumentCatalog().setAcroForm(acroForm);
List<PDField> fields = new ArrayList<PDField>();
PDCheckBox checkBox = new PDCheckBox(acroForm);
checkBox.setPartialName("checkbox");
PDAnnotationWidget widget = checkBox.getWidgets().get(0);
widget.setRectangle(new PDRectangle(50, 600, 100, 100));
PDBorderStyleDictionary bs = new PDBorderStyleDictionary();
bs.setStyle(PDBorderStyleDictionary.STYLE_SOLID);
bs.setWidth(1);
COSDictionary acd = new COSDictionary();
PDAppearanceCharacteristicsDictionary ac = new PDAppearanceCharacteristicsDictionary(acd);
ac.setBackground(new PDColor(new float[] { 1, 1, 0 }, PDDeviceRGB.INSTANCE));
ac.setBorderColour(new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE));
// 4 is checkmark, 8 is cross
ac.setNormalCaption("4");
widget.setAppearanceCharacteristics(ac);
widget.setBorderStyle(bs);
checkBox.setValue("Off");
fields.add(checkBox);
page.getAnnotations().add(widget);
acroForm.setFields(fields);
assertEquals("Off", checkBox.getValue());
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary in project PdfBox-Android by TomRoush.
the class AppearanceGeneratorHelper method initializeAppearanceContent.
/**
* Initialize the content of the appearance stream.
*
* Get settings like border style, border width and colors to be used to draw a rectangle and background color
* around the widget
*
* @param widget the field widget
* @param appearanceStream the appearance stream to be used
* @throws IOException in case we can't write to the appearance stream
*/
private void initializeAppearanceContent(PDAnnotationWidget widget, PDAppearanceStream appearanceStream) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
PDPageContentStream contents = new PDPageContentStream(field.getAcroForm().getDocument(), appearanceStream, output);
PDAppearanceCharacteristicsDictionary appearanceCharacteristics = widget.getAppearanceCharacteristics();
// TODO: support more entries like patterns, etc.
if (appearanceCharacteristics != null) {
PDColor backgroundColour = appearanceCharacteristics.getBackground();
if (backgroundColour != null) {
contents.setNonStrokingColor(backgroundColour);
PDRectangle bbox = resolveBoundingBox(widget, appearanceStream);
contents.addRect(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getWidth(), bbox.getHeight());
contents.fill();
}
float lineWidth = 0f;
PDColor borderColour = appearanceCharacteristics.getBorderColour();
if (borderColour != null) {
contents.setStrokingColor(borderColour);
lineWidth = 1f;
}
PDBorderStyleDictionary borderStyle = widget.getBorderStyle();
if (borderStyle != null && borderStyle.getWidth() > 0) {
lineWidth = borderStyle.getWidth();
}
if (lineWidth > 0 && borderColour != null) {
if (lineWidth != 1) {
contents.setLineWidth(lineWidth);
}
PDRectangle bbox = resolveBoundingBox(widget, appearanceStream);
PDRectangle clipRect = applyPadding(bbox, Math.max(DEFAULT_PADDING, lineWidth / 2));
contents.addRect(clipRect.getLowerLeftX(), clipRect.getLowerLeftY(), clipRect.getWidth(), clipRect.getHeight());
contents.closeAndStroke();
}
}
contents.close();
output.close();
writeToStream(output.toByteArray(), appearanceStream);
}
Aggregations