2013-08-29 87 views
0

我想解析一個由html表(4列)組成的asp.net面板控件到PD文檔。我已經將頁面大小設置爲A4,並將所有頁邊距設置爲10. 創建PDF時,會有很大的左右邊距。我怎樣才能得到左邊和邊緣賴特?iTextSharp忽略新創建的PDF中的頁邊距

這是所使用的代碼:

Dim strFileName = "CBB_" & lblZoekCriteria.Text & ".pdf" 

Response.ContentType = "application/pdf" 
Response.AddHeader("content-disposition", "attachment;filename=" & strFileName) 
Response.Cache.SetCacheability(HttpCacheability.NoCache) 
Dim sw As New StringWriter() 
Dim hw As New HtmlTextWriter(sw) 
'Me.Page.RenderControl(hw) 
pnlProtestInfo.RenderControl(hw) 

Dim sr As New StringReader(sw.ToString()) 
Dim pdfDoc As New Document(PageSize.A4, 10, 10, 10, 10) 
Dim htmlparser As New HTMLWorker(pdfDoc) 
PdfWriter.GetInstance(pdfDoc, Response.OutputStream) 
pdfDoc.Open() 
htmlparser.Parse(sr) 
pdfDoc.Close() 
Response.Write(pdfDoc) 
Response.[End]() 

回答

1

當你設置你的iText指示向利潤率在這些地區不會畫,僅此而已。你不是告訴iText繪製任何東西的寬度。

如果沒有看到您正在解析的HTML,我無法具體告訴您需要解決什麼問題。然而,下面是一個非常基本的示例,它使用一個設置爲100%寬度的表格,該表格應該可以滿足您的要求。

此外,請記住HTMLWorker是舊的且不受支持,並且只支持少數最基本的HTML和CSS標記和屬性。相反,我們鼓勵你轉移到XMLWorker

''//Output a file to the desktop 
Dim strFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf") 

''//Out very basic sample HTML 
Dim sampleHtml = <table border="1" width="100%" align="center"> 
        <tr> 
         <td>0:0</td> 
         <td>0:1</td> 
        </tr> 
        <tr> 
         <td>1:0</td> 
         <td>1:1</td> 
        </tr> 
       </table> 

''//Standard PDF setup, nothing special 
Using fs As New FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None) 

    ''//Create our document with margins specified 
    Using pdfDoc As New Document(PageSize.A4, 10, 10, 10, 10) 
     Using PdfWriter.GetInstance(pdfDoc, fs) 

      pdfDoc.Open() 

      ''//Parse our HTML into the document 
      Using sr As New StringReader(sampleHtml.ToString()) 
       Using htmlparser As New HTMLWorker(pdfDoc) 
        htmlparser.Parse(sr) 
       End Using 
      End Using 

      pdfDoc.Close() 
     End Using 
    End Using 
End Using