C# 如何显示✔;在PDF中使用iTextSharp?

C# 如何显示✔;在PDF中使用iTextSharp?,c#,pdf,itext,C#,Pdf,Itext,我正在尝试显示“✔" 使用iTextSharp打印PDF中的字符。但是,该字符不会显示在创建的PDF中。请帮助我。Font Wingdings打印此字符而不是“o”。 您需要将此字体连接到应用程序,然后将此字体应用于信函,并将字体嵌入pdf以实现兼容性 这是我之前在一个项目中使用的函数(未清理)。 请清理它,但它有一些您需要的基本功能。(我在项目目录中复制了我的自定义字体(font1.ttf和font2.ttf) 我希望它能帮助你 public void StartConvert(Str

我正在尝试显示“✔" 使用iTextSharp打印PDF中的字符。但是,该字符不会显示在创建的PDF中。请帮助我。Font Wingdings打印此字符而不是“o”。 您需要将此字体连接到应用程序,然后将此字体应用于信函,并将字体嵌入pdf以实现兼容性

这是我之前在一个项目中使用的函数(未清理)。 请清理它,但它有一些您需要的基本功能。(我在项目目录中复制了我的自定义字体(font1.ttf和font2.ttf)

我希望它能帮助你

    public void StartConvert(String originalFile, String newFile)
    {
        Document myDocument = new Document(PageSize.LETTER);
        PdfWriter.GetInstance(myDocument, new FileStream(newFile, FileMode.Create));
        myDocument.Open();

        int totalfonts = FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts");
        iTextSharp.text.Font content = FontFactory.GetFont("Pea Heather's Handwriting", 13);//13
        iTextSharp.text.Font header = FontFactory.GetFont("assign", 16); //16

        BaseFont customfont = BaseFont.CreateFont(@"font1.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
        Font font = new Font(customfont, 13);
        string s = " ";
        myDocument.Add(new Paragraph(s, font));

        BaseFont customfont2 = BaseFont.CreateFont(@"font2.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
        Font font2 = new Font(customfont2, 16);
        string s2 = " ";
        myDocument.Add(new Paragraph(s2, font2));

        try
        {
            try
            {                   
                using (StreamReader sr = new StreamReader(originalFile))
                {
                    // Read and display lines from the file until the end of
                    // the file is reached.
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        String newTempLine = "";
                        String[] textArray;
                        textArray = line.Split(' ');
                        newTempLine = returnSpaces(RandomNumber(0, 6)) + newTempLine;

                        int counterMax = RandomNumber(8, 12);
                        int counter = 0;
                        foreach (String S in textArray)
                        {
                            if (counter == counterMax)
                            {
                                Paragraph P = new Paragraph(newTempLine + Environment.NewLine, font);
                                P.Alignment = Element.ALIGN_LEFT;
                                myDocument.Add(P);
                                newTempLine = "";
                                newTempLine = returnSpaces(RandomNumber(0, 6)) + newTempLine;
                            }
                            newTempLine = newTempLine + returnSpaces(RandomNumber(1, 5)) + S;
                            counter++;
                        }
                        Paragraph T = new Paragraph(newTempLine, font2);
                        T.Alignment = Element.ALIGN_LEFT;

                        myDocument.Add(T);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
        catch (DocumentException de)
        {
            Console.Error.WriteLine(de.Message);
        }
        catch (IOException ioe)
        {
            Console.Error.WriteLine(ioe.Message);
        }

        try
        {
            myDocument.Close();
        }
        catch { }
    }
这对我很有用:


pdfStamper.formflatting=true;

您能否向我们展示使用iTextSharp将该字符添加到PDF文档中的代码?您是否使用带有✔ 角色?我从未见过这么多GC.Collect()在单个方法中调用。根据PDF规范,PDF呈现器必须支持一组共14种字体,Zapf Dingbats就是其中之一。此解决方案可能是最好的,因为它不需要任何额外的字体嵌入。实际嵌入字体的唯一原因是如果您不喜欢这种呈现方式。这当然要求您的PDF具有带有复选框的表单定义,该复选框包含“✔“已经,这个表单定义可能被压平。这不是问题的原始海报所呈现的情况。
Phrase phrase = new Phrase("A check mark: ");   
Font zapfdingbats = new Font(Font.FontFamily.ZAPFDINGBATS);
phrase.Add(new Chunk("\u0033", zapfdingbats));
phrase.Add(" and more text");
document.Add(phrase);