根据用户在 C# 中输入的内容量定义 iTextSharp 文本字段的大小
Define the size of iTextSharp Textfield based on how much content a user inputs in C#
我找不到任何关于根据用户在 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;
不仅如此,在调整文本字段大小后,我希望能够在调整大小的文本字段下方添加静态文本。这被证明是非常棘手的,因为文本字段设置为绝对定位。
如有任何帮助,我们将不胜感激。
你看过这个问题的答案了吗:
在这个答案中,我们创建了一个如下所示的表单:
如您所见,表单字段的大小因我们期望的内容长度而异。
这是怎么做到的?这很简单:我们使用带有通用标记事件的 Chunk
对象来添加字段。请参阅 GenericFields 示例:
public class FieldChunk extends PdfPageEventHelper {
@Override
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
TextField field = new TextField(writer, rect, text);
try {
writer.addAnnotation(field.getTextField());
} catch (IOException ex) {
throw new ExceptionConverter(ex);
} catch (DocumentException ex) {
throw new ExceptionConverter(ex);
}
}
}
这段代码是用 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
中引入的空格数决定:
Chunk day = new Chunk(" ");
Chunk month = new Chunk(" ");
Chunk year = new Chunk(" ");
空格越多,范围越广
使用 Chunk
s 将使您更容易在每个字段下引入额外的静态内容。
对于多行文本字段,我建议使用 PdfPTable
和单元格事件。在示例 Create fields in a table on the official web site.
中演示了在 table 单元格中添加字段
我找不到任何关于根据用户在 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;
不仅如此,在调整文本字段大小后,我希望能够在调整大小的文本字段下方添加静态文本。这被证明是非常棘手的,因为文本字段设置为绝对定位。
如有任何帮助,我们将不胜感激。
你看过这个问题的答案了吗:
在这个答案中,我们创建了一个如下所示的表单:
如您所见,表单字段的大小因我们期望的内容长度而异。
这是怎么做到的?这很简单:我们使用带有通用标记事件的 Chunk
对象来添加字段。请参阅 GenericFields 示例:
public class FieldChunk extends PdfPageEventHelper {
@Override
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
TextField field = new TextField(writer, rect, text);
try {
writer.addAnnotation(field.getTextField());
} catch (IOException ex) {
throw new ExceptionConverter(ex);
} catch (DocumentException ex) {
throw new ExceptionConverter(ex);
}
}
}
这段代码是用 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
中引入的空格数决定:
Chunk day = new Chunk(" ");
Chunk month = new Chunk(" ");
Chunk year = new Chunk(" ");
空格越多,范围越广
使用 Chunk
s 将使您更容易在每个字段下引入额外的静态内容。
对于多行文本字段,我建议使用 PdfPTable
和单元格事件。在示例 Create fields in a table on the official web site.