iTextSharp:在表格单元格上的文本字段上设置文本会导致文本太宽并且被压扁 [英] iTextSharp: Setting text on textfield that is on a table cell results in text being too wide and squashed

查看:212
本文介绍了iTextSharp:在表格单元格上的文本字段上设置文本会导致文本太宽并且被压扁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这可能是一个错误,但如果有人能提供帮助,我会很感激。我目前还有另一个问题涉及类似的问题,但我认为这个问题可以更好地说明问题,更简单。话虽如此,我不想删除旧的,以防它增加我的等待时间。我屈服于mod来决定哪个问题更好。

I think this might be a bug, but if anyone can help I'd appreciate it. I currently have another question open that deals with a similar issue, but I think this question better exemplifies the problem, and more simply too. That being said I don't want to delete the old one in case it increases my wait time. I yield to the mods to decide which question is better.

这是一个示例应用程序,它创建一个pdf,然后是一个表。它向表中添加一个单元格,然后将fieldpositioningevent绑定到单元格事件。

Here's a sample application that creates a pdf, then a table. It adds a cell to the table and then ties a fieldpositioningevent to the cell event.

using System;
using System.Diagnostics;
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, BaseColor.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");
            tf.Text = "test";
            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();
            Process.Start("TextBoxInTableCell.pdf");
            Console.WriteLine("End Of Program Execution");
            Console.ReadLine();
        }
    }
}

这是该字段的样子当它生成时:

Here's what this field looks like when it's generated:

正如你所看到的,文本被压扁了。我已经发布了生成的pdf 此处

As you can see, the text is squashed. I've published the generated pdf here.

推荐答案

我肯定会看到你所看到的,正如@mkl在你的另一篇文章中所说,问题归结为外观的 BBOX 条目未设置为与字段相同的大小。我无法在野外找到太多 FieldPositioningEvents 的例子,而且确实存在的例子在大多数情况下似乎是彼此的复制和粘贴。

I'm definitely seeing what you're seeing and as @mkl said in your other post, the problem comes down to the appearance's BBOX entry not being set to the same size as the field. I can't really find too many examples of FieldPositioningEvents in the wild and the ones that do exist appear to be copy-and-paste's of each other for the most part.

无论如何,如果你读了 FieldPositioningEvents 你会发现它可以用于这两个页面事件以及细胞事件让我觉得它可能用于更广泛的目的,但这只是我的一个猜测。

Anyway, if you read the actual code for FieldPositioningEvents you'll see that it can be used for both page events as well as cell events which makes me think it was intended for broader purposes possibly, but that's just a guess on my part.

一个解决方案就是自己写一个 IPdfPCellEvent 的子类。下面是一个示例,它遵循 FieldPositioningEvents 提供的示例,但它特定于 TextFields ,因为我们感兴趣设置 / BBOX 条目。它有两个构造函数,一个与 FieldPositioningEvents 非常相似,它接受 PdfWriter TextField 和一个只占用 TextFields 的最常设置属性,并实际为您创建它。 CellLayout 是接口合约的一部分,实际上会找出应该在哪里绘制注释。

One solution is to just write your own subclass of IPdfPCellEvent. Below is an example of that that follows the example provided by FieldPositioningEvents however it is specific to TextFields since we're interested in setting the /BBOX entry. It has two constructors, one that works very similar to FieldPositioningEvents that takes a PdfWriter and a TextField and one that just takes the most commonly set properties of a TextFields and actually creates it for you. The CellLayout is part of the interface contract and actually figures out where the annotation should be drawn.

public class SingleCellFieldPositioningEvent : IPdfPCellEvent {

    public TextField Field { get; set; }
    public PdfWriter Writer { get; set; }
    public float Padding { get; set; }

    public SingleCellFieldPositioningEvent(PdfWriter writer, TextField field) {
        this.Field = field;
        this.Writer = writer;
    }

    public SingleCellFieldPositioningEvent(PdfWriter writer, string fieldName, string text = "", BaseFont font = null, float fontSize = 14 ) {
        //The rectangle gets changed later so it doesn't matter what we use
        var rect = new iTextSharp.text.Rectangle(1, 1);

        //Create the field and set various properties
        this.Field = new TextField(writer, rect, fieldName);
        this.Field.Text = text;
        if (null == font) {
            font = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
        }
        this.Field.Font = font;
        this.Field.FontSize = fontSize;

        this.Writer = writer;
    }

    public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle rect, PdfContentByte[] canvases) {
        //Create the field's rectangle based on the current cell and requested padded
        var newRect = new PdfRectangle(rect.GetLeft(Padding), rect.GetBottom(Padding), rect.GetRight(Padding), rect.GetTop(Padding));

        //Set the appearance's rectangle to the same as the box
        Field.Box = newRect.Rectangle;

        //Get the raw field
        var tf = this.Field.GetTextField();

        //Change the field's rectangle
        tf.Put(PdfName.RECT, newRect);

        //Add the annotation to the writer
        Writer.AddAnnotation(tf);
    }
}

您可以通过两种不同的方式使用它。手动创建一个字段并设置各种属性:

You can use this in two different ways. Either manually create a field and set various properties:

//The rectangle is actually changed in the cell event so it doesn't matter what we use
TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(1, 1), "cellTextBox");
tf.Text = "test";
tf.Font = bfHelvetica;
tf.FontSize = 14;
PdfPCell tbCell = new PdfPCell(new Phrase(" ", helvetica12));
tbCell.CellEvent = new SingleCellFieldPositioningEvent(writer, tf);

或者只是传递房产:

PdfPCell tbCell = new PdfPCell(new Phrase(" ", helvetica12));
tbCell.CellEvent = new SingleCellFieldPositioningEvent(writer, "cellTextBox", "test", bfHelvetica, 14);
myTable.AddCell(tbCell);

这篇关于iTextSharp:在表格单元格上的文本字段上设置文本会导致文本太宽并且被压扁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆