iText - How to set stroke width and opacity for Pd

2020-04-21 04:31发布

问题:

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 there are no functions to set the width and opacity directly. Any suggestions? Thanks.

My sample program:

    final String sourceFile = "C:\\PdfAnnotation\\sample.pdf";
    final String destFile = "C:\\PdfAnnotation\\output\\output.pdf";

    PdfReader reader = new PdfReader(sourceFile);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destFile));

    Rectangle rect = new Rectangle(52.92f, 397.56f, 173.36f, 530.67f);
    float[][] inkList = {{61.736111f,530.669250f,61.295139f,525.820984f,61.295139f,518.768860f,
            61.295139f,505.986969f,61.295139f,490.560547f,61.295139f,470.726562f,59.972221f,452.214844f,
            57.767361f,434.143890f,56.003471f,418.276703f,53.357639f,404.172516f,51.593750f,391.390625f,
            50.711807f,382.134766f,49.829861f,376.845703f},
            {68.350693f,453.537109f,73.201385f,453.977875f,79.375000f,453.977875f,85.107635f,453.977875f,92.163193f,453.977875f,
                    100.541664f,453.977875f,108.038193f,453.977875f,117.298615f,453.977875f},
            {112.447914f,509.072266f,112.006943f,505.105469f,112.006943f,498.053375f,112.006943f,488.797516f,112.006943f,472.930328f,
                    112.006943f,457.503906f,112.006943f,441.636719f,112.006943f,426.210297f,111.565971f,412.106110f,
                    111.125000f,401.968750f,111.125000f,391.831390f},
            {161.836807f,454.859375f,161.836807f,449.129547f,161.836807f,441.636719f,161.836807f,433.262360f,161.836807f,
                    423.125000f,161.836807f,412.546875f,161.836807f,405.054047f,161.836807f,398.442719f,161.836807f,392.712891f,
                    161.836807f,389.627594f},
            {163.159729f,485.712250f,170.215271f,469.845062f}
    };

    PdfAnnotation an = PdfAnnotation.createInk(stamper.getWriter(), rect, "", inkList);
    an.setColor(new BaseColor(30, 89, 255));
    an.setFlags(PdfAnnotation.FLAGS_PRINT);
    stamper.addAnnotation(an, 1);

    stamper.close();
    reader.close();

回答1:

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:

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.



标签: itext