Search in sources :

Example 1 with PDAppearanceCharacteristicsDictionary

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();
}
Also used : PDAppearanceCharacteristicsDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) ArrayList(java.util.ArrayList) PDBorderStyleDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Example 2 with PDAppearanceCharacteristicsDictionary

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);
}
Also used : PDAppearanceCharacteristicsDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PDBorderStyleDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Aggregations

PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)2 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)2 PDAppearanceCharacteristicsDictionary (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary)2 PDBorderStyleDictionary (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)2 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)1 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)1 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)1 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)1 PDAnnotationWidget (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1