iText - How to set stroke width and opacity for PdfAnnotationInk

前端 未结 1 1522
太阳男子
太阳男子 2021-01-23 12:11

What functions should I call to set the stroke width and opacity when drawing ink type annotation? I have go through the class API for PdfAnnotation and PDFStamp, but it seems t

相关标签:
1条回答
  • 2021-01-23 12:35

    What functions should I call to set the stroke width and opacity when drawing ink type annotation?

    There are two answers:

    If the PDF viewer creates the appearance

    The PDF specification mentions

    BS dictionary (Optional) A border style dictionary (see Table 166) specifying the line width and dash pattern that shall be used in drawing the paths.

    as another entry specific to the Ink annotation dictionary. This at least allows you to set the stroke width but not the opacity. Simply add a line like this

    PdfAnnotation an = PdfAnnotation.createInk(stamper.getWriter(), rect, "", inkList);
    an.setColor(new BaseColor(30, 89, 255));
    an.setFlags(PdfAnnotation.FLAGS_PRINT);
    // vvv set line width to 5:
    an.setBorderStyle(new PdfBorderDictionary(5, PdfBorderDictionary.STYLE_SOLID));
    // ^^^ set line width to 5:
    stamper.addAnnotation(an, 1);
    

    to set the stroke width to 5 and get a result like this:

    Screenshot

    If the PDF supplies the appearance

    The PDF specification also mentions

    The annotation dictionary’s AP entry, if present, shall take precedence over the InkList and BS entries; see Table 168 and 12.5.5, “Appearance Streams.”

    Thus, you can create a PdfAppearance, use its methods to create an appearance exactly as you want it, including transparency, and set it as the normal appearance of the annotation. PDF viewers then shall display the annotation just like you want.

    0 讨论(0)
提交回复
热议问题