2014-03-05 42 views
1

我想添加一个复选框,使用C#的iTextSharp库。我跟着一些例子,并试图修改它们,因为它们展示了如何创建一组复选框。我如何创建一个?用iTextSharp添加一个复选框

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

写入器和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 }); 
     } 
    } 

回答

4

没关系,我找到了一个更好的办法。我的解决方案是将复选框放在一个表中,并以这种方式将其添加到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)); 
     } 
    } 
+0

这是一个绝对的耻辱,这不适用于iTextSharp v5.5.11 – CarneyCode