【问题标题】:To display special character in pdf using itextSharp使用 itextSharp 在 pdf 中显示特殊字符
【发布时间】:2011-03-18 06:27:34
【问题描述】:

如何在 pdf 中显示 '&' 之类的字符。我正在使用 itextsharp 创建 pdf。 请帮忙。

【问题讨论】:

    标签: c# itextsharp


    【解决方案1】:

    你需要给我们更多的合作。 “不工作”是什么意思。是否正在显示其他内容或角色只是没有出现?您是在解析 HTML 还是只使用常规的 PhraseParagraph 类?下面是使用常规 iTextSharp 类以及 HTMLWorker 来显示 & 符号的完整示例。这三个对我来说都很好:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //Create a new document
                iTextSharp.text.Document Doc = new iTextSharp.text.Document(PageSize.LETTER);
    
                //Write it to a memory stream
                using (FileStream FS = new FileStream(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop), "Output.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    object writer = PdfWriter.GetInstance(Doc, FS);
    
                    //Open the PDF for writing
                    Doc.Open();
    
                    //Insert a page
                    Doc.NewPage();
    
                    //Add a phrase with an ampersand
                    Doc.Add(new Phrase("Hello & Goodbye"));
    
                    //Create some HTML with an escaped and also unescaped ampersand
                    string Html = "<html><head><title></title></head><body><p>This is an escaped ampersand : &amp;</p><p>And this is a non-escaped ampersand : &</body></html";
    
                    //Read the HTML in a StringRead
                    using (StringReader SR = new StringReader(Html))
                    {
                        //Grab the elements
                        List<IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(SR, null);
    
                        //Loop through them
                        for (int i = 0; i < elements.Count; i++)
                        {
                            //Add them to the document
                            Doc.Add(elements[i]);
                        }
                    }
    
                    //Close the PDF
                    Doc.Close();
                }
                this.Close();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      • 2016-01-07
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多