chitgoks
chitgoks

Reputation: 310

Box Cloud Annotation Appearance in iText

This is my sample code to draw a box cloud annotation. I used code in PDFBox's implementation to draw a box cloud but i have a little problem when used in iText. I modified the border class and some parts to be usable in iText.

you can find the border class here.

My problem is, the top and right border clouds are not drawn. it seems their location is drawn beyond the rect difference. I figure the issue is with drawing the curves in cloudyPolygonImpl(). maybe itext has different ways to draw in PdfAppearance? I am not sure.

This is the what i have so far. boxcloud

    public class Test {
    public static void main(String[] args) throws Exception {
    PdfReader reader = new PdfReader("src.pdf");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("result.pdf"));

    PdfDictionary be = new PdfDictionary();
    be.put(PdfName.S, PdfName.C);
    be.put(PdfName.I, new PdfNumber(1));

    Rectangle location = new Rectangle(123.6f, 584.4f, 252.6f, 653.4f);
    PdfAnnotation stamp = PdfAnnotation.createSquareCircle(stamper.getWriter(), location, "", true);
    stamp.setBorderStyle(new PdfBorderDictionary(1, PdfBorderDictionary.STYLE_SOLID));
    stamp.put(new PdfName("BE"), be);
    stamp.setColor(BaseColor.RED);

    PdfContentByte cb = stamper.getOverContent(1);
    PdfAppearance app = cb.createAppearance(location.getWidth(), location.getHeight());
    stamp.setAppearance(PdfName.N, app);

    PdfArray stickyRect = stamp.getAsArray(PdfName.RECT);
    Rectangle annotRect = new Rectangle(stickyRect.getAsNumber(0).floatValue(),
        stickyRect.getAsNumber(1).floatValue(),
        stickyRect.getAsNumber(2).floatValue(),
        stickyRect.getAsNumber(3).floatValue());

    PdfArray arrDiff = annotation.getAsArray(PdfName.RD);
    Rectangle annotRectDiff = null;
    if (arrDiff != null) {
            annotRectDiff = new Rectangle(arrDiff.getAsNumber(0).floatValue(), arrDiff.getAsNumber(1).floatValue(),
        arrDiff.getAsNumber(2).floatValue(), arrDiff.getAsNumber(3).floatValue()
    }

    // Create cloud appearance
    CBorder cborder = new CBorder(app, 1, 1, annotRect);
    cborder.createCloudyRectangle(annotRectDiff);

    stamp.put(PdfName.RECT, new PdfRectangle(cborder.getRectangle()));
    stamp.put(PdfName.RD, new PdfArray(new float[] { 
        cborder.getRectDifference().getLeft(), 
        cborder.getRectDifference().getBottom(), 
        cborder.getRectDifference().getRight(), 
        cborder.getRectDifference().getTop() }));

    app.rectangle(cborder.getBBox());
    app.transform(cborder.getMatrix());


    app.setColorStroke(BaseColor.RED);
    app.setLineWidth(1);
    app.stroke();

    stamper.addAnnotation(stamp, 1);
    stamper.close();
    reader.close();
}    
    }

The correct output should be that all borders be drawn with cloud but currently only the left and bottom are drawn.

Upvotes: 1

Views: 826

Answers (1)

mkl
mkl

Reputation: 95918

(This answer is based on the code in revision 3 of your question as the changes in revision 4 introduced multiple errors.)

Your code here creates an invalid annotation appearance stream:

CBorder cborder = new CBorder(app, 1, 1, annotRect);
cborder.createCloudyRectangle(null);

stamp.put(PdfName.RECT, new PdfRectangle(cborder.getRectangle()));
stamp.put(PdfName.RD, new PdfArray(new float[] { 
    cborder.getRectDifference().getLeft(), 
    cborder.getRectDifference().getBottom(), 
    cborder.getRectDifference().getRight(), 
    cborder.getRectDifference().getTop() }));

app.rectangle(cborder.getBBox());
app.transform(cborder.getMatrix());


app.setColorStroke(BaseColor.RED);
app.setLineWidth(1);
app.stroke();

Its upper part creates a path:

2 j
121.58 588.63 m
122.06 588.95 122.6 589.18 123.16 589.3 c
120.73 588.78 119.18 586.4 119.7 583.96 c
120.19 581.67 122.35 580.14 124.68 580.44 c
...
122.06 596.42 122.6 596.64 123.16 596.76 c
121.09 596.32 119.6 594.49 119.6 592.36 c
119.6 590.87 120.34 589.47 121.58 588.63 c
h

Then app.rectangle(cborder.getBBox()) does not create anything (beware, this rectangle overload does not what you expect it to do!).

Then app.transform(cborder.getMatrix()) adds a change to the current transformation matrix, app.setColorStroke(BaseColor.RED) adds a change of the stroking color, and app.setLineWidth(1) adds a change of the line width:

1 0 0 1 -118.68 -579.48 cm
1 0 0 RG
1 w

And finally app.stroke() adds the command to stroke the path:

S

But between the definition of a path and the corresponding path drawing command, only clipping path instructions are allowed! Cf. Figure 9 – Graphics Objects – in the PDF specification ISO 32000-1.

You can fix this code like this, pulling up color and line width changes, and directly using the cloud bounding box:

// Create cloud appearance
app.setColorStroke(BaseColor.RED);
app.setLineWidth(1);

CBorder cborder = new CBorder(app, 1, 1, annotRect);
cborder.createCloudyRectangle(null);

stamp.put(PdfName.RECT, new PdfRectangle(cborder.getRectangle()));
stamp.put(PdfName.RD, new PdfArray(new float[] { 
    cborder.getRectDifference().getLeft(), 
    cborder.getRectDifference().getBottom(), 
    cborder.getRectDifference().getRight(), 
    cborder.getRectDifference().getTop() }));

app.stroke();
app.setBoundingBox(cborder.getBBox());

(CloudBoxAnnotation test testDrawLikeChitgoksImproved)

This in particular changes the result (as seen in Adobe Acrobat) from

Screenshot original

to

Screenshot fix

Upvotes: 2

Related Questions