How to set the font color for PDAnnotationFreeText with using pdfbox 2.0.16

怎甘沉沦 提交于 2019-12-11 19:55:49

问题


After imported XFDF to FDFAnnotation and converted to PDAnnotation, I find that the font color of PDAnnotationFreeText is missing.

<freetext page="0" rect="376.81193,778.184946,500.283994,807.936941" flags="print" name="373b558a-4892-5fb4-a9aa-b4d7175d3966" title="wwh" subject="Free text" date="D:20190808175721+08'00'" width="0" creationdate="D:20190808175700+08'00'" TextColor="#00CC63" FontSize="17">
    <contents>English</contents>
    <defaultappearance>0 0 0 rg /Arial 17 Tf</defaultappearance>
    <defaultstyle>font: Arial 17pt; text-align: left; color: #00CC63</defaultstyle>
</freetext>

and this is the details after import to FDFAnnotation

COSDictionary{
    COSName{Type}:COSName{Annot};
    COSName{Page}:COSInt{0};
    COSName{M}:COSString{D:20190808175721+08'00'};
    COSName{F}:COSInt{4};
    COSName{NM}:COSString{373b558a-4892-5fb4-a9aa-b4d7175d3966};
    COSName{Rect}:COSArray{COSFloat{376.81192};COSFloat{778.18494};COSFloat{500.284};COSFloat{807.93695};};
    COSName{T}:COSString{wwh};
    COSName{CreationDate}:COSString{D:20190808175700+08'00'};
    COSName{Subj}:COSString{Free text};
    COSName{IT}:COSName{};
    COSName{Contents}:COSString{English};
    COSName{Subtype}:COSName{FreeText};
    COSName{Q}:0;
    COSName{DA}:COSString{0 0 0 rg /Arial 17 Tf};
    COSName{DS}:COSString{font: Arial 17pt; text-align: left; color: #00CC63};
}

I have extract the color hex code and tried to convert to PDColor

int c = Integer.parseInt("#00CC63".substring(1), 16);
float r = ((c & 0xFF0000) >> 16) / 255f;
float g = ((c & 0x00FF00) >>  8) / 255f;
float b = ((c & 0x0000FF) >>  0) / 255f;
PDColor pdc = new PDColor( new float[] { r, g, b }, PDDeviceRGB.INSTANCE);

But the PDColor is only change the background color, not my expectation. May I know how to set the font color on it?

This is my sample file: https://1.bitsend.jp/download/c10903041b8af47195daeef1f471a366.html


回答1:


This will be fixed in PDFBox 2.0.17, the color of the /DS entry (annotation.getDefaultStyleString()) will be considered too and will have priority over the color in the /DA entry (annotation.getDefaultAppearance()). The new implementation can be seen here, a snapshot is available here.



来源:https://stackoverflow.com/questions/57459335/how-to-set-the-font-color-for-pdannotationfreetext-with-using-pdfbox-2-0-16

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!