How do I get the checkbox to show as checked in the generated PDF for this specific situation? [duplicate]

。_饼干妹妹 提交于 2019-12-25 18:35:18

问题


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

回答1:


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:

  • Check Mark in iTextSharp advises to use a PNG instead of a character.
  • How to add a check box in a PDF file using iText 7? explains how to add a check box using a font (my answer is the iText 5 answer; the accepted answer is the iText 7 answer; it beats me why you're still using iText 5 or earlier).

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 ∈, ∩, ∑, ∫, ∆ √, ∠,...



来源:https://stackoverflow.com/questions/43303301/how-do-i-get-the-checkbox-to-show-as-checked-in-the-generated-pdf-for-this-speci

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!