使用iTextSharp添加一个复选框

我正在尝试使用iTextSharp库为C#添加一个复选框。 我已经按照一些示例尝试修改它们,因为它们显示了如何创建一组复选框。 我如何创建一个?

private static void WriteFooter(Document doc, int page, int maxPages, bool lastPage, PdfWriter writer, PdfAppearance[] checkBoxAppearance) { if(!lastPage) { doc.Add(new Paragraph("\nMA-2028.1-F2(Portrait) Page " + page + " of " + maxPages, Fonts.ExtraSmall)); doc.NewPage(); } else { PdfContentByte cb = writer.DirectContent; PdfFormField _checkGroup = PdfFormField.CreateRadioButton(writer, true); RadioCheckField _radioG; PdfFormField _radioField1; _radioG = new RadioCheckField(writer, new Rectangle(20, 20), null, "Yes"); _radioG.CheckType = RadioCheckField.TYPE_CHECK; _radioField1 = _radioG.RadioField; _checkGroup.AddKid(_radioField1); ColumnText.ShowTextAligned(cb, Element.ALIGN_RIGHT, new Phrase("No Users Require Deletion"), 20, 20, 0); writer.AddAnnotation(_checkGroup); cb = writer.DirectContent; doc.Add( new Paragraph( "Operations Management:\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER }); doc.Add( new Paragraph( "Automation Manager or Designee: \n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER }); } } 

writer和checkBoxAppearance从另一个函数传入。 以前在我的代码中,我正在创建一个带复选框的表,但我需要在此表外添加一个。 我按照http://simpledotnetsolutions.wordpress.com/2012/11/01/itextsharp-creating-form-fields/中的代码进行操作

checkBoxAppearance是一个定义复选框行为的数组。

并试图修改它只有一个复选框。 我也试图让文本显示在它旁边。

编辑:我也在尝试这个:

 private static void WriteFooter(Document doc, int page, int maxPages, bool lastPage, PdfWriter writer, PdfFormField checkboxField, PdfAppearance[] checkbox, PdfContentByte cb) { if(!lastPage) { doc.Add(new Paragraph("\nMA-2028.1-F2(Portrait) Page " + page + " of " + maxPages, Fonts.ExtraSmall)); doc.NewPage(); } else { PdfFormField field; RadioCheckField checkBox; for (int i = 0; i < 1; i++ ) { checkBox = new RadioCheckField(writer, new Rectangle(20, 20), "noDelete", "Yes"); field = checkBox.CheckField; field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", checkbox[0]); field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", checkbox[1]); writer.AddAnnotation(field); //ColumnText.ShowTextAligned(cb, Element.ALIGN_RIGHT, new Phrase("No Users Need Deletion"), 210, 790, 0); } doc.Add( new Paragraph( "Operations Management:\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER }); doc.Add( new Paragraph( "Automation Manager or Designee:\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER }); } } 

没关系,我找到了更好的方法。 我的解决方案是将复选框放在表格中,然后将其添加到PDF中。 它看起来比我老板原本想要的方式更清洁。

 private static void WriteFooter(Document doc, int page, int maxPages, bool lastPage, PdfWriter writer, PdfFormField checkboxField, PdfAppearance[] checkbox, PdfContentByte cb) { if(!lastPage) { doc.Add(new Paragraph("\nMA-2028.1-F2(Portrait) Page " + page + " of " + maxPages, Fonts.ExtraSmall)); doc.NewPage(); } else { PdfFormField footerField = PdfFormField.CreateEmpty(writer); PdfPTable footerTbl = new PdfPTable(2); float[] footerWidths = new float[] { 1f, 4f }; PdfPCell noDeleteCell = new PdfPCell(); PdfPCell noDeleteText = new PdfPCell(new Paragraph("No Users Require Deletion", Fonts.Small)); RadioCheckField fCell = new RadioCheckField(writer, new Rectangle(20, 20), "NoDeletion", "Yes"); fCell.CheckType = RadioCheckField.TYPE_CROSS; PdfFormField footerCheck = null; footerCheck = fCell.CheckField; footerCheck.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", checkbox[0]); footerCheck.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", checkbox[1]); noDeleteCell.CellEvent = new ChildFieldEvent(footerField, footerCheck, 1, 20, 20); footerField.FieldName = "no_delete_table"; footerTbl.SetWidths(footerWidths); footerTbl.AddCell(noDeleteCell); footerTbl.AddCell(noDeleteText); doc.Add(footerTbl); writer.AddAnnotation(footerField); doc.Add( new Paragraph( "\n\nOperations Management: _______________________________________________ Date: ___________\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER }); doc.Add( new Paragraph( "Automation Manager or Designee: _______________________________________________ Date: ___________\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER }); doc.Add(new Paragraph("\nMA-2028.1-F2(Portrait) Page " + page + " of " + maxPages, Fonts.ExtraSmall)); } }