iText-如何为PdfAnnotationInk设置笔触宽度和不透明度 [英] iText - How to set stroke width and opacity for PdfAnnotationInk

查看:135
本文介绍了iText-如何为PdfAnnotationInk设置笔触宽度和不透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在绘制墨水类型注释时,应该调用哪些函数来设置笔划宽度和不透明度? 我已经遍历了PdfAnnotation和PDFStamp的类API,但是似乎没有直接设置宽度和不透明度的函数.有什么建议?谢谢.

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.

我的示例程序:

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

推荐答案

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

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

有两个答案:

PDF规范提及

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

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.

作为特定于 Ink 注释词典的另一个条目.这至少允许您设置笔画宽度,但不能设置 opacity .只需添加这样的行

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

将笔划宽度设置为5并获得如下结果:

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

PDF规范还提到

注释词典的 AP 条目(如果存在)应优先 在 InkList BS 条目上;请参见表168和12.5.5,外观 流."

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."

因此,您可以创建一个PdfAppearance,使用其方法创建所需外观的外观,包括透明度,并将其设置为注释的常规外观.然后,PDF查看器将根据需要显示注释.

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-如何为PdfAnnotationInk设置笔触宽度和不透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆