简体   繁体   中英

How can I add multiline Textfields to a PDF form using iTextSharp?

My travails in trying to use both "generic" textbox-type controls and multiline ones (that have greater height/are taller) are detailed here

What can I do to use an iTextSharp Textfield (or its basic equivalent), with the only difference being that it is multiline (covers more vertical space on the form)?

This gives me back the "textboxes" that I want:

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);
    }
}

...but this derivation on that theme, attempting to generate a "taller" version of that, fails:

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);
    }
}

The failure is in that, if I use a rectangle of 300, 600, it produces a monstrous Blob that threatens to cover this county and the next. If I use 30,60 it shows nothing - and in either case, where I expect the "tall textboxes" to be just sports horizontal lines, like so:

在此处输入图片说明

Am I barking up the wrong tree? How is what I'm trying to accomplish (add multline controls to a PDF file) possible?

UPDATE

Awedly enough, if I just give the textboxes any old literal string, it works:

在此处输入图片说明

The code for that is this:

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);

...but, of couse, that won't do. Accessing the InnerText of the HtmlTextArea controls on the WebPart, which are coded up like this:

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

...results in the "no-usable-height" controls as shown in the first scream shot.

And referencing a Textbox control on the WebPart (instead of a HtmlTextArea control) also results in the "horizontal lines" only. Is this a result of the value being empty (if the string is empty, no space is allotted for it)?

UPDATE 2

Apparently so, because doing this:

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

...works (the textboxes, albeit empty, display in their expected sizes/heights).

UPDATE 3

It dawned on me that maybe I needed to incrase the size of the cell first (prior to worrying about the size of the "multiline textbox" within the cell). SO I changed the code to this:

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

(the MinimumHeight, NoWrap, and Rowspan values are new). This, though, does not solve the "won't wrap" problem that I've got - the area is large enough, but I can only enter one line of text; the more text I enter, the smaller the text gets:

在此处输入图片说明

Is there a solution to this conundrum? It seems what I need is a way to tell the "Textbox" being created to "be multiline" (increase its height, or "rowcount", or make it wrappable, as in the case with the Cell itself above) but there seems to be no such capability "on the surface" - is there something beneath the covers that exposes this functionality?

I have a similar requirement and achieved with the following code. Let me know if I misunderstood something or your requirement is something else.

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

Screenshot from Adobe Acrobat:

Adobe Acrobat 的屏幕截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM