使用iTextSharp在表格单元格内的RadioButtonGroup

时间:2015-11-11 10:34:41

标签: c# .net pdf-generation itextsharp

我一直在尝试使用iTextSharp在表格单元格中填充 RadioButtonGroup

在我的代码中,我试图在表格中生成性别 RadioButtonGroup。

的Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text;

namespace RadioButton
{
    public class Program
    {
       static void Main(string[] args)
       {
          Document document = new Document(PageSize.A4, 50, 50, 25, 25);

          FileStream pdfFileStream = new FileStream("D:\\Output1.pdf", FileMode.Create);

        using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, pdfFileStream))
        {

            document.Open();
            PdfContentByte cb = pdfWriter.DirectContent;


            ///// ---- Table creation in PDF using iTextSharp -----///

            PdfPTable documentTable = new PdfPTable(1);

            documentTable.HorizontalAlignment = 0;//0=Left, 1=Centre, 2=Right
            documentTable.TotalWidth = 500;
            documentTable.LockedWidth = true;
            documentTable.DefaultCell.Border = Rectangle.TOP_BORDER;

            PdfPTable genderFieldTable = new PdfPTable(1);
            genderFieldTable.HorizontalAlignment = 0;//0=Left, 1=Centre, 2=Right
            genderFieldTable.DefaultCell.FixedHeight = 500f;
            genderFieldTable.TotalWidth = 15;
            genderFieldTable.LockedWidth = true;
            genderFieldTable.DefaultCell.Border = 0;

            PdfPCell genderFieldCell;

            PdfFormField _radioGroup = PdfFormField.CreateRadioButton(pdfWriter, true);
            _radioGroup.FieldName = "Gender";
            string[] genders = { "Male", "Female" };

            for (int i = 0; i < 1; i++)
            {
                genderFieldCell = new PdfPCell();
                genderFieldCell.Border = 0;
                genderFieldCell.CellEvent = new MyCellField(pdfWriter, _radioGroup, "CheckBox");
                genderFieldTable.AddCell(genderFieldCell);
                documentTable.AddCell(genderFieldTable);
            }

                document.Add(documentTable);
                document.Close();

                Console.WriteLine("Completed");
                Console.ReadLine();
         }
     }
  }
}

我编写了一个自定义类来创建RadioCheckField

MyCellField.cs:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using iTextSharp;
 using iTextSharp.text.pdf;
 using System.IO;
 using iTextSharp.text;

 namespace RadioButton
 {
    public class MyCellField : IPdfPCellEvent
    {
        protected PdfWriter writer;
        protected PdfFormField radiogroup;
        protected String value;
        public MyCellField(PdfWriter writer, PdfFormField radiogroup, String value)// 
        {
            this.writer = writer;
            this.radiogroup = radiogroup;
            this.value = value;
        }

        public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
        {
            float x = (position.Left + position.Right) / 2;
            float y = (position.Top + position.Bottom) / 2;
            // define the position of a check box that measures 20 by 20
            Rectangle rect = new Rectangle(x - 7, y - 7, x + 7, y + 7);
            // define the check box
            RadioCheckField radioButton = new RadioCheckField(
                writer, rect, "Gender", value);

            try
            {
                radiogroup.AddKid(radioButton.RadioField);
                // writer.AddAnnotation(radioButton.RadioField);
            }
            catch (IOException)
            {
                throw;
            }
            catch (DocumentException)
            {
                throw;
            }
        }
    } 
}

执行此操作时,我没有获得 RadioButtonGroup ,而是获得了空白的PDF页面。

我的代码有什么问题?请帮助我得到这个。

用于Checkbox的相同MyCellField.cs类( 我只是注释掉了RadioGroupButton生成代码并编写了用于生成复选框的代码 )。 Checkbox工作正常。

我生成复选框的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text;

namespace HTMLtoPDF
{
    public class MyCellField : IPdfPCellEvent
    {
        protected PdfWriter writer;
        protected PdfFormField radiogroup;
        protected String value;
        protected int checkType;
        public MyCellField(PdfWriter writer, PdfFormField radiogroup,   String value)// 
        {
            this.writer = writer;
            this.radiogroup = radiogroup;
            this.value = value;
        }

        public void CellLayout(PdfPCell cell, Rectangle position,  PdfContentByte[] canvases)

        {
            /* Generating Checkbox fields */

            float x = (position.Left + position.Right) / 2;
            float y = (position.Top + position.Bottom) / 2;

             //define the position of a check box that measures 20 by 20
            Rectangle rect = new Rectangle(x - 5, y - 5, x + 5, y + 5);

            //define the check box
            RadioCheckField checkbox = new RadioCheckField(
                writer, rect, value, "Yes");

            checkbox.CheckType = 1;
            checkbox.FieldName = value;

            RadioCheckField radio = new RadioCheckField(writer, rect, "Gender", value);

            try
            {
                writer.AddAnnotation(checkbox.CheckField);
            }
            catch (IOException)
            {
                throw;
            }
            catch (DocumentException)
            {
                throw;
            }

        /* Generating RadioGroup fields */

        //PdfWriter writer = canvases[0].PdfWriter;
        //Rectangle rect = new Rectangle(200, 806, 170, 890);

        //float x = (position.Left + position.Right) / 2;
        //float y = (position.Top + position.Bottom) / 2;
        //// define the position of a check box that measures 20 by 20
        //Rectangle rect = new Rectangle(x - 7, y - 7, x + 7, y + 7);
        //// define the check box
        //RadioCheckField radioButton = new RadioCheckField(
        //        writer, rect, "Gender", value);

        ////RadioCheckField radio = new RadioCheckField(writer, rect, "Gender", value);
        //try
        //{
        //    radiogroup.AddKid(radioButton.RadioField);
        //    writer.AddAnnotation(radioButton.RadioField);
        //}
        //catch (IOException)
        //{
        //    throw;
        //}
        //catch (DocumentException)
        //{
        //    throw;
        //}

    }
}

}

请告诉我 RadioButton 组的生成有什么问题。

0 个答案:

没有答案