ITextSharp - PdfPCell 中的文本字段

发布于 2024-08-29 17:28:41 字数 53 浏览 2 评论 0原文

我正在使用 iTextSharp 创建 PDF,如何将文本字段添加到 PdfPCell 中?

I'm using iTextSharp to create a PDF, how can I add a textField into PdfPCell?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青萝楚歌 2024-09-05 17:28:41

您不会真正向 PdfPCell 添加“文本字段”,而是创建一个 PdfPCell 并向其添加文本(或其他内容)。

mikesdotnetting.com 可能有最清晰的示例并且总是有iTextSharp 教程

You wouldn't really add a 'text field' to a PdfPCell, you'd create a PdfPCell and add text (or other stuff) to that.

mikesdotnetting.com might have the clearest example and there's always the iTextSharp tutorial.

倦话 2024-09-05 17:28:41

尝试一下。这对我有用。

Document doc = new Document(PageSize.LETTER, 18f, 18f, 18f, 18f);
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
doc.Open();

// Create your PDFPTable here....

TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(67, 585, 140, 800), "cellTextBox");
PdfPCell tbCell = new PdfPCell();
iTextSharp.text.pdf.events.FieldPositioningEvents events = new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, tf.GetTextField());
tbCell.CellEvent = events; 

myTable.AddCell(tbCell);

// More code...

我改编自这篇文章的代码。

编辑:

这是一个完整的工作控制台应用程序,它将文本框放入表格单元格中。我试图将代码保持在最低限度。

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace iTextSharpTextBoxInTableCell
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PDF with a TextBox in a table cell
            BaseFont bfHelvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false);
            Font helvetica12 = new Font(bfHelvetica, 12, Font.NORMAL, Color.BLACK);

            Document doc = new Document(PageSize.LETTER, 18f, 18f, 18f, 18f);
            FileStream fs = new FileStream("TextBoxInTableCell.pdf", FileMode.Create);
            PdfWriter writer = PdfWriter.GetInstance(doc, fs);

            doc.Open();
            PdfPTable myTable = new PdfPTable(1);
            myTable.TotalWidth = 568f;
            myTable.LockedWidth = true;
            myTable.HorizontalAlignment = 0;

            TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(67, 585, 140, 800), "cellTextBox");
            PdfPCell tbCell = new PdfPCell(new Phrase(" ", helvetica12));
            iTextSharp.text.pdf.events.FieldPositioningEvents events = 
                new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, tf.GetTextField());
            tbCell.CellEvent = events;

            myTable.AddCell(tbCell); 

            doc.Add(myTable);

            doc.Close();

            fs.Close();

            Console.WriteLine("End Of Program Execution");
            Console.ReadLine();
        }
    }
}

好机会

Give this a try. It works for me.

Document doc = new Document(PageSize.LETTER, 18f, 18f, 18f, 18f);
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
doc.Open();

// Create your PDFPTable here....

TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(67, 585, 140, 800), "cellTextBox");
PdfPCell tbCell = new PdfPCell();
iTextSharp.text.pdf.events.FieldPositioningEvents events = new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, tf.GetTextField());
tbCell.CellEvent = events; 

myTable.AddCell(tbCell);

// More code...

I adapted this code from this post.

Edit:

Here is a full working console application that puts a TextBox in a table cell. I tried to keep the code to a bare minimum.

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace iTextSharpTextBoxInTableCell
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PDF with a TextBox in a table cell
            BaseFont bfHelvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false);
            Font helvetica12 = new Font(bfHelvetica, 12, Font.NORMAL, Color.BLACK);

            Document doc = new Document(PageSize.LETTER, 18f, 18f, 18f, 18f);
            FileStream fs = new FileStream("TextBoxInTableCell.pdf", FileMode.Create);
            PdfWriter writer = PdfWriter.GetInstance(doc, fs);

            doc.Open();
            PdfPTable myTable = new PdfPTable(1);
            myTable.TotalWidth = 568f;
            myTable.LockedWidth = true;
            myTable.HorizontalAlignment = 0;

            TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(67, 585, 140, 800), "cellTextBox");
            PdfPCell tbCell = new PdfPCell(new Phrase(" ", helvetica12));
            iTextSharp.text.pdf.events.FieldPositioningEvents events = 
                new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, tf.GetTextField());
            tbCell.CellEvent = events;

            myTable.AddCell(tbCell); 

            doc.Add(myTable);

            doc.Close();

            fs.Close();

            Console.WriteLine("End Of Program Execution");
            Console.ReadLine();
        }
    }
}

Bon chance

傲世九天 2024-09-05 17:28:41

DaveB 的答案有效,但问题是您必须知道将文本字段放入的坐标(67, 585, 140, 800)。更常见的方法是创建表格单元格并向该单元格添加自定义事件。当表格生成调用 celllayout 事件时,它会向其传递单元格的尺寸和坐标,您可以使用它们来放置文本字段并调整其大小。

首先创建此调用,这是自定义事件

public class CustomCellLayout : IPdfPCellEvent
{
    private string fieldname;

    public CustomCellLayout(string name)
    {
        fieldname = name;
    }

    public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
    {
        PdfWriter writer = canvases[0].PdfWriter;

        // rectangle holds the dimensions and coordinates of the cell that was created
        // which you can then use to place the textfield in the correct location
        // and optionally fit the textfield to the size of the cell


        float textboxheight = 12f;
        // modify the rectangle so the textfield isn't the full height of the cell
        // in case the cell ends up being tall due to the table layout
        Rectangle rect = rectangle;
        rect.Bottom = rect.Top - textboxheight;

        TextField text = new TextField(writer, rect, fieldname);
        // set and options, font etc here

        PdfFormField field = text.GetTextField();

        writer.AddAnnotation(field);
    }
}

然后在创建表的代码中,您将使用如下事件:

PdfPCell cell = new PdfPCell()
    {
        CellEvent = new CustomCellLayout(fieldname)
        // set borders, or other cell options here
    };

如果您想要不同类型的文本字段,您可以创建其他自定义事件,也可以添加额外的属性到 CustomCellLayout 类,例如您使用类构造函数设置的“fontsize”或“multiline”,然后在 CellLayout 代码中检查以调整文本字段属性。

DaveB's answer works, but the problem is that you have to know the coordinates to place the textfield into, the (67, 585, 140, 800). The more normal method of doing this is to create the table cell and add a custom event to the cell. When the table generation calls the celllayout event it passes it the dimensions and coordinates of the cell which you can use to place and size the textfield.

First create this call, which is the custom event

public class CustomCellLayout : IPdfPCellEvent
{
    private string fieldname;

    public CustomCellLayout(string name)
    {
        fieldname = name;
    }

    public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
    {
        PdfWriter writer = canvases[0].PdfWriter;

        // rectangle holds the dimensions and coordinates of the cell that was created
        // which you can then use to place the textfield in the correct location
        // and optionally fit the textfield to the size of the cell


        float textboxheight = 12f;
        // modify the rectangle so the textfield isn't the full height of the cell
        // in case the cell ends up being tall due to the table layout
        Rectangle rect = rectangle;
        rect.Bottom = rect.Top - textboxheight;

        TextField text = new TextField(writer, rect, fieldname);
        // set and options, font etc here

        PdfFormField field = text.GetTextField();

        writer.AddAnnotation(field);
    }
}

Then in your code where you create the table you'll use the event like this:

PdfPCell cell = new PdfPCell()
    {
        CellEvent = new CustomCellLayout(fieldname)
        // set borders, or other cell options here
    };

If you want to different kinds of textfields you can either make additional custom events, or you could add extra properties to the CustomCellLayout class like "fontsize" or "multiline" which you'd set with the class constructor, and then check for in the CellLayout code to adjust the textfield properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文