【发布时间】:2016-03-04 04:10:47
【问题描述】:
我找不到任何关于根据用户在 C# 中的输入内容调整或重新调整 iTextSharp 的文本字段的溢出帖子。我希望有人可以在这里帮助我,因为这对我来说相当困难。
这是我想要完成的。根据用户的文本输入量,我希望能够重新调整 iTextSharp 的文本字段大小,而无需在文本框末尾包装文本内容或调整字体大小。
到目前为止,这是我用于文本框的代码块:
using iTextSharp.text.pdf;
string bodyText = "This is the text that the user inputs. I want the textfield to resize ";
bodyText += "based on how much text content there is here. I do not want the ";
bodyText += "text to wrap around or shrink text size at the end of the textfield.";
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strPDFpath, FileMode.Create));
PdfContentByte cb = writer.DirectContent;
TextField _subject = new TextField(writer, new Rectangle(103f, 485f, 503, 499f), "tfSubject");
//I want this Rectangle size to be readjustable based on the text content but
//I'm not sure how to do that.
_subject.FontSize = 10f;
_subject.Alignment = Element.ALIGN_LEFT;
_subject.Text = bodyText;
writer.AddAnnotation(_subject.GetTextField());
cb = writer.DirectContent;
不仅如此,在调整文本字段大小后,我希望能够在调整大小的文本字段正下方添加静态文本。这被证明是相当棘手的,因为文本字段设置为绝对定位。
任何帮助将不胜感激。
【问题讨论】:
-
您的问题不清楚。在标题中,您声称要重新调整文本字段的大小,这意味着您有一个包含现有文本字段的现有 PDF。在问题的正文中,您创建一个文本字段,就像您从头开始创建一个文档一样。你想要什么?您要重新调整现有文本字段的大小还是定义新文本字段的大小?
-
我想根据用户将输入的字符串内容来定义新文本字段的大小。
-
到底要measure a string based on a given font and size, right?该链接应该让您最容易到达那里,您只需要考虑依赖于应用程序的小部件的填充,但您可以捏造一些可能在大多数地方都有效的数字。
-
或者,如果您在谈论多行文本字段,您可以使用
ColumnText创建一个固定宽度的矩形,添加内容,然后询问该列的高度是多少“消费”。
标签: c# pdf itextsharp textfield autoresize