[英]How to write UTF-8 characters to a pdf file using itextsharp?
我在谷歌上嘗試了很多但找不到..
任何幫助表示贊賞。
請在下面找到代碼:-
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");
}
您是否將HTML轉換為PDF? 如果是這樣,你應該注意,否則沒關系。 我問的唯一原因是你最后的評論是關於獲得æ
讓我想到了。 如果你是,請看看這篇文章: iTextSharp 5波蘭人物
此外,有時當人們說“Unicode”時,他們真正想做的是將像Wingdings這樣的符號變成PDF。 如果你的意思是看看這篇文章,並知道Unicode和Wingding符號真的根本沒有關系。 iTextSharp中的Unicode符號
這是一個完整的工作示例,它使用兩種方式編寫Unicode字符,一種使用字符本身,另一種使用C#轉義序列。 確保以支持寬字符的格式保存文件。 此示例使用iTextSharp 5.0.5。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Create our document object
Document Doc = new Document(PageSize.LETTER);
//Create our file stream
using (FileStream fs = new FileStream(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read))
{
//Bind PDF writer to document and stream
PdfWriter writer = PdfWriter.GetInstance(Doc, fs);
//Open document for writing
Doc.Open();
//Add a page
Doc.NewPage();
//Full path to the Unicode Arial file
string ARIALUNI_TFF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
//Create a base font object making sure to specify IDENTITY-H
BaseFont bf = BaseFont.CreateFont(ARIALUNI_TFF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//Create a specific font object
Font f = new Font(bf, 12, Font.NORMAL);
//Write some text, the last character is 0x0278 - LATIN SMALL LETTER PHI
Doc.Add(new Phrase("This is a test ɸ", f));
//Write some more text, the last character is 0x0682 - ARABIC LETTER HAH WITH TWO DOTS VERTICAL ABOVE
Doc.Add(new Phrase("Hello\u0682", f));
//Close the PDF
Doc.Close();
}
}
}
}
使用iTextSharp時,您必須確保使用的字體支持您要使用的Unicode代碼點。 使用字體時,還需要指定IDENTITY-H
。 我不完全知道這意味着什么,但這里有一些討論: iTextSharp國際文本
public static Font GetArialUtf8Font(int fontSize = 9)
{
string fontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIAL.TTF");
//Create a base font object making sure to specify IDENTITY-H
BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//Create a specific font object
return new Font(bf, fontSize, Font.NORMAL);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.