Search code examples
javapdfcolorspdfclown

How to change color of a text field in pdfclown?


I want to fill in a PDF form. I am using library Pdfclown for this.

I have a problem changing the color of a TextField. I can change font size without problems, but not the color of the text.

I put the code where I managed to set values in the PDF form:

public void setPDF(String Valor, String aField) {
    Form form = document.getForm();

    for (Field field : form.getFields().values()) {
        if (aField.equals(field.getName())) {
            DefaultStyle style = new DefaultStyle();
            style.setForeColor(DeviceRGBColor.get(Color.red));
            String newValue = Valor;                 
            field.setValue(newValue);                        
            style.apply(field);
        }
    }

}


Solution

  • DefaultStyle applies itself to TextField instances like this:

    ...
    if(isGraphicsVisibile())
    {
        composer.beginLocalState();
        composer.setLineWidth(lineWidth);
        composer.setFillColor(getBackColor());
        composer.setStrokeColor(getForeColor());
        composer.drawRectangle(frame, 5);
        composer.fillStroke();
        composer.end();
    }
    ...
    

    (apply(TextField) in DefaultStyle.java)

    Thus, you might have to set

    style.setGraphicsVisibile(true);
    

    before applying your style to field.