GridView控件的TemplateField使用iTextSharp的PDF [英] Gridview Templatefield to PDF using iTextSharp

查看:132
本文介绍了GridView控件的TemplateField使用iTextSharp的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,我敢转换为与iTextSharp的PDF文件。我一直在用下面的方法沿着这帖子

I have a gridview which I'm converting to a PDF with iTextSharp. I've been following along with the method in this post.

由于我使用的VB.net我已经改变了所有的C#code以上,但我一直有一个线路问题。

Since I'm using VB.net I've changed all the C# code over but I've been having problems with one line.

if (gvReport.Columns[columnNo] is TemplateField)

我不知道怎么写这条线在VB.net和我在GridView中有一些模板列(格式化)他们出现在PDF中为空白。

I don't know how to write this line in VB.net and as I have a few templatefields in my gridview (for formatting) they show up as blank in the PDF.

如果任何人有任何想法,这将是极大的AP preciated。

If anyone has any ideas it would be greatly appreciated.

推荐答案

您可以随时使用这个网站:的http:// converter.telerik.com/

You can always use this site: http://converter.telerik.com/

If TypeOf gvReport.Columns(columnNo) Is TemplateField Then

在这里其实是你的整个方法:

In fact, here is that whole method for you:

Protected Sub ExportToPDF(gvReport As GridView, LandScape As Boolean)
Dim noOfColumns As Integer = 0, noOfRows As Integer = 0
Dim tbl As DataTable = Nothing

If gvReport.AutoGenerateColumns Then
    tbl = TryCast(gvReport.DataSource, DataTable)
    ' Gets the DataSource of the GridView Control.
    noOfColumns = tbl.Columns.Count
    noOfRows = tbl.Rows.Count
Else
    noOfColumns = gvReport.Columns.Count
    noOfRows = gvReport.Rows.Count
End If

Dim HeaderTextSize As Single = 8
Dim ReportNameSize As Single = 10
Dim ReportTextSize As Single = 8
Dim ApplicationNameSize As Single = 7

' Creates a PDF document

Dim document As Document = Nothing
If LandScape = True Then
    ' Sets the document to A4 size and rotates it so that the     orientation of the page is Landscape.
    document = New Document(PageSize.A4.Rotate(), 0, 0, 15, 5)
Else
    document = New Document(PageSize.A4, 0, 0, 15, 5)
End If

' Creates a PdfPTable with column count of the table equal to no of columns of the gridview or gridview datasource.
Dim mainTable As New iTextSharp.text.pdf.PdfPTable(noOfColumns)

' Sets the first 4 rows of the table as the header rows which will be repeated in all the pages.
mainTable.HeaderRows = 4

' Creates a PdfPTable with 2 columns to hold the header in the exported PDF.
Dim headerTable As New iTextSharp.text.pdf.PdfPTable(2)

' Creates a phrase to hold the application name at the left hand side of the header.
Dim phApplicationName As New Phrase("Sample Application", FontFactory.GetFont("Arial", ApplicationNameSize, iTextSharp.text.Font.NORMAL))

' Creates a PdfPCell which accepts a phrase as a parameter.
Dim clApplicationName As New PdfPCell(phApplicationName)
' Sets the border of the cell to zero.
clApplicationName.Border = PdfPCell.NO_BORDER
' Sets the Horizontal Alignment of the PdfPCell to left.
clApplicationName.HorizontalAlignment = Element.ALIGN_LEFT

' Creates a phrase to show the current date at the right hand side of the header.
Dim phDate As New Phrase(DateTime.Now.[Date].ToString("dd/MM/yyyy"), FontFactory.GetFont("Arial", ApplicationNameSize, iTextSharp.text.Font.NORMAL))

' Creates a PdfPCell which accepts the date phrase as a parameter.
Dim clDate As New PdfPCell(phDate)
' Sets the Horizontal Alignment of the PdfPCell to right.
clDate.HorizontalAlignment = Element.ALIGN_RIGHT
' Sets the border of the cell to zero.
clDate.Border = PdfPCell.NO_BORDER

' Adds the cell which holds the application name to the headerTable.
headerTable.AddCell(clApplicationName)
' Adds the cell which holds the date to the headerTable.
headerTable.AddCell(clDate)
' Sets the border of the headerTable to zero.
headerTable.DefaultCell.Border = PdfPCell.NO_BORDER

' Creates a PdfPCell that accepts the headerTable as a parameter and then adds that cell to the main PdfPTable.
Dim cellHeader As New PdfPCell(headerTable)
cellHeader.Border = PdfPCell.NO_BORDER
' Sets the column span of the header cell to noOfColumns.
cellHeader.Colspan = noOfColumns
' Adds the above header cell to the table.
mainTable.AddCell(cellHeader)

' Creates a phrase which holds the file name.
Dim phHeader As New Phrase("Sample Export", FontFactory.GetFont("Arial", ReportNameSize, iTextSharp.text.Font.BOLD))
Dim clHeader As New PdfPCell(phHeader)
clHeader.Colspan = noOfColumns
clHeader.Border = PdfPCell.NO_BORDER
clHeader.HorizontalAlignment = Element.ALIGN_CENTER
mainTable.AddCell(clHeader)

' Creates a phrase for a new line.
Dim phSpace As New Phrase(vbLf)
Dim clSpace As New PdfPCell(phSpace)
clSpace.Border = PdfPCell.NO_BORDER
clSpace.Colspan = noOfColumns
mainTable.AddCell(clSpace)

' Sets the gridview column names as table headers.
For i As Integer = 0 To noOfColumns - 1
    Dim ph As Phrase = Nothing

    If gvReport.AutoGenerateColumns Then
        ph = New Phrase(tbl.Columns(i).ColumnName, FontFactory.GetFont("Arial", HeaderTextSize, iTextSharp.text.Font.BOLD))
    Else
        ph = New Phrase(gvReport.Columns(i).HeaderText, FontFactory.GetFont("Arial", HeaderTextSize, iTextSharp.text.Font.BOLD))
    End If

    mainTable.AddCell(ph)
Next

' Reads the gridview rows and adds them to the mainTable
For rowNo As Integer = 0 To noOfRows - 1
    For columnNo As Integer = 0 To noOfColumns - 1
        If gvReport.AutoGenerateColumns Then
            Dim s As String = gvReport.Rows(rowNo).Cells(columnNo).Text.Trim()
            Dim ph As New Phrase(s, FontFactory.GetFont("Arial", ReportTextSize, iTextSharp.text.Font.NORMAL))
            mainTable.AddCell(ph)
        Else
            If TypeOf gvReport.Columns(columnNo) Is TemplateField Then
                Dim lc As DataBoundLiteralControl = TryCast(gvReport.Rows(rowNo).Cells(columnNo).Controls(0), DataBoundLiteralControl)
                Dim s As String = lc.Text.Trim()
                Dim ph As New Phrase(s, FontFactory.GetFont("Arial", ReportTextSize, iTextSharp.text.Font.NORMAL))
                mainTable.AddCell(ph)
            Else
                Dim s As String = gvReport.Rows(rowNo).Cells(columnNo).Text.Trim()
                Dim ph As New Phrase(s, FontFactory.GetFont("Arial", ReportTextSize, iTextSharp.text.Font.NORMAL))
                mainTable.AddCell(ph)
            End If
        End If
    Next

    ' Tells the mainTable to complete the row even if any cell is left incomplete.
    mainTable.CompleteRow()
Next

' Gets the instance of the document created and writes it to the output stream of the Response object.
PdfWriter.GetInstance(document, Response.OutputStream)

' Creates a footer for the PDF document.
Dim pdfFooter As New HeaderFooter(New Phrase(), True)
pdfFooter.Alignment = Element.ALIGN_CENTER
pdfFooter.Border = iTextSharp.text.Rectangle.NO_BORDER

' Sets the document footer to pdfFooter.
document.Footer = pdfFooter
' Opens the document.
document.Open()
' Adds the mainTable to the document.
document.Add(mainTable)
' Closes the document.
document.Close()

Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment; filename= SampleExport.pdf")
Response.[End]()

结束小组

这篇关于GridView控件的TemplateField使用iTextSharp的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆