【问题标题】:Define the size of iTextSharp Textfield based on how much content a user inputs in C#根据用户在 C# 中输入的内容量定义 iTextSharp 文本字段的大小
【发布时间】: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


【解决方案1】:

你有没有看过这个问题的答案:Add PdfPCell to Paragraph

在这个答案中,我们创建了一个如下所示的表单:

如您所见,表单字段的大小因我们期望的内容长度而异。

这是怎么做到的?这很简单:我们使用带有通用标签事件的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);
        }
    }
}

这个 sn-p 是用 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("            ");

空格越多,范围越广。

使用Chunks 可以让您更轻松地在每个字段下引入额外的静态内容。

对于多行文本字段,我建议使用PdfPTable 和单元格事件。在official web site 上的示例Create fields in a table 中演示了在表格单元格中添加字段。

【讨论】:

    猜你喜欢
    • 2015-04-08
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多