0

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

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

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

4

1 回答 1

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
于 2013-04-09T13:52:57.863 回答