2012-06-11 102 views
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>&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問題。它似乎是空的,就是全部。任何幫助將不勝感激。

回答

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和我會很好。但那是另一個問題。

相關問題