C# 使用itextSharp在pdf中显示特殊字符

C# 使用itextSharp在pdf中显示特殊字符,c#,itextsharp,C#,Itextsharp,如何在pdf中显示像“&”这样的字符。我正在使用itextsharp创建pdf。 请提供帮助。您需要为我们提供更多的帮助。“不工作”是什么意思。是否正在显示其他内容,或者角色只是没有出现?您是在解析HTML还是只使用常规的短语和段落类?下面是一个完整的示例,它使用常规的iTextSharp类以及HTMLWorker来显示符号和。这三个对我来说都很好: using System; using System.Collections.Generic; using System.ComponentMod

如何在pdf中显示像“&”这样的字符。我正在使用itextsharp创建pdf。
请提供帮助。

您需要为我们提供更多的帮助。“不工作”是什么意思。是否正在显示其他内容,或者角色只是没有出现?您是在解析HTML还是只使用常规的
短语
段落
类?下面是一个完整的示例,它使用常规的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();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用iTextSharp.text;
使用iTextSharp.text.pdf;
使用System.IO;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
//创建新文档
iTextSharp.text.Document Doc=新的iTextSharp.text.Document(PageSize.LETTER);
//将其写入内存流
使用(FileStream FS=newfilestream(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop),“Output.pdf”)、FileMode.Create、FileAccess.Write、FileShare.Read)
{
objectwriter=PdfWriter.GetInstance(Doc,FS);
//打开PDF进行写作
Doc.Open();
//插入一页
Doc.NewPage();
//添加带有符号和的短语
添加文档(新短语(“你好&再见”);
//创建一些带有转义符和未转义符的HTML
string Html=“这是一个转义符号和:&;

这是一个非转义符号和:&