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(stringWrite.ToString())); document doc = new document(PageSize.A4); HTMLWorker parser = new HTMLWorker(doc); pdfWriter.GetInstance(doc,Response.OutputStream); doc.open(); parser.Parse(reader); doc.Close(); } public static string textConvert(string S) { if (S == null) { return null; } try { System.Text.EnCoding encFrom = System.Text.EnCoding.UTF8; System.Text.EnCoding encTo = System.Text.EnCoding.UTF8; string str = S; Byte[] b = encFrom.GetBytes(str); return encTo.GetString(b); } catch { return null; } }
注意:当我想在pdf文档中插入字符时,会显示缺少的字符.我用这段代码插入字符:
BaseFont bfFont = BaseFont.CreateFont("C:\windows\Fonts\arial.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMbedDED); Font Fontozel = new Font(bfFont,12,Font.norMAL,new color(0,0)); doc.Add(new Paragraph("İİııŞŞşşĞĞğğ",Fontozel));解决方法 最后我想我找到了解决方案,我改变了它的一些源代码,以显示土耳其字符(土耳其语字符代码是cp1254)
I add “
public const string CP1254 = "Cp1254";
” to [BaseFont.cs] in the source code.After that I modify the [FactoryPropertIEs.cs].I changed like this;
public Font GetFont(ChainedPropertIEs props){I don't write the whole code.I changed only code below;------------Default itextsharp code------------------------------------------------------ if (enCoding == null) enCoding = BaseFont.WINANSI; return FontImp.GetFont(face,enCoding,true,size,style,color);-------------modifIEd code-------------------------------------------- enCoding = BaseFont.CP1254; return FontImp.GetFont("C:\windows\Fonts\arial.ttf",color);}
在编译新的dll之后,显示缺少的字符.
总结以上是内存溢出为你收集整理的c# – Html to pdf一些字符丢失(itextsharp)全部内容,希望文章能够帮你解决c# – Html to pdf一些字符丢失(itextsharp)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)