C# 根据用户在C中输入的内容量定义iTextSharp文本字段的大小#
我找不到任何关于根据用户在C#中的输入内容调整或重新调整iTextSharp文本字段大小的溢出帖子。我希望如果有人能在这里帮助我,因为这对我来说相当困难 这就是我想要实现的目标。根据用户输入的文本量,我希望能够重新调整iTextSharp的文本字段大小,而无需包装文本内容或调整文本框末尾的字体大小 以下是我迄今为止为文本框编写的代码块:C# 根据用户在C中输入的内容量定义iTextSharp文本字段的大小#,c#,pdf,itextsharp,textfield,autoresize,C#,Pdf,Itextsharp,Textfield,Autoresize,我找不到任何关于根据用户在C#中的输入内容调整或重新调整iTextSharp文本字段大小的溢出帖子。我希望如果有人能在这里帮助我,因为这对我来说相当困难 这就是我想要实现的目标。根据用户输入的文本量,我希望能够重新调整iTextSharp的文本字段大小,而无需包装文本内容或调整文本框末尾的字体大小 以下是我迄今为止为文本框编写的代码块: using iTextSharp.text.pdf; string bodyText = "This is the text that the user i
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;
不仅如此,在调整textfield的大小后,我希望能够在调整大小的textfield下添加静态文本。这证明是相当棘手的,因为textfield设置为绝对位置
非常感谢您的帮助。您是否查看了此问题的答案: 在这个答案中,我们创建了一个如下所示的表单:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
writer.setPageEvent(new FieldChunk());
document.open();
Paragraph p = new Paragraph();
p.add("The Effective Date is ");
Chunk day = new Chunk(" ");
day.setGenericTag("day");
p.add(day);
p.add(" day of ");
Chunk month = new Chunk(" ");
month.setGenericTag("month");
p.add(month);
p.add(", ");
Chunk year = new Chunk(" ");
year.setGenericTag("year");
p.add(year);
p.add(" that this will begin.");
document.add(p);
document.close();
}
正如您所看到的,表单字段的大小取决于我们期望的内容的长度
这是怎么做到的?这很简单:我们使用带有通用标记事件的Chunk
对象来添加字段。请参见示例:
这个代码段是用Java编写的,但将其移植到C#应该相当容易(如果您不懂Java,就假装我编写了伪代码)
我们可以像这样使用页面事件:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
writer.setPageEvent(new FieldChunk());
document.open();
Paragraph p = new Paragraph();
p.add("The Effective Date is ");
Chunk day = new Chunk(" ");
day.setGenericTag("day");
p.add(day);
p.add(" day of ");
Chunk month = new Chunk(" ");
month.setGenericTag("month");
p.add(month);
p.add(", ");
Chunk year = new Chunk(" ");
year.setGenericTag("year");
p.add(year);
p.add(" that this will begin.");
document.add(p);
document.close();
}
如您所见,字段的长度由我们在块中引入的空格数决定:
Chunk day = new Chunk(" ");
Chunk month = new Chunk(" ");
Chunk year = new Chunk(" ");
空间越多,场地越宽
使用Chunk
s将使您更容易在每个字段下引入额外的静态内容
对于多行文本字段,我建议使用PdfPTable
和单元格事件。在表格单元格中添加字段在上的示例中演示。您的问题不清楚。在标题中,您声称要调整文本字段的大小,这意味着您有一个包含现有文本字段的现有PDF。在问题主体中,您创建一个文本字段,就像从头开始创建文档一样。你想要什么?是否要调整现有文本字段的大小或定义新文本字段的大小?我要根据用户将输入的字符串内容定义新文本字段的大小。最终是否要?这个链接应该能让你达到最好的效果,你只需要考虑小部件的填充,这取决于应用程序,但你可以捏造一些可能在大多数地方都适用的数字。或者,如果你说的是多行文本字段,你可以使用ColumnText
创建一个固定宽度的矩形,添加内容,然后询问专栏“消耗”了多少高度。