iTextSharp打印GridView - 输出标题宽度

时间:2013-04-09 07:26:13

标签: asp.net itextsharp

我正在使用iTextSharp打印gridview,但在尝试确定列的适当宽度时遇到问题。在我的gridview中我没有设置它,而是让ASP.NET引擎调整宽度(它根据文本的样式动态调整)。

默认情况下,所有列都具有相同的大小,在大多数情况下都是坏的。

如何配置标头的PdfPCell对象,其宽度适合其中的文本?

1 个答案:

答案 0 :(得分:0)

列宽在PdfPTable级别计算,因此您需要在那里进行处理。根据您使用的解析器,有几种方法可以启动它,但最终结果是您需要将HTML解析为iTextSharp对象,然后在将它们提交到PDF之前手动应用您自己的逻辑。下面的代码基于使用(首选)XMLWorker和iTextSharp 5.4.0以及以下示例HTML:

<html>
   <head>
       <title>This is a test</title>
   </head>
   <body>
       <table>
           <thead>
               <tr>
                   <td>First Name</td>
                   <td>Last Name</td>
               </tr>
           </thead>
           <tbody>
               <tr>
                   <td>Bob</td>
                   <td>Dole</td>
               </tr>
           </tbody>
       </table>
   </body>
</html>

首先,您需要实现自己的IElementHandler课程。这将允许您在将元素写入PDF之前捕获元素。

Public Class CustomElementHandler
    Implements IElementHandler
    ''//List of all elements
    Public elements As New List(Of IElement)

    ''//Will get called for each top-level elements
    Public Sub Add(w As IWritable) Implements IElementHandler.Add
        ''//Sanity check
        If (TypeOf w Is WritableElement) Then
            ''//Add the element (which might have sub-elements)
            elements.AddRange(DirectCast(w, WritableElement).Elements)
        End If
    End Sub
End Class

然后你只需要使用上面的处理程序而不是直接写入文档:

''//TODO: Populate this with your HTML
Dim Html = ""
''//Where we're write our PDF to
Dim File1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File1.pdf")
''//Create our PDF, nothing special here
Using FS As New FileStream(File1, FileMode.Create, FileAccess.Write, FileShare.None)
    Using Doc As New Document()
        Using writer = PdfWriter.GetInstance(Doc, FS)
            Doc.Open()

            ''//Create an instance of our handler
            Dim Handler As New CustomElementHandler()
            ''//Bind a StringReader to our text
            Using SR As New StringReader(Html)
                ''//Have the XMLWorker read our HTML and push it to our handler
                XMLWorkerHelper.GetInstance().ParseXHtml(Handler, SR)
            End Using

            ''//Loop through each element that the parser found
            For Each El In Handler.elements
                ''//If the element is a table
                If TypeOf El Is PdfPTable Then
                    ''//Below is just for illustration, change as needed

                    ''//Set the absolute width of the table
                    DirectCast(El, PdfPTable).TotalWidth = 500

                    ''//Set the first column to be 25% and the second column to be 75%
                    DirectCast(El, PdfPTable).SetWidths({0.25, 0.75})

                    ''//Also, just for illustration, turn borders on for each cell so that we can see the actual widths
                    For Each R In DirectCast(El, PdfPTable).Rows
                        For Each C In R.GetCells()
                            C.BorderWidthLeft = 1
                            C.BorderWidthRight = 1
                            C.BorderWidthBottom = 1
                            C.BorderWidthTop = 1
                        Next
                    Next
                End If

                ''//Add the element to the document
                Doc.Add(El)
            Next
            Doc.Close()
        End Using
    End Using
End Using