c# - 将 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();
解决方案
推荐阅读
- php - 在 XIBO CMS 中导出布局并另存为 HTML
- graph - chartjs 的自定义工具提示中的插入符号
- javascript - Promises 问题:尝试包装 mysql 查询以在 NodeJS/Express 上使用它
- javascript - 如何获取选定的值
- annotations - 如何通过调整缩放级别将所有坐标包含在 MapBox 中?
- python - 使用create_engine的creator参数时如何将驱动参数传递给sqlalchemy
- aem - AEM 中的 OOTB Granite 渲染条件
- python - 在 python 多处理队列中推进 tensorflow 数据集迭代器
- google-cloud-platform - 如果在 GCP 中运行快照时添加了新数据会怎样?
- java - 如何使用 onAcitivtyResult 进入一些片段 int Android