【问题标题】:iTextSharp filling an existing pdf but the multiline text field won't wrapiTextSharp 填充现有的 pdf,但多行文本字段不会换行
【发布时间】:2019-09-05 19:46:44
【问题描述】:

我有一个我在 Adob​​e Acrobat Pro 2017 中创建/编辑的 PDF。目标是在我们的网站上动态填写它。它是成功的,除了一个部分;其中一个表单字段不会按预期换行。

    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 Reader 2017 手动输入的 PDF 表单输出

这是代码的输出

这是一个希望匿名的文件版本 https://github.com/Wyseguys/wyseguys.github.io/blob/Wyseguys-patch-1/form.pdf(这不是什么大秘密,只是保持安静)

另外,这可能是一个相关的问题,我现在正在探索 Text not fitting into form fields (iTextSharp)

【问题讨论】:

  • 请分享有问题的 PDF,以便重现问题。或者,如果它是机密的,则创建另一个只包含与问题 PDF 中相同的问题字段。
  • @mkl 谢谢。我添加了一个链接。
  • 我会在下周初调查。

标签: c# pdf itext7


【解决方案1】:

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”被分成两行。但是,由于字段小部件外观流包含沿字段小部件边框的剪辑路径,因此您只能看到第一行。此剪辑路径还允许您仅看到展平视图中的第一行,但您可以将光标导航到第二行并选择那里的文本。

因此,要填充多行字段,请将它们的字体大小设置为可能会完成这项工作的值,即允许所有可能的输入适合小部件区域。或者,如果您无法控制 PDF 表单,请在填写前删除 Multiline 标志,以使 iText 自动调整为单行。

【讨论】:

  • 我讨厌答案这么短。这钉住了它。我在 Adob​​e 中编辑了表单,并取消了这些字段的“多行”框。字体(我从未设置过大小)在展平时正确调整。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
  • 2016-09-12
  • 2018-11-15
  • 2020-06-10
  • 1970-01-01
相关资源
最近更新 更多