2013-08-29 53 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