调整iTextSharp中的表单域的大小 [英] Resizing a form field in iTextSharp
问题描述
我正在向表格单元格添加一个复选框字段,它会在整个单元格中展开。有没有办法可以控制元素的大小,因此它不占用整个单元格?
I am adding a checkbox field to a table cell and it expands across the whole cell. Is there a way I can control the size of the element so it does not take up the whole cell?
我发现其他人问同样的问题但它是从2006年开始所以我希望有些东西现在可用。
I found someone else asking the same question but it was from 2006 so I am hoping something is now available.
我写这篇文章的一个想法是在表格单元格中嵌套一个新表格并尝试做一些单元格对齐技巧以使复选框中心对齐但是我今天已经洗了个澡,不想再拿一个。
One idea I had while writing this was to nest a new table in the table cell and try do some cell alignment trickery to get the checkbox center aligned but I have already had a shower today and do not want to take another.
用代码编辑:
这个是我用于单元格事件的类:
This is the class I use for the cell event:
public class ChildFieldEvent : IPdfPCellEvent
{
protected PdfWriter writer;
protected PdfFormField parent;
protected string checkBoxName;
internal ChildFieldEvent(PdfWriter writer, PdfFormField parent, string CheckBoxName)
{
this.writer = writer;
this.parent = parent;
this.checkBoxName = CheckBoxName;
}
public void CellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] cb)
{
createCheckboxField(rect);
}
private void createCheckboxField(Rectangle rect)
{
RadioCheckField bt = new RadioCheckField(this.writer, rect, this.checkBoxName, "Yes");
bt.CheckType = RadioCheckField.TYPE_SQUARE;
bt.Checked = true;
this.parent.AddKid(bt.CheckField);
}
}
这是一项正在进行的工作,所以它有它现在有些不一致,但我认为Bruno的下面的示例基于父单元格矩形的复选框矩形是有意义的。如果有可能,我很好奇,因为上面提到的帖子发表的评论不是。
Its a bit of a work in progress so it has some inconsistencies in it right now but I think Bruno's example below of basing the checkbox's rectangle off the parent cell's rectangle makes sense. I was mainly curious if it was possible since the post I reference above made the comment that is wasn't.
推荐答案
我拿了一段时间来创建一个解释如何使用单元格事件的小例子。请参阅官方iText网站上的 CheckboxCell 。
I took some time to create you a small example that explains how to use cell events. See CheckboxCell on the official iText site.
想法是在 CheckboxCellEvent
中实现 PdfPCellEvent
接口。
在这种情况下,你得到一个 Rectangle
值: position
。你可以像这样计算这个矩形的中心:
In this event, you get a Rectangle
value: position
. You can calculate the center of this rectangle like this:
float x = (position.getLeft() + position.getRight()) / 2;
float y = (position.getTop() + position.getBottom()) / 2;
假设您要添加一个20 x 20的复选框,并且该复选框位于单元格内部,那么你需要创建一个这样的矩形:
Suppose that you want to add a check box that measures 20 by 20 and that is centered inside a cell, then you'd need to create a rectangle like this:
Rectangle rect = new Rectangle(x - 10, y - 10, x + 10, y + 10);
我认为你创建一个复选框没有任何问题(如果你这样做,请采取查看 CheckboxCell 示例。
I assume that you don't have any problem creating a check box (if you do, please take a look at the CheckboxCell example).
这篇关于调整iTextSharp中的表单域的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!