athenatechie
athenatechie

Reputation: 699

Missing colored area on pdf using itext pdf

enter image description here

I am editing an existing pdf using itext pdf. While doing this, only a portion of rectangle box is showing as colored and some part is not highlighted. Looks like some overlay issue is happening here.

The yellow colour is not showing in complete rectangle.

    PdfContentByte canvas = stamper.getUnderContent(1);
    canvas.saveState();
    canvas.setColorFill(BaseColor.YELLOW);
    canvas.rectangle(36, 786, 66, 16);
    canvas.fill();
    canvas.restoreState();
    stamper.close();

Upvotes: 0

Views: 222

Answers (1)

mkl
mkl

Reputation: 95888

To make your task work, you should not draw under the existing content (as so that content can simply cover your mark) but instead over it. And to make the original content shine through, you should use an appropriate blend mode:

PdfContentByte canvas = stamper.getOverContent(1);
canvas.saveState();
PdfGState state = new PdfGState();
state.setBlendMode(new PdfName("Multiply"));
canvas.setGState(state);
canvas.setColorFill(BaseColor.YELLOW);
canvas.rectangle(36, 786, 66, 16);
canvas.fill();
canvas.restoreState();
stamper.close();

(MarkContent test)

You didn't share your PDF, so I had to try with a PDF I have here. Using an appropriately changed rectangle position and size the code marks this

Before

to look like this:

After

Upvotes: 2

Related Questions