0

我们无法更改从以下代码生成的 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();
    }
   }
4

2 回答 2

2

尝试应用样式表:

...
    var style = new StyleSheet();
    style.LoadTagStyle("body", "size", "12px");
    parser.SetStyleSheet(style);
...
于 2013-10-05T10:22:06.253 回答
-1

下面的 C# 代码非常适合更改 itextpdf 字体大小:

var style = new StyleSheet();
style.LoadTagStyle("body", "size", "8px");
HTMLWorker htmlworker = new HTMLWorker(document);
htmlworker.SetStyleSheet(style);
于 2014-08-14T10:32:33.817 回答