0
使用iTextSharp將生成的html頁面轉換爲pdf,我無法從asp:DataGrid創建的表格顯示在PDF中。頁面的HTML輸出爲DataGrid看起來是這樣的:iTextSharp asp:DataGrid沒有顯示在PDF中
<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> </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 </td>
</tr><tr>
<td>Total</td>
<td> </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
問題。它似乎是空的,就是全部。任何幫助將不勝感激。