[英]Signature appearance font color in layer2 (itext 7)
我正在尝试使用 iText7 7.1.0 (java) 中的字体颜色生成具有外观的签名。
使用 iText5,调用FontFactory.getFont()
时包含FontFactory.getFont()
,然后:
Font font = FontFactory.getFont(fontName, encoding, embedFont, fontSize, style, bColor);
appearance.setLayer2Font(font);
但是,在 iText7 中,Font 似乎丢失了 fontSize 和 fontColor 信息。 fontSize 有一个新的appearance.setLayer2FontSize()
.setLayer2FontSize appearance.setLayer2FontSize()
方法。 但我无法找到指示 layer2 字体颜色的方法。
我在文本或段落中找到了一个 setFontColor。
但是,在生成签名PdfSignatureAppearance.setLayer2Text(String)
,要调用的方法似乎是PdfSignatureAppearance.setLayer2Text(String)
,参数只是一个字符串。
如何在 iText7 中修改 layer2 字体颜色?
提前非常感谢。
显然,在将PdfSignatureAppearance
从iText 5移植到iText 7时,没有考虑过在iText 5字体对象中传输颜色的选项,至少我看不到任何将所需颜色传输到外观创建过程中的官方方法。
在这种情况下,显而易见的选择是手动创建第2层。 这样,您可以根据需要选择所有选项来设计外观。 而且,您可以复制并粘贴原始代码,包括必需的隐藏帮助程序方法,以从原始iText设计开始设计。
如果您不想这样做,即,如果您仍然希望iText创建外观并且仅对其进行一些调整,则可以采取一种变通方法:您可以要求iText创建外观,然后进行一些操作。
不幸的是,这现在需要反思,因为用于生成外观的PdfSignatureAppearance
方法getAppearance()
protected
。 (它曾经在iText 5中public
...)
如果您可以采用这种解决方法,则可以像这样为文本着色:
PdfSigner signer = ...;
PdfSignatureAppearance appearance = signer.getSignatureAppearance();
[... customize the appearance using its usual methods ...]
// call appearance.getAppearance() using reflection
// this initializes the layers in the appearance object
Method getAppearanceMethod = PdfSignatureAppearance.class.getDeclaredMethod("getAppearance");
getAppearanceMethod.setAccessible(true);
getAppearanceMethod.invoke(appearance);
// add a fill color setting instruction
// at the start of layer 2
PdfFormXObject layer2 = appearance.getLayer2();
PdfStream layer2Stream = layer2.getPdfObject();
byte[] layer2Bytes = layer2Stream.getBytes();
layer2Stream.setData("1 0 0 rg\n".getBytes());
layer2Stream.setData(layer2Bytes, true);
signer.signDetached(...);
( CreateSpecialSignatureAppearance测试方法testColorizeLayer2Text
)
由于没有显式设置最初生成的外观中的填充颜色,而是默认设置为黑色,因此该前置指令会将所有文本着色为红色(使用RGB颜色,其中100%为红色,0%为绿色和0%为蓝色)。
实际上,令iText 7仍然包含所有这些签名层的东西,我感到有些惊讶。 至少自从ISO 32000-1在2008年发布以来,除了支持Adobe查看器特定的行为(甚至Adobe自己在ISO 32000-1之前已经宣布弃用)之外,没有理由再使用这些图层。
有这么大的利益集团游说支持那些不赞成的行为吗?
您可以使用这种方式创建自定义第 2 层,然后对其进行修改。
// Create the signature appearance
Rectangle rect = new Rectangle(36, 50, 200, 100);
PdfSignatureAppearance appearance = signer.getSignatureAppearance();
appearance
.setLocation(location)
// Specify if the appearance before field is signed will be used
// as a background for the signed field. The "false" value is the default value.
.setReuseAppearance(false)
.setPageRect(rect)
.setPageNumber(r.getNumberOfPages());
signer.setFieldName("sig");
appearance.setLayer2Font(PdfFontFactory.createFont(StandardFonts.TIMES_ITALIC));
// Get the background layer and draw a gray rectangle as a background.
PdfFormXObject n0 = appearance.getLayer0();
float x = n0.getBBox().toRectangle().getLeft();
float y = n0.getBBox().toRectangle().getBottom();
float width = n0.getBBox().toRectangle().getWidth();
float height = n0.getBBox().toRectangle().getHeight();
PdfCanvas canvas = new PdfCanvas(n0, signer.getDocument());
canvas.setFillColor(ColorConstants.CYAN);
canvas.rectangle(x, y, width, height);
canvas.fill();
// Set the signature information on layer 2
PdfFormXObject n2 = appearance.getLayer2();
Paragraph p = new Paragraph("This document was signed by Bruno Specimen.");
new Canvas(n2, signer.getDocument()).add(p);
// Creating the signature
IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider);
IExternalDigest digest = new BouncyCastleDigest();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.