天道酬勤,学无止境

How to display chinese characters in PDF using ITextSharp in c#?

I want to display one word of chinese characters in PDF along with english characters. Currently chinese characters are not getting displayed in PDF.I have used itextsharp to generate pdf with contents.I have tried some chinese fonts but not working. How to show chinese characters along with english characters in PDF using itextsharp?

var docIndex = new Document(new Rectangle(792, 612));

var writer = PdfWriter.GetInstance(docIndex, new FileStream(Server.MapPath("~") + "/pdf/Project-" + report.pulledId + currentTime + ".pdf", FileMode.Create));

docIndex.Open();
FontFactory.Register("c:/windows/fonts/msmincho.ttc");
StyleSheet style = new StyleSheet();
style.LoadTagStyle("body", "face", "songti");
style.LoadTagStyle("body", "encoding", BaseFont.IDENTITY_H);
foreach (var elementIndex in HTMLWorker.ParseToList(
new StringReader(sbsample.ToString()), style))
{
    docIndex.Add(elementIndex);

}
docIndex.Close();

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • 如何在使用iTextSharp创建的pdf文件中显示汉字(How to display chinese characters in pdf file created with iTextSharp)
    问题 我有包含一些英文字符和一些中文字符的字符串数据。 我正在使用iTextSharp使用此数据创建pdf文件。 创建pdf文件后,当我打开它时,pdf仅包含英文字符。 它没有显示汉字。 您能告诉我如何在pdf文件中显示汉字吗? 请注意,即时通讯写入pdf的字符串数据包含动态语言字符,例如,有时是英语,somethimes中文,有时是日语等等。 回答1 在iText(Sharp)文档中对此进行了说明。 当具有不同语言的字形的String时,需要使用FontSelector ,如本示例所示。 FontSelector selector = new FontSelector(); selector.AddFont(FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12)); selector.AddFont(FontFactory.GetFont("MSung-Light", "UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED)); Phrase ph = selector.Process(TEXT); document.Add(new Paragraph(ph)); 在这种情况下,我先将Times Roman添加到字体选择器,然后再添加MSung-Light。 现在, TEXT所有英文字符都将使用Times Roman
  • How to display chinese characters in pdf file created with iTextSharp
    I have string data which contains some english characters and some chinese characters. I m creating a pdf file with this data using iTextSharp. After pdf file is created, when i open it, pdf contains only english characters. It is not showing chinese characters. Can you please tell me how to display chinese characters in pdf file?. Please note that the string data that i m writing to pdf contains dynamic language characters i.e sometimes english, somethimes chinese, sometimes japanese and so on.
  • How to do to display Chinese font in pdf using iTextSharp?
    In the following code , chinese font( contained html text) doesnot display in pdf generated. I also try styles and font in this method. Please help to solve this problem. Thanks in advance to all. public static bool GeneratedPDF(string strHTMLText, string filename, string action, string rpttype) { bool blnReturn = false; string fontpath = HttpContext.Current.Server.MapPath("~/files/fonts/"); string filepath = HttpContext.Current.Server.MapPath("~/files/pdf/"); BaseFont customfont = BaseFont.CreateFont(fontpath + "simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font font = new Font
  • 在将HTML转换为Pdf时显示Unicode字符(Display Unicode characters in converting Html to Pdf)
    问题 我正在使用itextsharp dll将HTML转换为PDF。 HTML包含一些Unicode字符,例如α,β...当我尝试将HTML转换为PDF时,Unicode字符未显示在PDF中。 我的功能: Document doc = new Document(PageSize.LETTER); using (FileStream fs = new FileStream(Path.Combine("Test.pdf"), FileMode.Create, FileAccess.Write, FileShare.Read)) { PdfWriter.GetInstance(doc, fs); doc.Open(); doc.NewPage(); string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF"); BaseFont bf = BaseFont.CreateFont(arialuniTff, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font fontNormal = new Font(bf, 12, Font.NORMAL); List<IElement> list =
  • How to display ✔ in PDF using iTextSharp?
    I am trying to display the "✔" character in a PDF using iTextSharp. However the character won't show up on the created PDF. Please help me on this.
  • html至pdf缺少某些字符(itextsharp)(Html to pdf some characters are missing (itextsharp))
    问题 我想使用itextsharp库将gridview导出为pdf。 问题是pdf文档中缺少某些土耳其字符,如İ,ı,Ş,ş等。 用于导出pdf的代码为: protected void LinkButtonPdf_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); System.IO.StringWriter stringWrite = new StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); GridView1.RenderControl(htmlWrite); StringReader reader = new StringReader(textConvert
  • 如何使用iTextSharp获取文本格式(how can i get text formatting with iTextSharp )
    问题 我正在使用iTextSharp从PDF阅读文本内容。 我也能够阅读。 但是我失去了字体,颜色等文本格式。还有什么方法可以获取这种格式。 以下是我用于精确输入文字的代码段- PdfReader reader = new PdfReader("F:\\EBooks\\AspectsOfAjax.pdf"); textBox1.Text = ExtractTextFromPDFBytes(reader.GetPageContent(1)); private string ExtractTextFromPDFBytes(byte[] input) { if (input == null || input.Length == 0) return ""; try { string resultString = ""; // Flag showing if we are we currently inside a text object bool inTextObject = false; // Flag showing if the next character is literal e.g. '\\' to get a '\' character or '\(' to get '(' bool nextLiteral = false; // () Bracket nesting
  • 使用itextsharp将阿拉伯语“ unicode”内容html或xml转换为pdf(Convert arabic“unicode” content html or xml to pdf using itextsharp)
    问题 经过大量搜索后,我试图在asp.net MVC3应用程序中创建报告,我发现许多博客文章都谈到ITextSharp将Html/Razor转换为Pdf我试图解析razor视图以获取PDf,如下所示 public void Render(ViewContext viewContext, TextWriter writer) { var doc = new Document(); // associate output with response stream var pdfWriter = PdfWriter.GetInstance(doc, viewContext.HttpContext.Response.OutputStream); pdfWriter.CloseStream = false; viewContext.HttpContext.Response.ContentType = "application/pdf"; viewContext.HttpContext.Response.ContentEncoding = System.Text.Encoding.UTF8; // generate view into string var sb = new System.Text.StringBuilder(); TextWriter tw = new System.IO
  • iText pdf not displaying Chinese characters when using NOTO fonts or Source Hans
    I am trying to use NOTO fonts (https://www.google.com/get/noto/) to display Chinese characters. Here is my sample code,a modified sample code from iText. public void createPdf(String filename) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); //This is simple English Font FontFactory.register("c:/temp/fonts/NotoSerif-Bold.ttf", "my_nato_font"); Font myBoldFont = FontFactory.getFont("my_nato_font"); BaseFont bf = myBoldFont.getBaseFont(); document.add(new Paragraph(bf
  • 使用NOTO字体或Source Hans时,iText pdf不显示汉字(iText pdf not displaying Chinese characters when using NOTO fonts or Source Hans)
    问题 我正在尝试使用NOTO字体(https://www.google.com/get/noto/)显示中文字符。 这是我的示例代码,是iText的修改后的示例代码。 public void createPdf(String filename) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); //This is simple English Font FontFactory.register("c:/temp/fonts/NotoSerif-Bold.ttf", "my_nato_font"); Font myBoldFont = FontFactory.getFont("my_nato_font"); BaseFont bf = myBoldFont.getBaseFont(); document.add(new Paragraph(bf.getPostscriptFontName(), myBoldFont)); //This is Chinese font //Option 1 : Font
  • 如何使用iTextSharp将HTML转换为PDF(How to convert HTML to PDF using iTextSharp)
    问题 我想使用iTextSharp将以下HTML转换为PDF,但不知道从哪里开始: <style> .headline{font-size:200%} </style> <p> This <em>is </em> <span class="headline" style="text-decoration: underline;">some</span> <strong>sample<em> text</em></strong> <span style="color: red;">!!!</span> </p> 回答1 首先,尽管HTML和PDF是在同一时间创建的,但它们并不相关。 HTML旨在传达更高级别的信息,例如段落和表格。 尽管有控制它的方法,但最终由浏览器来绘制这些更高级的概念。 PDF旨在传达文档,并且无论文档在何处呈现,它们都必须“看起来”相同。 在HTML文档中,段落的宽度可能为100%,根据显示器的宽度,它可能需要2行或10行,而在打印时可能是7行,而在手机上查看时可能会乘20条线。 但是,PDF文件必须独立于渲染设备,因此无论您的屏幕大小如何,它都必须始终完全相同。 因为上面的葡萄汁中,PDF不支持像“表”或“段落”抽象的东西。 PDF支持三项基本功能:文本,线条/形状和图像。 (还有其他内容,例如注释和电影,但我想在这里保持简单。)在PDF中,您不会说
  • ITextSharp查找PDF中特定文本的坐标(ITextSharp Find coordinates of specific text in PDF)
    问题 我发现许多站点和帖子都与我的问题相同,但是它们似乎都具有共同点,那就是人们在使用示例在特定位置插入新文本的方式回答这些问题。 我有一个我无法控制的另一个程序生成的PDF文档,它有一行供客户端登录,但是该行不在绝对位置,因此我们使用的名为AssureSign的服务将无法正常工作,因为您必须知道签名行的位置。 因此,我需要创建一个新程序,该程序将找到签名行的位置,并将该信息发送到assuresign系统。 这确实应该很简单,但是由于某种原因我没有得到它 回答1 您可以使用iText(Sharp)的解析器包来查找给定文本的位置。 但是,您必须实现自己的RenderListener,因为该程序包的主要用例是文本提取,而不是文本位置查找。 这并不像您想的那样容易,例如单词的各个字符可能以任何顺序分开出现。 PS: 但是,首先您必须找出签名行是由字符组成(正如您的问题所暗示的那样)还是它是绘制的路径。 另外,您将必须找出该行在文档中是否唯一。 在前一种情况下,您需要的RenderListener实现必须检查转发的TextRenderInfo对象,以在其RenderText方法中进行处理。 如果其文本内容包含构成签名行的那些独特字符,则必须存储此TextRenderInfo的位置数据。 如果行字符不是唯一的,则您将不得不找到使其成为唯一的其他条件,例如,某些前面的字符串
  • Html to pdf some characters are missing (itextsharp)
    I want to export gridview to pdf by using the itextsharp library. The problem is that some turkish characters such as İ,ı,Ş,ş etc... are missing in the pdf document. The code used to export the pdf is: protected void LinkButtonPdf_Click(object sender, EventArgs e) { Response.ContentType = "application/pdf"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); System.IO.StringWriter stringWrite = new StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite =
  • 如何通过在 C# 中使用 itextsharp 创建带有泰米尔语字体的 PDF 文件?(How to Create PDF file with Tamil Font by using itextsharp in C#?)
    问题 我们通过传递 TAMIL 文本(印度语言之一)在 C# 应用程序中创建 pdf 文件。所以,我已经为我的泰米尔语字体安装了 AVVAIYAR.TTF(泰米尔语字体之一)字体。但是当我运行下面提到的pgm,创建的pdf文件不包含任何泰米尔字体显示。它显示空行而不是泰米尔文本...... C#代码: Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\pdfout.pdf", FileMode.Create)); document.Open(); PdfContentByte pcb = writer.DirectContent; Font ft = new Font(); FontFactory.Register(@"C:\WINDOWS\Fonts\AVVAIYAR.TTF", "AVVAIYAR"); ft = FontFactory.GetFont("AVVAIYAR"); Paragraph pr1 = new Paragraph("இது முதல் பேரா", ft); Paragraph pr2 = new Paragraph("This is a Sub Paragraph"); Paragraph
  • iTextSharp - acroform field encoding
    I'm trying to fill out a AcroForm's textfield with iTextSharp. The Acroform textfield was created also by iTextSharp by this piece of code: TextField Field = new TextField(OutputWriter, FieldPos, "MyField"); OutputWriter.AddAnnotation(Field.GetTextField()); // OutputWriter is writing to form.pdf I fill the form using this code: PdfReader reader = new PdfReader("form.pdf"); PdfStamper filledOutForm = new PdfStamper(reader, new FileStream("filled_form.pdf", FileMode.Create)); AcroFields form = filledOutForm.AcroFields; form.SetField("MyField", "some unicode data"); However, when I open filled
  • iTextSharp - acroform 字段编码(iTextSharp - acroform field encoding)
    问题 我正在尝试使用 iTextSharp 填写 AcroForm 的文本字段。 Acroform 文本字段也是由 iTextSharp 通过这段代码创建的: TextField Field = new TextField(OutputWriter, FieldPos, "MyField"); OutputWriter.AddAnnotation(Field.GetTextField()); // OutputWriter is writing to form.pdf 我使用以下代码填写表格: PdfReader reader = new PdfReader("form.pdf"); PdfStamper filledOutForm = new PdfStamper(reader, new FileStream("filled_form.pdf", FileMode.Create)); AcroFields form = filledOutForm.AcroFields; form.SetField("MyField", "some unicode data"); 但是,当我在 Acrobat Reader 中打开 fill_form.pdf 时,除非我手动编辑该字段(例如,我手动将一个字符附加到该字段),否则 unicode 字符是不可见的。 我还尝试通过以下方式设置字段的字体:
  • How to use non breaking space in iTextSharp
    How can the non breaking space can be used to have a multiline content in a PdfPTable cell. iTextSharp is breaking down the words with the space characters. The scenario is I want a multiline content in a table head, such as in first line it may display "Text1 &" and on second line it would display "Text", on rendering the PDF the Text1 is displayed in first line, then on second line & is displayed and on third it takes the length of the first line and truncates the remaining characters to the next line. Or can I set specific width for each and every column of the table so as to accomodate
  • 如何使用 iTextSharp 和 C# 在现有 PDF 文件中添加粘滞便笺、在光标处插入文本、.. 注释?(How to add Sticky Notes, Insert Text at cursor, .. Annotations in existing PDF file using iTextSharp with C#?)
    问题 我想使用 iTextSharp 和 C# 在现有 PDF 文件中添加注释注释。 请给出示例代码以在现有 PDF 文件中添加注释。 这里是我的注释的 PS 脚本: [/Contents (My Text contents) /Rect [100 600 150 550] /SrcPg 1 /Title (My Title text) /Color [0 0 1] /Subtype /Caret /ANN pdfmark 回答1 iText in Action - 2nd Edition 第 7 章中的 iText(Sharp) 示例 TimetableAnnotations1.java / TimetableAnnotations1.cs 展示了如何向现有 PDF 添加注释。 中心代码是(在 C# 示例中): rect = GetPosition(screening); annotation = PdfAnnotation.CreateText( stamper.Writer, rect, movie.MovieTitle, string.Format(INFO, movie.Year, movie.Duration), false, "Help" ); annotation.Color = WebColors.GetRGBColor( "#" + movie.entry
  • Convert arabic“unicode” content html or xml to pdf using itextsharp
    I am trying to create reports in my asp.net MVC3 application after a lot of search I found many blog posts talks about ITextSharp to convert my Html/Razor to Pdf I am trying to parse razor view to get PDf as follows public void Render(ViewContext viewContext, TextWriter writer) { var doc = new Document(); // associate output with response stream var pdfWriter = PdfWriter.GetInstance(doc, viewContext.HttpContext.Response.OutputStream); pdfWriter.CloseStream = false; viewContext.HttpContext.Response.ContentType = "application/pdf"; viewContext.HttpContext.Response.ContentEncoding = System.Text
  • iTextSharp 和特殊字符(斯洛伐克语素)(iTextSharp and special characters (slovak graphemes))
    问题 我遇到了一些特殊的斯洛伐克字符(例如 č、ň 和 ť)的问题。 它们在 itextsharp 生成的 pdf 中消失了。 据我BaseFont ,这个问题与我的BaseFont编码有关。 目前我正在使用这个: BaseFont.CreateFont(BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1250, BaseFont.NOT_EMBEDDED) 有人建议这应该有效: BaseFont.CreateFont(BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED) 但它抛出这个异常错误: System.ArgumentException was caught Message='Identity-H' is not a supported encoding name. Parameter name: name ParamName=name Source=mscorlib 任何人都知道可能的原因和解决方案? 回答1 问题在这里: BaseFont.CreateFont(BaseFont.HELVETICA ... BaseFont.HELVETICA是标准的 type 1 字体,不能用于BaseFont