【问题标题】:Converting aspx to pdf将 aspx 转换为 pdf
【发布时间】:2011-10-06 15:29:07
【问题描述】:

我使用 ItextSharp 从 aspx 页面生成 pdf 文件。

但它在 obj.Parse(se) 中给了我一个错误,其中 se 是字符串读取器,它采用以下路径:-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

我被这个问题困住了。

请帮助并提供建议。

谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text.RegularExpressions;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml;
using System.Xml;
using iTextSharp.text.html.simpleparser;
public partial class Pdf : System.Web.UI.Page
{
protected override void Render(HtmlTextWriter writer)
{
    MemoryStream mem = new MemoryStream();
    StreamWriter twr = new StreamWriter(mem);
    HtmlTextWriter myWriter = new HtmlTextWriter(twr);
    base.Render(myWriter);
    myWriter.Flush();
    myWriter.Dispose();
    StreamReader strmRdr = new StreamReader(mem);
    strmRdr.BaseStream.Position = 0;
    string pageContent = strmRdr.ReadToEnd();
    strmRdr.Dispose();
    mem.Dispose();
    writer.Write(pageContent);
    CreatePDFDocument(pageContent);


 }
 public  void CreatePDFDocument(string strHtml)
 {

    string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
    // step 1: creation of a document-object
    Document document = new Document();
    // step 2:
    // we create a writer that listens to the document
    PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
    StringReader se = new StringReader(strHtml);
    HTMLWorker obj = new HTMLWorker(document);
    document.Open();
    obj.Parse(se);
    document.Close();
    ShowPdf(strFileName);



 }
 public void ShowPdf(string strFileName)
 {
    Response.ClearContent();
    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
    Response.ContentType = "application/pdf";
    Response.WriteFile(strFileName);
    Response.Flush();
    Response.Clear();
}
}

【问题讨论】:

  • 愿意与我们分享错误信息吗?
  • 查看此链接是否对您有帮助stackoverflow.com/questions/6702884/…
  • 更好的解决方案:stackoverflow.com/q/1220423/2291
  • 我还警告您不要将 HtmlWorker 用于任何复杂的布局。它的解析能力非常有限,除非您在其中推送非常简单的 XHTML,否则您应该考虑在代码中构建 PDF
  • 你能发布错误吗?您可能需要考虑购买一个组件来为您执行此操作。它物有所值,它为您节省的时间很容易收回。虽然免费组件往往可以正常工作,但我已经用它们遇到了很多边缘情况。我使用过 ABCPDF.Net [websupergoo.com/abcpdf-1.htm],它对我扔给它的所有东西都非常有效。您可以下载免费试用版进行测试。

标签: c# .net asp.net


【解决方案1】:

这是一个对我有用的例子。有一些代码,但它显示了您可以利用诸如 css 之类的东西的方法。希望对您有所帮助。

private void ExportToPDF()
{
    string s = "<table><td><tr>First<b>row</b></tr></td></table>";
    Document document = new Document(PageSize.LETTER, 30, 30, 60, 35);
    MemoryStream msReport = new MemoryStream();
    string strHTMLpath = Server.MapPath("../myHTML.html");
    string strPDFpath = Server.MapPath("../myPDF.pdf");
    try
    {
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        StreamWriter strWriter = new StreamWriter(strHTMLpath, false, Encoding.UTF8);
        strWriter.Write(s.ToString());
        strWriter.Close();
        strWriter.Dispose();
        iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
        styles.LoadTagStyle("ol", "leading", "16,0");
        PdfWriter.GetInstance(document, new FileStream(strPDFpath, FileMode.Create));
        document.Add(new Header(iTextSharp.text.html.Markup.HTML_ATTR_STYLESHEET, "Style.css"));
        document.Open();
        ArrayList objects;
        styles.LoadTagStyle("li", "face", "garamond");
        styles.LoadTagStyle("span", "size", "8px");
        styles.LoadTagStyle("body", "font-family", "times new roman");
        styles.LoadTagStyle("body", "font-size", "10px");
        document.NewPage();
        objects = iTextSharp.text.html.simpleparser.
        HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);
        for (int k = 0; k < objects.Count; k++)
        {
            document.Add((IElement)objects[k]);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        document.Close();
        Response.Write(strPDFpath);
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "attachment; filename=myFileName.pdf");
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(strPDFpath);
        Response.Flush();
        Response.Close();
        File.Delete(strPDFpath);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 2019-01-07
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    相关资源
    最近更新 更多