我试图使用iText7 7.1.0 (java)中的字体颜色生成外观签名。
使用iText5,在调用FontFactory.getFont()
时包含了fontColor,然后:
Font font = FontFactory.getFont(fontName, encoding, embedFont, fontSize, style, bColor);
appearance.setLayer2Font(font);
但是,在iText7中,字体似乎丢失了fontSize和fontColor信息。有一个新的appearance.setLayer2FontSize()
方法用于fontSize。但我无法找到指示layer2字体颜色的方法。
我在clases文本或段落中找到了一个setFontColor。
但是,在生成signatureApperance时,要调用的方法似乎是PdfSignatureAppearance.setLayer2Text(String)
,参数只是一个字符串。
如何在layer2中修改iText7字体颜色?
提前谢谢。
发布于 2018-01-29 07:26:44
显然,在将iText 5移植到PdfSignatureAppearance
5到iText 7时,没有考虑到在iText 5字体对象中传输颜色的选项,至少我没有看到将所需颜色传输到外观创建过程中的任何官方方法。
在这种情况下,最明显的选择是手动创建第2层。这样做,您有所有的选项,以设计外观,你想要的。您可以复制和粘贴原始代码,包括所需的隐藏助手方法,以便从原始iText设计开始您的设计。
如果您不想这样做,也就是说,如果您仍然希望iText创建外观,而只是稍微调整一下外观,那么就有一个解决办法:您可以要求iText创建外观,然后对它们进行一些操作。
不幸的是,这现在需要反射,因为生成外观的PdfSignatureAppearance
方法getAppearance()
是protected
。(它曾经是public
in iText 5.)
如果你对这样的工作没意见,你可以把你的文字涂成这样的颜色:
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(...);
(https://github.com/mkl-public/testarea-itext7/blob/master/src/test/java/mkl/testarea/itext7/signature/CreateSpecialSignatureAppearance.java#L79测试方法testColorizeLayer2Text
__)
由于最初生成的外观中的填充颜色不是显式设置的,而是默认设置为黑色的,此预置指令将所有文本红色(使用100%红色、0%绿色和0%蓝色的RGB颜色)着色。
实际上,我对iText 7仍然携带着所有这些签名层内容感到有点惊讶。至少自从ISO 32000-1在2008年发布以来,除了支持Adobe特有的行为之外,没有理由再使用这些层了,即使是Adobe自己也在ISO 32000-1之前几年就已经声明了这些行为。
是否有这么大的利益集团游说支持这些不受欢迎的行为?
发布于 2022-01-12 06:49:05
您可以使用这种方法创建自定义层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();
https://stackoverflow.com/questions/48467637
复制相似问题