单选按钮未在Acrobat阅读器中显示

时间:2014-11-12 15:23:23

标签: c# pdf itextsharp

我正在用itextsharp创建一个带有可填写形式的pdf。 除了acrobat reader XI之外,所有的东西都在狐狸阅读器中使用peachy,内联pdf的铬。

问题是,我正在创建一个带有单元格的表格,其中有一个带有几列的表格。 每列都有一个cellevent来创建一个复选框(radiobutton-special)

这实际上就像一个radiobutton组,但遗憾的是不在acrobat-reader中,我只看到1个复选框

主类,调用测试并将pdf写入文件系统:

class Program
    {
        static void Main(string[] args)
        {
            byte[] arrBytes;

            TestSimple ts = new TestSimple();
            ts.CreatePDF(out arrBytes);

            File.WriteAllBytes("C:\\Kaas\\Foo.pdf", arrBytes); 
        }
    }

创建文档并添加复选框的TestClass

public class TestSimple
{
    public TestSimple() { }

    public void CreatePDF(out byte[] ssPdf)
    {
        ssPdf = new byte[] { };
        using (MemoryStream ms = new MemoryStream())
        {
            // step 1
            // document will be of A4 with margins
            Document document = new Document(PageSize.A4, 25, 25, 50, 50);
            // step 2
            PdfWriter writer = PdfWriter.GetInstance(document, ms);
            document.Open();
            document.Add(CreateSimpleTable());
            document.Close();
            ssPdf = ms.ToArray();

        }
    }

    private PdfPTable CreateSimpleTable()
    {
        PdfPCell cell;
        PdfPTable table = new PdfPTable(2);
        table.WidthPercentage = 100;
        for (int row = 1; row <= 10; row++)
        {
            cell = new PdfPCell(new Phrase("Row " + row));
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Column" + row));
            cell.PaddingLeft = 25f;
            cell.CellEvent = new RadioCheckBox("radiogroup", "check" + row);
            //checkbox
            table.AddCell(cell);
        }
        return table;
    }
}

CellEvent类,用于在单元格中创建复选框。

 public class RadioCheckBox : IPdfPCellEvent
    {
        private string fieldName;
        private string onValue;

        private int boxSize = 12;
        private BaseColor backgroundColor = new BaseColor(255, 255, 255);
        private BaseColor borderColor = new BaseColor(0, 0, 00);

        public RadioCheckBox(string fieldName, string onValue)
        {
            this.fieldName = fieldName;
            this.onValue = onValue;
        }

        public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
        {
            PdfWriter writer = canvases[0].PdfWriter;

            PdfAppearance[] onOff = new PdfAppearance[2];
            onOff[0] = canvases[0].CreateAppearance(boxSize, boxSize);
            onOff[0].SetColorFill(backgroundColor);
            onOff[0].SetColorStroke(borderColor);
            onOff[0].Rectangle(1, 1, boxSize - 1, boxSize - 1);
            onOff[0].FillStroke();
            onOff[1] = canvases[0].CreateAppearance(boxSize, boxSize);
            onOff[1].SetColorFill(backgroundColor);
            onOff[1].SetColorStroke(borderColor);
            onOff[1].Rectangle(1, 1, boxSize - 1, boxSize - 1);
            onOff[1].FillStroke();
            onOff[1].SetColorStroke(new BaseColor(0, 0, 0));
            onOff[1].MoveTo(1, 1);
            onOff[1].LineTo(boxSize, boxSize);
            onOff[1].MoveTo(1, boxSize);
            onOff[1].LineTo(boxSize, 1);
            onOff[1].Stroke();

            //make it small rectangle
            Rectangle CheckboxRectangle = new Rectangle(rectangle);
            CheckboxRectangle.Right = CheckboxRectangle.Left + boxSize;
            CheckboxRectangle.Top = CheckboxRectangle.Bottom + boxSize;

            RadioCheckField bt = new RadioCheckField(writer, CheckboxRectangle, null, "Yes");
            bt.CheckType = RadioCheckField.TYPE_CROSS;
            bt.Checked = false;
            bt.FontSize = 9;
            bt.Alignment = Element.ALIGN_RIGHT;

            PdfFormField field = bt.CheckField;
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "No", onOff[0]);
            field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", onOff[1]);

            PdfFormField pff = PdfFormField.CreateRadioButton(writer, false);
            pff.FieldName = fieldName;
            pff.AddKid(field);
            writer.AddAnnotation(pff);
        }
    }

在创建单元格时,这段代码将在for循环中被调用几次 我错过了什么或做错了所以它在acrobat阅读器中不起作用,但它在foxit,chrome-inline阅读器中有效吗?

示例pdf:http://www.filedropper.com/foo

0 个答案:

没有答案