首页 > 解决方案 > 将 html 转换为 pdf (itextsharp) 时更改字体和样式

问题描述

我在 c# 中动态创建 html 表并使用 iTextSharp 将其转换为 pdf。PDF 是从 aspx 页面成功创建的,但在 pdf 中,字体和其他对齐方式会自动从原始文本更改。我的代码如下

pfilename = uName + shiftName + ".pdf";
//Converting Page to PDF               
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline;" + "filename=" + pfilename);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 0f, 0f, 0f, 19f);
FileStream fs = new FileStream(Context.Server.MapPath("~") + "/PDF_FILES/" + 
pfilename, FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, fs);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close(); 

标签: c#itexthtml-to-pdfxmlworkerhelper

解决方案


推荐阅读