如何使用 itextsharp 在 C# ASP.NET 中的复选框旁边添加文本

How to add text next to checkbox in C# ASP.NET with itextsharp

我正在使用 itextsharp 库我想选中或取消选中 text.But 旁边的复选框我无法使其工作。

这是我的 class 构造器。

    public pdfCreator(string fileName)
    {
        //Create document  
        pdfDoc = new Document(PageSize.A4, 25, 25, 25, 15);
        //Create a PDF file in specific path  
        PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream(System.Web.HttpContext.Current.Server.MapPath(fileName+".pdf"), FileMode.Create));
        pdfDoc.Open();
    }

这是我的方法。

    public void chkBoxesCreator()
    {
        string FONT = "c:/windows/fonts/WINGDING.TTF";
        string checkBox = "\u00fe";
        string uncheckBox = "o";

        BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font f = new Font(bf, 12);
        Paragraph p = new Paragraph(checkBox, f);
        Paragraph p2 = new Paragraph(uncheckBox, f);
        pdfDoc.Add(p);
        pdfDoc.Add(p2);
    }

我想要复选框旁边的文本,但是是蓝色的。我怎样才能在我的复选框旁边得到文本。非常感谢您的帮助。

您应该使用 PdfAppearance 来设置复选框(或单选按钮、按钮等)

这是一个完整的例子

        public static void chkBoxesCreator()
        {  
           String[] texts = { "one", "two", "three" };

            using (var pdfDoc = new Document(PageSize.A4))
            using (var output = new FileStream(fileLoc, FileMode.Create))
                using (var writer = PdfWriter.GetInstance(pdfDoc, output))

            {

                {
                    pdfDoc.Open();                     

                    PdfContentByte cb = writer.DirectContent;
                    Rectangle _rect;
                    PdfFormField _Field1;

                    PdfAppearance[] checkBoxes = new PdfAppearance[2];
                    //set first  checkBox style
                    checkBoxes[0] = cb.CreateAppearance(20, 20);
                    checkBoxes[0].Rectangle(1, 1, 18, 18);
                    checkBoxes[0].Stroke();

                    //set second  checkBox style
                    checkBoxes[1] = cb.CreateAppearance(20, 20);                  
                    checkBoxes[1].Rectangle(1, 1, 18, 18);
                    checkBoxes[1].FillStroke();

                    RadioCheckField _checkbox1;

                    for (int i = 0; i < texts.Length; i++)
                    {
                        _rect = new Rectangle(180, 806 - i * 40, 200, 788 - i * 40); //can be any location
                        _checkbox1 = new RadioCheckField(writer, _rect, texts[i], "on");
                        _Field1 = _checkbox1.CheckField;
                        _Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", checkBoxes[0]);
                        _Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", checkBoxes[1]);
                        writer.AddAnnotation(_Field1);

                        ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(texts[i], new Font(Font.FontFamily.HELVETICA, 18)), 210, 790 - i * 40, 0);
                    }

                    cb = writer.DirectContent;
                    pdfDoc.Close();
                }
            }
        }

PS 我遇到的唯一问题是由于某些原因我无法使用您的字体我变得乱码

编辑

你甚至可以改变你的复选框填充颜色(你可以做任何更疯狂的事情,比如在复选框被选中时创建一个十字)

  checkBoxes[1].SetRGBColorFill(255, 128, 128); //change fill color

输出是(SetRGBColorFill