如何使用itextsharp将UTF-8字符写入pdf文件?
我在谷歌上尝试了很多,但找不到..
感谢任何帮助。
请找到下面的代码:-
protected void Page_Load(object sender, EventArgs e)
{
StreamReader read = new StreamReader(@"D:\queryUnicode.txt", Encoding.Unicode);
string str = read.ReadToEnd();
Paragraph para = new Paragraph(str);
FileStream file = new FileStream(@"D:\Query.pdf",FileMode.Create);
Document pdfDoc = new Document();
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, file );
pdfDoc.Open();
pdfDoc.Add(para);
pdfDoc.Close();
Response.Write("Pdf file generated");
}
I have tried a lot on google but not able to find..
Any help is appreciated.
Please find the code below:-
protected void Page_Load(object sender, EventArgs e)
{
StreamReader read = new StreamReader(@"D:\queryUnicode.txt", Encoding.Unicode);
string str = read.ReadToEnd();
Paragraph para = new Paragraph(str);
FileStream file = new FileStream(@"D:\Query.pdf",FileMode.Create);
Document pdfDoc = new Document();
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, file );
pdfDoc.Open();
pdfDoc.Add(para);
pdfDoc.Close();
Response.Write("Pdf file generated");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您要将 HTML 转换为 PDF 吗?如果是这样,你应该注意这一点,否则没关系。我问的唯一原因是你最后关于获取
æ
的评论让我这么想。如果你是,请查看这篇文章:iTextSharp 5 波兰语字符
另外,有时当人们说“Unicode”时,他们真正的意思是什么尝试做的是将 Wingdings 之类的符号放入 PDF 中。如果您的意思是查看这篇文章,就会知道 Unicode 和 Wingding Symbols 实际上根本不相关。 iTextSharp 中的 Unicode 符号
这是一个完整的工作示例,它使用两种方法来编写 Unicode 字符,一种使用字符本身,一种使用 C# 转义序列。确保以支持宽字符的格式保存文件。此示例使用 iTextSharp 5.0.5。
使用 iTextSharp 时,您必须确保使用的字体支持您想要使用的 Unicode 代码点。使用字体时,您还需要指定
IDENTITY-H
。我不完全知道它是什么意思,但这里有一些讨论:iTextSharp 国际文本Are you converting HTML to PDF? If so, you should note that, otherwise never mind. The only reason I ask is that your last comment about getting
æ
makes me think that. If you are, check out this post:iTextSharp 5 polish character
Also, sometimes when people say "Unicode" what they're really trying to do is to get symbols like Wingdings into a PDF. If you mean that check out this post and know that Unicode and Wingding Symbols really aren't related at all. Unicode symbols in iTextSharp
Here's a complete working example that uses two ways to write Unicode characters, one using the character itself and one using the C# escape sequence. Make sure to save your file in a format that supports wide characters. This sample uses iTextSharp 5.0.5.
When working with iTextSharp you have to make sure that you're using a font that supports the Unicode code points that you want to use. You also need to specify
IDENTITY-H
when using your font. I don't completely know what it means but there's some talk about it here: iTextSharp international textArial 和 calibri 字体对我来说不起作用,用 Check“✔”(u+2713) 和 X-Mark“✘”(u+2718) 进行测试。
我使用 Segoe 字体并嵌入该字体取得了成功。
Arial and calibri fonts did not work for me, testing with the Check "✔" (u+2713) and X-Mark "✘" (u+2718).
I had success with the Segoe font, and embedding the font as well.