iTextSharp 填充现有 pdf,但多行文本字段不会换行
iTextSharp filling an existing pdf but the multiline text field won't wrap
我在 Adobe Acrobat Pro 2017 中有一个 PDF I created/edit。目标是在我们的网站上动态填写它。除了一部分之外,它在所有方面都是成功的;表单域之一不会按预期换行文本。
var TheForm = PdfAcroForm.GetAcroForm(TheDocument, true);
var Fields = TheForm.GetFormFields();
//* just trying some stuff, it didn't pan out
//Fields["MyField"].SetFieldFlag(PdfTextFormField.FF_MULTILINE).SetFontSizeAutoScale();
//#This set the value correctly, but it doesn't wrap
Fields["MyField"].SetValue("Field Office Wide - Fuelwood");
//# proof the field is multiline, this is true in debugging
var helpme = Fields["MyField"].IsMultiline();
//# proof the field is set correctly, just the display won't wrap
var grrrrr = Fields["MyField"].GetValue();
//# make the PDF read-only and remove the form fields
TheForm.FlattenFields();
这是我用 Acrobat 手动输入的 PDF 表单输出 Reader 2017
这是代码的输出
这是该文件的匿名版本
https://github.com/Wyseguys/wyseguys.github.io/blob/Wyseguys-patch-1/form.pdf(这不是什么大秘密,只是保持安静)
此外,这可能是一个相关问题,我现在正在研究这个问题
Text not fitting into form fields (iTextSharp)
iText 7 尚未确定整个文本适合表单域的字体大小:
/// <summary>Adjust font in case autosize.</summary>
private float GetFontSize(PdfArray bBox, String value) {
if (this.fontSize == 0) {
//We do not support autosize with multiline.
if (IsMultiline() || bBox == null || value == null || String.IsNullOrEmpty(value)) {
return DEFAULT_FONT_SIZE;
}
else {
return Math.Max(ApproximateFontSizeToFitBBox(this.font, bBox.ToRectangle(), value), MIN_FONT_SIZE);
}
}
return this.fontSize;
}
(PdfFormField
辅助方法)
(如您所见,DEFAULT_FONT_SIZE
用作字体大小。此值定义为12
。)
特别是 iText 7 不支持您在表单字段上设置的 DoNotScroll 表单字段标志。
因此文本 "Field Office Wide - Fuelwood" 被分成两行。但是,由于字段小部件外观流包含沿字段小部件边框的剪辑路径,因此您只能看到第一行。此剪辑路径还可以让您仅看到展平视图中的第一行,但您可以将光标导航到第二行,select 那里的文本。
因此,要填充多行字段,请将它们的字体大小设置为可能会完成工作的值,即允许所有可能的输入适合小部件区域。或者,如果您无法控制 PDF 表单,请在填写之前删除 Multiline 标志,以使 iText 自动调整为单行。
我在 Adobe Acrobat Pro 2017 中有一个 PDF I created/edit。目标是在我们的网站上动态填写它。除了一部分之外,它在所有方面都是成功的;表单域之一不会按预期换行文本。
var TheForm = PdfAcroForm.GetAcroForm(TheDocument, true);
var Fields = TheForm.GetFormFields();
//* just trying some stuff, it didn't pan out
//Fields["MyField"].SetFieldFlag(PdfTextFormField.FF_MULTILINE).SetFontSizeAutoScale();
//#This set the value correctly, but it doesn't wrap
Fields["MyField"].SetValue("Field Office Wide - Fuelwood");
//# proof the field is multiline, this is true in debugging
var helpme = Fields["MyField"].IsMultiline();
//# proof the field is set correctly, just the display won't wrap
var grrrrr = Fields["MyField"].GetValue();
//# make the PDF read-only and remove the form fields
TheForm.FlattenFields();
这是我用 Acrobat 手动输入的 PDF 表单输出 Reader 2017
这是代码的输出
这是该文件的匿名版本 https://github.com/Wyseguys/wyseguys.github.io/blob/Wyseguys-patch-1/form.pdf(这不是什么大秘密,只是保持安静)
此外,这可能是一个相关问题,我现在正在研究这个问题 Text not fitting into form fields (iTextSharp)
iText 7 尚未确定整个文本适合表单域的字体大小:
/// <summary>Adjust font in case autosize.</summary>
private float GetFontSize(PdfArray bBox, String value) {
if (this.fontSize == 0) {
//We do not support autosize with multiline.
if (IsMultiline() || bBox == null || value == null || String.IsNullOrEmpty(value)) {
return DEFAULT_FONT_SIZE;
}
else {
return Math.Max(ApproximateFontSizeToFitBBox(this.font, bBox.ToRectangle(), value), MIN_FONT_SIZE);
}
}
return this.fontSize;
}
(PdfFormField
辅助方法)
(如您所见,DEFAULT_FONT_SIZE
用作字体大小。此值定义为12
。)
特别是 iText 7 不支持您在表单字段上设置的 DoNotScroll 表单字段标志。
因此文本 "Field Office Wide - Fuelwood" 被分成两行。但是,由于字段小部件外观流包含沿字段小部件边框的剪辑路径,因此您只能看到第一行。此剪辑路径还可以让您仅看到展平视图中的第一行,但您可以将光标导航到第二行,select 那里的文本。
因此,要填充多行字段,请将它们的字体大小设置为可能会完成工作的值,即允许所有可能的输入适合小部件区域。或者,如果您无法控制 PDF 表单,请在填写之前删除 Multiline 标志,以使 iText 自动调整为单行。