iText - PDFAppearence 问题
iText - PDFAppearence issue
我们正在使用 iText 将文本放入 PDF 的签名占位符中。我们使用类似于此的代码片段来定义签名外观
PdfStamper stp = PdfStamper.createSignature(inputReader, os, '[=10=]', tempFile2, true);
sap = stp.getSignatureAppearance();
sap.setVisibleSignature(placeholder);
sap.setRenderingMode(PdfSignatureAppearance.RenderingMode.DESCRIPTION);
sap.setCertificationLevel(PdfSignatureAppearance.NOT_CERTIFIED);
Calendar cal = Calendar.getInstance();
sap.setSignDate(cal);
sap.setLayer2Text(text+"\n"+cal.getTime().toString());
sap.setReason(text+"\n"+cal.getTime().toString()); `
一切正常,但签名文本并没有像我们预期的那样填充所有签名占位符区域,但填充区域的高度似乎约为可用区域的 70% space。
因此,有时特别是如果签名文本的长度很大,签名文本不适合占位符并且文本被剥离。
填写签名示例:
我查看了 PdfSignatureAppearence class 并在 getApperance() 方法中找到了这段代码片段,它负责此行为并在
时被调用
sap.setRenderingMode(PdfSignatureAppearance.RenderingMode.DESCRIPTION);
正在呼叫
else {
dataRect = new Rectangle(
MARGIN,
MARGIN,
rect.getWidth() - MARGIN,
rect.getHeight() * (1 - TOP_SECTION) - MARGIN);
}
我不明白这样做的原因,因为我希望文本可以使用所有可用的占位符高度,并具有适当的边距。
有什么方法可以绕过这种行为吗?
我们使用的是 iText 5.4.2,但较新的版本也包含相同的代码片段,因此我希望行为会相同。
作为@JJ。已经评论了,
TOP_SECTION is connected with acro6layers
rendering and the code [determining the datarect
in pure DESCRIPTION
mode] does not take into account the value of the acro6layer
flag.
除非有人想在 iText 5 代码本身中解决这个问题,否则使描述使用整个签名的最简单方法 space 是自己构建第 2 层外观。
要做到这一点,只需从 PdfSignatureAppearance.getLayer(2)
中检索 PdfTemplate
并在调用 PdfSignatureAppearance.setVisibleSignature
后根据需要填充它。 PdfSignatureAppearance
记住你已经取回了第 2 层,不再更改它。
对于手头的情况,我们基本上复制 PdfSignatureAppearance.getAppearance
代码以在纯描述模式下生成第 2 层,仅更正确定 datarect
:
的代码
PdfSignatureAppearance appearance = ...;
[...]
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
PdfTemplate layer2 = appearance.getLayer(2);
String text = "We're using iText to put a text inside a signature placeholder in a PDF. "
+ "We use a code snippet similar to this to define the Signature Appearence.\n"
+ "Everything works fine, but the signature text does not fill all the signature "
+ "placeholder area as expected by us, but the area filled seems to have an height "
+ "that is approximately the 70% of the available space.\n"
+ "As a result, sometimes especially if the length of the signature text is quite "
+ "big, the signature text does not fit in the placeholder and the text is striped "
+ "away.";
Font font = new Font();
float size = font.getSize();
final float MARGIN = 2;
Rectangle dataRect = new Rectangle(
MARGIN,
MARGIN,
appearance.getRect().getWidth() - MARGIN,
appearance.getRect().getHeight() - MARGIN);
if (size <= 0) {
Rectangle sr = new Rectangle(dataRect.getWidth(), dataRect.getHeight());
size = ColumnText.fitText(font, text, sr, 12, appearance.getRunDirection());
}
ColumnText ct = new ColumnText(layer2);
ct.setRunDirection(appearance.getRunDirection());
ct.setSimpleColumn(new Phrase(text, font), dataRect.getLeft(), dataRect.getBottom(), dataRect.getRight(), dataRect.getTop(), size, Element.ALIGN_LEFT);
ct.go();
(CreateSignature.java 测试 signWithCustomLayer2
)
(作为描述文本,我使用了问题正文中的一些段落。)
结果:
通过调整上面代码中的 MARGIN
值,甚至可以使用更多 are。但是,这可能会导致文本触及边框,这可能不是很漂亮。
顺便说一句:
if the length of the signature text is quite big, the signature text does not fit in the placeholder and the text is striped away.
如果您使用非正值初始化上面的 size
变量,if (size <= 0)
块中的代码将计算允许所有文本适合签名矩形的字体大小.这确实发生在上面的代码中,因为 new Font()
returns 大小为 UNDEFINED
的字体是一个常量 -1
.
我们正在使用 iText 将文本放入 PDF 的签名占位符中。我们使用类似于此的代码片段来定义签名外观
PdfStamper stp = PdfStamper.createSignature(inputReader, os, '[=10=]', tempFile2, true);
sap = stp.getSignatureAppearance();
sap.setVisibleSignature(placeholder);
sap.setRenderingMode(PdfSignatureAppearance.RenderingMode.DESCRIPTION);
sap.setCertificationLevel(PdfSignatureAppearance.NOT_CERTIFIED);
Calendar cal = Calendar.getInstance();
sap.setSignDate(cal);
sap.setLayer2Text(text+"\n"+cal.getTime().toString());
sap.setReason(text+"\n"+cal.getTime().toString()); `
一切正常,但签名文本并没有像我们预期的那样填充所有签名占位符区域,但填充区域的高度似乎约为可用区域的 70% space。
因此,有时特别是如果签名文本的长度很大,签名文本不适合占位符并且文本被剥离。
填写签名示例:
我查看了 PdfSignatureAppearence class 并在 getApperance() 方法中找到了这段代码片段,它负责此行为并在
时被调用sap.setRenderingMode(PdfSignatureAppearance.RenderingMode.DESCRIPTION);
正在呼叫
else {
dataRect = new Rectangle(
MARGIN,
MARGIN,
rect.getWidth() - MARGIN,
rect.getHeight() * (1 - TOP_SECTION) - MARGIN);
}
我不明白这样做的原因,因为我希望文本可以使用所有可用的占位符高度,并具有适当的边距。
有什么方法可以绕过这种行为吗?
我们使用的是 iText 5.4.2,但较新的版本也包含相同的代码片段,因此我希望行为会相同。
作为@JJ。已经评论了,
TOP_SECTION is connected with
acro6layers
rendering and the code [determining thedatarect
in pureDESCRIPTION
mode] does not take into account the value of theacro6layer
flag.
除非有人想在 iText 5 代码本身中解决这个问题,否则使描述使用整个签名的最简单方法 space 是自己构建第 2 层外观。
要做到这一点,只需从 PdfSignatureAppearance.getLayer(2)
中检索 PdfTemplate
并在调用 PdfSignatureAppearance.setVisibleSignature
后根据需要填充它。 PdfSignatureAppearance
记住你已经取回了第 2 层,不再更改它。
对于手头的情况,我们基本上复制 PdfSignatureAppearance.getAppearance
代码以在纯描述模式下生成第 2 层,仅更正确定 datarect
:
PdfSignatureAppearance appearance = ...;
[...]
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
PdfTemplate layer2 = appearance.getLayer(2);
String text = "We're using iText to put a text inside a signature placeholder in a PDF. "
+ "We use a code snippet similar to this to define the Signature Appearence.\n"
+ "Everything works fine, but the signature text does not fill all the signature "
+ "placeholder area as expected by us, but the area filled seems to have an height "
+ "that is approximately the 70% of the available space.\n"
+ "As a result, sometimes especially if the length of the signature text is quite "
+ "big, the signature text does not fit in the placeholder and the text is striped "
+ "away.";
Font font = new Font();
float size = font.getSize();
final float MARGIN = 2;
Rectangle dataRect = new Rectangle(
MARGIN,
MARGIN,
appearance.getRect().getWidth() - MARGIN,
appearance.getRect().getHeight() - MARGIN);
if (size <= 0) {
Rectangle sr = new Rectangle(dataRect.getWidth(), dataRect.getHeight());
size = ColumnText.fitText(font, text, sr, 12, appearance.getRunDirection());
}
ColumnText ct = new ColumnText(layer2);
ct.setRunDirection(appearance.getRunDirection());
ct.setSimpleColumn(new Phrase(text, font), dataRect.getLeft(), dataRect.getBottom(), dataRect.getRight(), dataRect.getTop(), size, Element.ALIGN_LEFT);
ct.go();
(CreateSignature.java 测试 signWithCustomLayer2
)
(作为描述文本,我使用了问题正文中的一些段落。)
结果:
通过调整上面代码中的 MARGIN
值,甚至可以使用更多 are。但是,这可能会导致文本触及边框,这可能不是很漂亮。
顺便说一句:
if the length of the signature text is quite big, the signature text does not fit in the placeholder and the text is striped away.
如果您使用非正值初始化上面的 size
变量,if (size <= 0)
块中的代码将计算允许所有文本适合签名矩形的字体大小.这确实发生在上面的代码中,因为 new Font()
returns 大小为 UNDEFINED
的字体是一个常量 -1
.