【问题标题】:How can I add multiline Textfields to a PDF form using iTextSharp?如何使用 iTextSharp 将多行文本字段添加到 PDF 表单?
【发布时间】:2015-05-21 17:23:06
【问题描述】:

详细说明here

如何使用 iTextSharp 文本字段(或其基本等效项),唯一的区别是它是多行的(覆盖表单上更多的垂直空间)?

这给了我想要的“文本框”:

public class DynamicTextbox : IPdfPCellEvent
{
    private string fieldname;

    public DynamicTextbox(string name)
    {
        fieldname = name;
    }

    public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
    {
        PdfWriter writer = canvases[0].PdfWriter;
        iTextSharp.text.pdf.TextField text = new iTextSharp.text.pdf.TextField(writer, rectangle, fieldname);
        //Microsoft.SharePoint.WebControls.TextField text = new TextField(writer, rectangle, fieldname);
        PdfFormField field = text.GetTextField();
        writer.AddAnnotation(field);
    }
}

...但是这个主题的派生,试图生成一个“更高”的版本,失败了:

public class DynamicMultilineTextbox : IPdfPCellEvent
{
    private string fieldname;

    public DynamicMultilineTextbox(string name)
    {
        fieldname = name;
    }

    public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
    {
        Rectangle wreckTangle = new Rectangle(30, 60); // changed from 300, 600
        PdfWriter writer = canvases[0].PdfWriter;
        iTextSharp.text.pdf.TextField text = new iTextSharp.text.pdf.TextField(writer, wreckTangle, fieldname);
        PdfFormField field = text.GetTextField();
        writer.AddAnnotation(field);
    }
}

失败在于,如果我使用 300、600 的矩形,它会产生一个巨大的 Blob,威胁要覆盖这个县和下一个县。如果我使用 30,60 它什么都不会显示 - 在任何一种情况下,我都希望“高文本框”只是运动水平线,如下所示:

我是不是找错树了?我想要完成的工作(向 PDF 文件中添加多线控件)如何实现?

更新

太棒了,如果我只是给文本框提供任何旧的文字字符串,它就可以工作:

代码如下:

PdfPCell cellNotesMultilineTextBox = new PdfPCell()
{
    CellEvent = new DynamicTextbox("multilineTextboxNotes"),
    Phrase = new Phrase("I will be darned like a sock", timesRoman9Font)
};
tblMultilineTextAreas.AddCell(cellNotesMultilineTextBox);

Phrase blankPhrase = new Phrase();
PdfPCell blankCell = new PdfPCell(blankPhrase);
blankCell.BorderWidth = 0;
tblMultilineTextAreas.AddCell(blankCell);

PdfPCell cellAccountCodesMultilineTextBox = new PdfPCell()
{
    CellEvent = new DynamicTextbox("multilineTextboxAccountCodes"),
    Phrase = new Phrase("I will be dammed like a reservoir", timesRoman9Font)
};
tblMultilineTextAreas.AddCell(cellAccountCodesMultilineTextBox);

Phrase blankPhrase2 = new Phrase();
PdfPCell blankCell2 = new PdfPCell(blankPhrase2);
blankCell2.BorderWidth = 0;
tblMultilineTextAreas.AddCell(blankCell2);

PdfPCell cell1099TaxReportableMultilineTextBox = new PdfPCell()
{
    CellEvent = new DynamicTextbox("multilineTextbox1099TaxReportable"),
    Phrase = new Phrase("I will be the uncle of a monkey", timesRoman9Font)
};
tblMultilineTextAreas.AddCell(cell1099TaxReportableMultilineTextBox);
doc.Add(tblMultilineTextAreas);

...但是,当然,那是行不通的。访问 WebPart 上 HtmlTextArea 控件的 InnerText,编码如下:

HtmlTextArea txtarNotes = null;
. . .
txtarNotes = new HtmlTextArea();
txtarNotes.Cols = 40;
txtarNotes.Rows = 6;

...导致“no-usable-height”控件,如第一个尖叫镜头所示。

并且在 WebPart 上引用文本框控件(而不是 HtmlTextArea 控件)也只会导致“水平线”。这是值为空的结果吗(如果字符串为空,则不为其分配空间)?

更新 2

显然是这样,因为这样做:

String s = txtarAccountCodes.InnerText;
if (String.IsNullOrEmpty(s))
{
    s = " ";
}
PdfPCell cellNotesMultilineTextBox = new PdfPCell()
{
    CellEvent = new DynamicTextbox("multilineTextboxNotes"),
    Phrase = new Phrase(s, timesRoman9Font)
};

...工作(文本框,虽然是空的,但以预期的大小/高度显示)。

更新 3

我突然意识到,也许我需要先增加 单元格 的大小(在担心单元格内“多行文本框”的大小之前)。所以我将代码更改为:

PdfPCell cellNotesMultilineTextBox = new PdfPCell()
{
    CellEvent = new DynamicTextbox("multilineTextboxNotes"),
    Phrase = new Phrase(notes, timesRoman9Font),
    MinimumHeight = 120,
    NoWrap = false,
    Rowspan = 40
};

(MinimumHeight、NoWrap 和 Rowspan 值是新的)。但是,这并不能解决我遇到的“不会换行”问题-区域足够大,但我只能输入一行文字;我输入的文字越多,文字越小:

有解决这个难题的方法吗?似乎我需要一种方法来告诉正在创建的“文本框”是“多行”(增加它的高度,或“行数”,或者使其可包装,就像上面的单元格本身一样)但似乎有“表面上”没有这样的能力 - 是否有什么东西暴露了这个功能?

【问题讨论】:

  • 我看到了你以前的帖子,但我很难找到问题。我对当前问题有同样的问题。如果问题是:如何创建多行字段,那么最近herehere 已经回答了这个问题。例如,参见itextpdf.com/sandbox/acroforms(例如MultiLineFieldReadOnlyField3、...)
  • 嗨,我也面临同样的问题。您最终找到解决方案了吗?
  • @user4582348 我不记得了;这个问题差不多三年了,我也快一年没做任何编程了。你捷克出布鲁诺的链接了吗?
  • 是的,似乎没有任何效果。没关系。仍在寻找:)

标签: c# pdf-generation itextsharp textfield multiline


【解决方案1】:

我有类似的要求,并通过以下代码实现。如果我误解了某些内容或您的要求是其他内容,请告诉我。

TextField tf = new TextField(stamper.Writer, new Rectangle(llx, lly, urx, ury), property.ControlId)
{
    Options = TextField.MULTILINE | TextField.READ_ONLY,
    FontSize = 11
};

来自 Adob​​e Acrobat 的屏幕截图:

【讨论】:

  • 谢谢;这是差不多 6 年前的事了,我不记得有什么,但也许它会帮助别人。
猜你喜欢
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-27
  • 1970-01-01
相关资源
最近更新 更多