C#iTextSharp CellEvent未正确应用于FormField

时间:2015-08-25 19:36:42

标签: c# .net itextsharp

我尝试使用iTextSharp在C#中创建FormField,以便用户可以电子方式签名/填写结果pdf。我从here获取了以下代码,但我无法让它解决我的具体问题。

private static void AddPrintedNameCell(PdfPTable table, PDFReport pdf, int index)
{
                                                         //also tried 0, 0 here
    TextField tf = new TextField(pdf.Writer, new Rectangle(35, 340, 300, 350), "PrintedNameCell_" + index);
    var root = PdfFormField.CreateEmpty(pdf.Writer);
    root.Name = "Root_" + index;

    PdfPCell cell = new PdfPCell(new Phrase("Printed Name: \r\n_______________________________________\r\n\r\n"));
    cell.HorizontalAlignment = Element.ALIGN_LEFT;
    cell.CellEvent = new ChildFieldEvent(root, tf.GetTextField(), 1);

    table.AddCell(cell);
    pdf.Writer.AddAnnotation(root);
}        

public class ChildFieldEvent : IPdfPCellEvent
    {
        /** A parent field to which a child field has to be added. */
        protected PdfFormField parent;
        /** The child field that has to be added */
        protected PdfFormField kid;
        /** The padding of the field inside the cell */
        protected float padding;

        /**
         * Creates a ChildFieldEvent.
         * @param parent the parent field
         * @param kid the child field
         * @param padding a padding
         */
        public ChildFieldEvent(PdfFormField parent, PdfFormField kid, float padding)
        {
            this.parent = parent;
            this.kid = kid;
            this.padding = padding;
        }

        /**
         * Add the child field to the parent, and sets the coordinates of the child field.
         */
        public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle rect, PdfContentByte[] cb)
        {                
            parent.AddKid(kid);
            kid.SetWidget(new iTextSharp.text.Rectangle(
                                                        rect.GetLeft(padding),
                                                        rect.GetBottom(padding),
                                                        rect.GetRight(padding),
                                                        rect.GetTop(padding)
                                                        ),
                                                        PdfAnnotation.HIGHLIGHT_INVERT
                                                        );
        }
    }

ChildFieldEvent触发时,rect.GetLeft(padding)rect.GetRight(padding)等都会返回0,所以我不能完全确定我所遗漏的内容。

0 个答案:

没有答案