0

I am using iTextSharp v5.5.11 and I want to amend the following code to display a checked checkbox (may not necessarily need a checkbox control; an equivalent checked checkbox icon or similar would do) in the PDF that is generated. The code I have so far produced a checkbox which is not checked on PDF generation. As hinted, there may be better way to do this. Here is the code I have so far. Please help:

using System;
using System.Collections.Generic;
using System.IO;
using Carnotaurus.UtilityPack.Extensions.PrimitiveExtensions;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Carno.GhostPubs.WindowsFormsApp
{
    public static class PdfITextSharpExtensions
    {
        public static void CreatePdf(this Document doc, IReadOnlyList<string> lines, int pageSize,
            string outputFullFilePath)
        {
            try
            {
                var writer = PdfWriter.GetInstance(doc,
                    new FileStream(outputFullFilePath, FileMode.Create, FileAccess.Write, FileShare.None));

                doc.Open();

                for (var index = 0; index <= lines.Count - 1; index++)
                {
                    var mod = index % pageSize;

                    if (mod == 0 && index > 0)
                    {
                        doc.NewPage();
                    }

                    var field = lines[index];

                    doc.Add(new Paragraph(field));

                    var cb = writer.DirectContent;

                    var onOff = new PdfAppearance[2];

                    onOff[0] = cb.CreateAppearance(20, 20);
                    onOff[0].Rectangle(1, 1, 18, 18);
                    onOff[0].Stroke();

                    onOff[1] = cb.CreateAppearance(20, 20);
                    onOff[1].SetRGBColorFill(255, 128, 128);
                    onOff[1].Rectangle(1, 1, 18, 18);
                    onOff[1].FillStroke();
                    onOff[1].MoveTo(1, 1);
                    onOff[1].LineTo(19, 19);
                    onOff[1].MoveTo(1, 19);
                    onOff[1].LineTo(19, 1);
                    onOff[1].Stroke();

                    var rect = new Rectangle(180, 806 - index * 40, 200, 788 - index * 40);
                    var checkFieldName = field.RemoveSpaces();
                    var radioCheckField = new RadioCheckField(writer, rect, checkFieldName, "On");
                    var checkField = radioCheckField.CheckField;
                    checkField.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1]);
                    checkField.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
                    // checkField.ValueAsName = "On";
                    writer.AddAnnotation(checkField);

                    ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT,
                        new Phrase(field, new Font(Font.FontFamily.HELVETICA, 18)), 210, 790 - index * 40, 0);

                    cb = writer.DirectContent;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                doc.Close();
            }
        }
    }
}
Phil C
  • 3,687
  • 4
  • 29
  • 51
  • 1
    A downvote without a comment isn't much use – Phil C Apr 09 '17 at 05:20
  • I didn't downvote, but the question has some serious flaws. From a previous conversation, I assume that you don't want to add a form field. You only want to add a check box, either checked or unchecked. That is done using a glyph, **NOT** using a `RadioCheckField`. If my assumption is correct, people will get frustrated, because they will tell you have to check a check box in a form; you will also get frustrated because you don't want a form, you just want a checked check box. In short: the way you phrase your question frustrates everyone. I suggest that you fix your question. – Bruno Lowagie Apr 09 '17 at 07:34
  • If you don't believe I wasn't the one who down-voted, I can easily add an extra down-vote to convince you. – Bruno Lowagie Apr 09 '17 at 07:34
  • @BrunoLowagie That's right; I just want to add a checkbox, not necessarily a checkbox field. – Phil C Apr 09 '17 at 07:37
  • In that case, **please update your question** because right now, every answer is going to explain how to create a form, which is not what you want. It's as if you want to eat an ice cream, but you are asking for a lollipop. Then when people give you a lollipop, you are disappointed, and the people giving you what you want don't understand your disappointment. – Bruno Lowagie Apr 09 '17 at 07:40
  • Updated - Thanks – Phil C Apr 09 '17 at 07:44

1 Answers1

0

From the comments, we have established that you don't want an interactive check box (one that can be changed by the user by clicking the check box in Adobe Reader). You don't want a PDF form; you just want a check box characters (either checked or unchecked).

Unfortunately, I see that you are using Helvetica as font, and Helvetica doesn't contain a check box or check mark character. You will have to find another font.

Your question has been answered many times before on Stack Overflow:

The advantage of using an image is that you really want to shape the check box exactly the way you want. When using a font, you will have to be happy with whatever glyph the font provides. That could be an empty check box (as is the case in the TickboxCharacter) example; or a check mark without a box, as explained in How to display ✔ in PDF using iTextSharp?

You may also want to read iText : Unable to print mathematical characters like ∈, ∩, ∑, ∫, ∆ √, ∠ because a check box (checked or unchecked) is nothing more that a special character such as ∈, ∩, ∑, ∫, ∆ √, ∠,...

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165