简体   繁体   English

iTextSharp:在带有文本的表格单元格内添加复选框

[英]iTextSharp: Add checkbox inside table cell with text

I want to add two checboxes inside an itexsharp cell on a table.我想在表格的 itexsharp 单元格内添加两个复选框。 But I don't know how to do it.但我不知道该怎么做。 The check box must NOT be editable.复选框不得编辑。 They need to be generated from code, based on user data on DB to check the yes option or the no option.它们需要根据 DB 上的用户数据从代码生成,以检查是选项还是否选项。

在此处输入图像描述

I try this:我试试这个:

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("YES" + checkBox + "\n" + "NO" + uncheckBox, f);

cell = new PdfPCell(p);
table.AddCell(cell);

It works good without the YES and NO text, if I only put both textboxes it workd good.没有 YES 和 NO 文本也能很好地工作,如果我只放置两个文本框,它就可以很好地工作。 The problem is when I write "YES" + checkbox, because it generate an extrange icon.问题是当我写“是”+复选框时,因为它会生成一个 extrange 图标。 Any solution?任何解决方案?

Since you indicated that the checkboxes shouldn't be modified, one possibility is to use images.由于您表示不应修改复选框,因此一种可能性是使用图像。 Create an empty checkbox and a checked checkbox.创建一个空复选框和一个选中的复选框。 Then add the appropriate image.然后添加适当的图像。

Pre-requisites :先决条件

  • Download / install NuGet package: iTextSharp (v. 5.5.13.3)下载/安装 NuGet package: iTextSharp (v. 5.5.13.3)

Add the following using directives添加以下使用指令

  • using System.IO;
  • using iTextSharp.text;
  • using iTextSharp.text.pdf;

Use your favorite image editor to create two checkboxes (ex: Paint)使用您最喜欢的图像编辑器创建两个复选框(例如:画图)

Here are some examples which can be used for testing:以下是一些可用于测试的示例:

  • Checkbox-Unchecked.png:复选框-Unchecked.png: 复选框-未选中
  • Checkbox-CheckedBlue1.png:复选框-CheckedBlue1.png: 复选框-CheckedBlue1
  • Checkbox-CheckedBlue2.png:复选框-CheckedBlue2.png: 复选框-CheckedBlue2
  • Checkbox-CheckedRed.png:复选框-CheckedRed.png: 复选框-CheckedRed
  • Checkbox-CheckedBlack.png:复选框-CheckedBlack.png: 复选框-CheckedBlack

In Visual Studio (Solution Explorer), create a folder (name: Images).在 Visual Studio(解决方案资源管理器)中,创建一个文件夹(名称:Images)。 Add each of your images to the folder.将每个图像添加到文件夹中。 In the Properties Window (for each filename), set "Copy to Output Directory" to "Copy Always".在属性 Window(对于每个文件名)中,将“复制到 Output 目录”设置为“始终复制”。

Code :代码

Note : Modify the image filenames in the code below to your desired filenames ("imgChecked" and "imgUnchecked").注意:将下面代码中的图像文件名修改为您想要的文件名(“imgChecked”和“imgUnchecked”)。

public void CreatePdf(string filename, string checkBoxFilename)
{
    using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
    {
        //create new instance - specify page size and margins
        using (Document doc = new Document(PageSize.LETTER, 1.0F, 1.0F, 1.0F, 1.0F))
        {
            PdfWriter writer = PdfWriter.GetInstance(doc, fs);

            //open
            doc.Open();

            //add new page
            doc.NewPage();

            //create table
            PdfPTable table = CreatePdfTable();

            //add table to Document
            doc.Add(table);

            //flush the FileStream
            fs.Flush();
        }
    }
}

private PdfPTable CreatePdfTable()
{
    PdfPTable table = new PdfPTable(new float[] { 40f, 18f, 50f }) { WidthPercentage = 100f };
    table.TotalWidth = 555f;

    //create font
    var fuente_cabecera = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12f);

    //column 1
    PdfPCell cell = new PdfPCell(new Phrase("¿Fuma o ha fumado alguna vez?", fuente_cabecera));
    cell.Rowspan = 2;
    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
    table.AddCell(cell);

    //column 2
    Paragraph p = new Paragraph();

    p = GetFilledInCheckBoxes("Yes", fuente_cabecera); //check "Yes"
    //p = GetFilledInCheckBoxes("No", fuente_cabecera); //check "No"
    //p = GetFilledInCheckBoxes("None", fuente_cabecera); //check "None"

    cell = new PdfPCell(p);
    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    cell.Rowspan = 2;
    
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase("This is text line 1", fuente_cabecera));
    table.AddCell(cell);

    cell = new PdfPCell(new Phrase("This is text line 2", fuente_cabecera));
    table.AddCell(cell);

    return table;
}

private Paragraph GetFilledInCheckBoxes(string whichCheckBoxIsChecked, Font font)
{
    //create new instance
    Paragraph p = new Paragraph();

    //get images
    Image imgChecked = Image.GetInstance(@".\Images\Checkbox-CheckedBlue1.png");
    Image imgUnchecked = Image.GetInstance(@".\Images\Checkbox-Unchecked.png");


    Image imgYes = null;
    Image imgNo = null;

    if (whichCheckBoxIsChecked == "Yes")
    {
        //set values
        imgYes = imgChecked;
        imgNo = imgUnchecked;
    }
    else if (whichCheckBoxIsChecked == "No")
    {
        //set values
        imgYes = imgUnchecked;
        imgNo = imgChecked;
    }
    else
    {
        //set values
        imgYes = imgUnchecked;
        imgNo = imgUnchecked;
    }

    //add appropriate checkbox
    Chunk chunkCheckBoxYes = new Chunk(imgYes, 0, 0, true);
    p.Add(chunkCheckBoxYes); //add to paragraph

    //add text - 'Yes'
    p.Add(new Chunk("Yes", font)); //add to paragraph

    //add spaces
    p.Add(new Chunk("  ", font)); //add to paragraph

    //add appropriate checkbox
    Chunk chunkCheckBoxNo = new Chunk(imgNo, 0, 0, true);
    p.Add(chunkCheckBoxNo); //add to paragraph

    //add text - 'No'
    p.Add(new Chunk("No", font));

    return p;
}

Resources :资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM