Skip to main content
 首页 » 编程设计

c#之使用 iTextSharp 添加一个复选框

2024年02月13日11kuangbin

我正在尝试使用适用于 C# 的 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 }); 
        } 
    } 

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