iText - 如何为PdfAnnotationInk设置笔触宽度和不透明度

时间:2014-12-16 04:05:47

标签: itext

绘制墨迹类型注释时,我应该调用哪些函数来设置笔触宽度和不透明度? 我已经浏览了PdfAnnotation和PDFStamp的类API,但似乎没有直接设置宽度和不透明度的函数。有什么建议?感谢。

我的示例程序:

    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 个答案:

答案 0 :(得分:2)

  

绘制墨迹类型注释时,我应该调用哪些函数来设置笔触宽度和不透明度?

有两个答案:

如果PDF查看器创建外观

PDF specification提及

  

BS 字典(可选)指定行的边框样式字典(参见表166)   绘制路径时应使用的宽度和虚线图案。

作为 Ink 注释字典的另一个特定条目。这至少允许您设置笔划宽度而不是不透明度。只需添加这样的一行

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);

将笔触宽度设置为5并得到如下结果:

Screenshot

如果PDF提供外观

PDF specification也提及

  

注释词典的 AP 条目(如果存在)应优先   通过 InkList BS 条目;见表168和12.5.5,“外观   流“。

因此,您可以创建PdfAppearance,使用其方法完全按照您的意愿创建外观,包括透明度,并将其设置为注释的正常外观。然后,PDF查看器将按照您的需要显示注释。