本文介绍了使用itextSharp在pdf中显示特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在pdf中显示像'&'这样的字符。我正在使用itextsharp来创建pdf。
请帮忙。
How to display character like '&' in pdf.I am using itextsharp to create pdf.Please help.
推荐答案
您需要向我们提供更多信息。什么是没有工作的意思。是显示其他内容还是角色没有出现?您是在解析HTML还是仅使用常规短语
和段落
类?下面是一个完整的示例,它使用常规的iTextSharp类以及 HTMLWorker
来显示&符号。这三个对我来说都很合适:
You need to give us more to work with. What does "no working" mean. Is something else being displayed or is the character just not appearing? Are you parsing HTML or just using regular Phrase
and Paragraph
classes? Below is a full sample that uses regular iTextSharp classes as well as the HTMLWorker
to display ampersands. All three work just fine for me:
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 : &</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();
}
}
}
这篇关于使用itextSharp在pdf中显示特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!