如何使用iTextSharp更改Pdf文档的字体大小
本文关键字:字体 文档 Pdf 何使用 iTextSharp 更改 | 更新日期: 2023-09-27 18:34:12
我们无法更改从以下代码生成的pdf的字体大小,有人可以帮助我们吗?
我们希望在更改字体大小后将该 html 文件转换为 pdf。
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
protected void ConvertToPDFNow()
{
StringWriter sw = new StringWriter();
HtmlTextWriter w = new HtmlTextWriter(sw);
print.RenderControl(w);
string htmWrite = sw.GetStringBuilder().ToString();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
htmWrite = Regex.Replace(htmWrite, "</?(a|A).*?>", "");
htmWrite = htmWrite.Replace("'r'n", "");
StringReader reader = new StringReader(htmWrite);
Document doc = new Document(PageSize.A4);
//Creating Document of A4 Size
HTMLWorker parser = new HTMLWorker(doc);
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
try
{
//rendering Html File
parser.Parse(reader);
}
catch (Exception ex)
{
}
finally
{
doc.Close();
}
}
尝试应用样式表:
...
var style = new StyleSheet();
style.LoadTagStyle("body", "size", "12px");
parser.SetStyleSheet(style);
...
下面的C#代码可以很好地改变itextpdf字体大小:
var style = new StyleSheet();
style.LoadTagStyle("body", "size", "8px");
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.SetStyleSheet(style);