【问题标题】:How to convert panel into pdf with same font and style using iTextSharp如何使用 iTextSharp 将面板转换为具有相同字体和样式的 pdf
【发布时间】:2015-01-07 12:59:52
【问题描述】:

我正在使用 iTextSharp dll 来转换 aspx 页面中的面板。面板中有标签、文本框以及网格视图和列表视图等控件。问题是,在将面板转换为 pdf 格式后,我丢失了 listview/gridview/其他控件的所有格式。我正在使用下面的代码`

    string file = txtenqid.Text + ".pdf";
    string attachment = string.Format("attachment;filename={0}", file);
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/pdf";
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    Panel1.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
   iTextSharp.text.Document pdfdoc = new iTextSharp.text.Document();
   pdfdoc.SetMargins(0f, 0f, 0f, 0f);

    pdfdoc.SetPageSize(iTextSharp.text.PageSize.A3);
    HTMLWorker htmlparser = new HTMLWorker(pdfdoc);
    PdfWriter.GetInstance(pdfdoc, Response.OutputStream);
    pdfdoc.Open();
    htmlparser.Parse(sr);
    pdfdoc.Close();

请对代码进行必要的更改,以便我得到我想要的结果。提前致谢

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    格式可能会丢失,因为生成的 pdf 可能没有面板本身那么宽。尝试将 pdf 页面大小设置为 A4 而不是 A3

     iTextSharp.text.Document pdfdoc = new Document(new Rectangle(288f, 144f), 10, 10, 10, 10);
        pdfdoc.SetPageSize(iTextSharp.text.PageSize.A4);
    

    将其旋转为横向也可以解决您的问题

      pdfdoc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
    

    【讨论】:

    • ths 将改变页面设置 bt 不能解决我相同字体和样式的问题,并且 gridview 失去了我在 aspx 页面中所做的自动格式化
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2017-04-05
    • 2013-08-31
    • 2015-10-01
    • 2019-07-21
    • 2014-09-29
    相关资源
    最近更新 更多