【问题标题】:How to display ✔ in PDF using iTextSharp?如何使用 iTextSharp 在 PDF 中显示✔?
【发布时间】:2012-04-02 18:39:39
【问题描述】:

我正在尝试使用 iTextSharp 在 PDF 中显示“✔”字符。但是,该字符不会出现在创建的 PDF 中。请帮我解决这个问题。

【问题讨论】:

  • 您能否向我们展示您使用 iTextSharp 将该字符添加到 PDF 文档的代码?
  • 您是否使用带有 ✔ 字符的字体?

标签: c# pdf itext


【解决方案1】:
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);

【讨论】:

  • 根据 PDF 规范,PDF 渲染器必须支持一组通用的 14 种字体,Zapf Dingbats 就是其中之一。这个解决方案可能是最好的,因为它不需要任何额外的字体嵌入。实际嵌入字体的唯一原因是您不喜欢它的呈现方式。
【解决方案2】:

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 { }
    }

【讨论】:

  • 我从未见过在一个方法中调用这么多 GC.Collect()。
【解决方案3】:

这对我有用:

pdfStamper.FormFlattening = true;

【讨论】:

  • 这当然要求您的 pdf 有一个表单定义,其中包含一个已包含“✔”的复选框,并且此表单定义可能会被展平。这不是问题的原始发布者提出的情况。
猜你喜欢
  • 2015-07-22
  • 1970-01-01
  • 2011-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多