Resizing a form field in iTextSharp

我们两清 提交于 2019-12-25 16:43:49

问题


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?

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.

Edit with code:

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);
    }
}

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.


回答1:


I took some time to create you a small example that explains how to use cell events. See CheckboxCell on the official iText site.

The idea is to implement the PdfPCellEvent interface the way it's done in CheckboxCellEvent.

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;

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);

I assume that you don't have any problem creating a check box (if you do, please take a look at the CheckboxCell example).



来源:https://stackoverflow.com/questions/21342034/resizing-a-form-field-in-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!