iTextSharp asp:DataGrid不会以PDF格式显示

时间:2012-06-11 17:40:28

标签: datagrid itextsharp

使用iTextSharp将生成的html页面转换为pdf,我无法将从asp:DataGrid创建的表格显示在PDF中。数据网格的页面HTML输出如下所示:

<p>Purchases:</p>
    <table rules="all" id="dg_Purchase" style="font-size: smaller; border-collapse: collapse;" border="1" cellspacing="0">
    <tbody><tr>
        <td>Lot #</td>
        <td>&nbsp;</td>
        <td>Title</td>
        <td align="right">Price</td>
        <td align="right">Buyer's Premium</td>
        <td align="right">Tax</td>
        <td align="right">Sub Total</td>
    </tr><tr>
        <td>118</td>
        <td><input id="dg_Purchase_ctl02_CheckBox1" name="dg_Purchase$ctl02$CheckBox1" type="checkbox"></td>
        <td>Yih-Wen Kuo (1959, Taiwan)</td>
        <td align="right">$1,900.00</td>
        <td align="right">$332.50</td>
        <td align="right">$0.00</td>
        <td align="right">$2,232.50</td>
    </tr><tr>
        <td>119</td>
        <td><input id="dg_Purchase_ctl03_CheckBox1" name="dg_Purchase$ctl03$CheckBox1" type="checkbox"></td>
        <td>Yih-Wen Kuo (1959, Taiwan)</td>
        <td align="right">$1,800.00</td>
        <td align="right">$315.00</td>
        <td align="right">$0.00</td>
        <td align="right">$2,115.00</td>
    </tr>
</tbody></table>

<p>Payments:</p>
    <table rules="all" id="dg_Payment" style="font-size: smaller; border-collapse: collapse;" border="1" cellspacing="0">
    <tbody><tr>
        <td style="width: 120px;">Payment Method</td>
        <td>Date</td>
        <td align="right">Amount&nbsp;&nbsp;</td>
    </tr><tr>
        <td>Total</td>
        <td>&nbsp;</td>
        <td align="right">$0.00</td>
    </tr>
</tbody></table>

查看HTML页面时,表格看起来应该如何。但是当我生成PDF时,表格(或者更确切地说,表格中的所有内容)都没有显示。只有边框可见,几乎只是每张桌子的2 px直线。

所以在我的正常脚本中,在整个页面加载完毕后,我做的最后一件事是:

    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter writer = new HtmlTextWriter(stringWriter);
    Render(writer);

iTextSharp的功能如下:

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();
}

同样,页面上以编程方式设置的所有内容都会显示在数据网格的PDF EXCEPT中。离开边界,似乎不是width问题。它似乎是空的,就是全部。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

好吧,我找不到押韵或理由为什么这不起作用,所以我走另一条路。我决定将生成HTML代码的页面作为辅助页面,并让生成PDF的页面引用它。我最终得到了:

String address = "http://address.com/admin/Invoice2.aspx?sale_id=" + sale_id + "&bidder_number=" + bidder_number;

using (WebClient wc = new WebClient())
{
    string content = wc.DownloadString(address);
    CreatePDFDocument(content);
}

在我的页面加载方法中,只保留了其他两个函数:

public void CreatePDFDocument(string strHtml)
{

    string strFileName = HttpContext.Current.Server.MapPath("invoice.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();
}

现在我只需要将表格的样式转移到PDF,我就会很好。但这是另一个问题。