Problem with backgroundcolor for iTextSharp ex in VB.

siraero

Active member
Joined
Jan 21, 2012
Messages
32
Programming Experience
Beginner
Hi

Im making a page that can get some info from a DB from a gridview and then print it to a PDF file.
But im having problems with the background color.

If i use this line of code
VB.NET:
cell.BackgroundColor = New Color(System.Drawing.ColorTranslator.FromHtml("#008000"))
Then i get this error
System.Drawing.Color' has no constructors

If i changes it to this code
VB.NET:
cell.BackgroundColor = System.Drawing.ColorTranslator.FromHtml("#008000")
Then i get this error
Value of type 'System.Drawing.Color' cannot be converted to 'iTextSharp.text.BaseColor'.

Hope someone can guide/help me. and sry my english.

My code is: (the error is underline)
VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim cmd As New OleDbCommand()
        Dim myAdapter As New OleDbDataAdapter()
        Dim myDataSet As New DataSet()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "select CustomerID,City,Country from customers"
        cmd.Connection = myAccessConnection
        myAdapter.SelectCommand = cmd

        Try
            myAccessConnection.Open()
            myAdapter.Fill(myDataSet)
            GridView1.EmptyDataText = "No Records Found"
            GridView1.DataSource = myDataSet
            GridView1.DataBind()

        Catch ex As Exception

            Throw ex

        Finally

            myAccessConnection.Close()
            myAccessConnection.Dispose()

        End Try
    End Sub

    Protected Sub btnExportPDF_Click(ByVal sender As Object, ByVal e As EventArgs)
        GridView1.AllowPaging = Convert.ToBoolean(rbPaging.SelectedItem.Value)
        GridView1.DataBind()

        'Create a table
        Dim table As New PdfPTable(GridView1.Columns.Count)
        table.AddCell("textstring")
        Dim celladd As PdfPCell = table.DefaultCell
        celladd.Padding = 5

        'Set the column widths 
        Dim widths As Integer() = New Integer(GridView1.Columns.Count - 1) {}
        For x As Integer = 0 To GridView1.Columns.Count - 1
            widths(x) = CInt(GridView1.Columns(x).ItemStyle.Width.Value)
            Dim cellText As String = Server.HtmlDecode(GridView1.HeaderRow.Cells(x).Text)
            Dim cell As PdfPCell = New PdfPCell(New Phrase(cellText))
            [B][U]cell[/U][/B][B][U].BackgroundColor = New Color(System.Drawing.ColorTranslator.FromHtml("#008000"))[/U][/B]
            table.AddCell(cell)
        Next
        table.SetWidths(widths)

        'Transfer rows from GridView to table
        For i As Integer = 0 To GridView1.Rows.Count - 1
            If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then
                For j As Integer = 0 To GridView1.Columns.Count - 1
                    Dim cellText As String = Server.HtmlDecode(GridView1.Rows(i).Cells(j).Text)
                    Dim cell As PdfPCell = New PdfPCell(New Phrase(cellText))

                    'Set Color of Alternating row
                    If i Mod 2 <> 0 Then
                        [B][U]cell[/U][/B][B][U].BackgroundColor = New Color(System.Drawing.ColorTranslator.FromHtml("#C2D69B"))[/U][/B]
                    End If
                    table.AddCell(cell)
                Next
            End If
        Next
        'Create the PDF Document
        Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
        pdfDoc.Open()
        pdfDoc.Add(table)
        pdfDoc.Close()
        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.Write(pdfDoc)
        Response.End()
    End Sub
    Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        ' Verifies that the control is rendered 
    End Sub
    Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataBind()
    End Sub
 
PdfPCell.BackgroundColor property type is BaseColor. You can create a BaseColor from a Color value, such as that returned from ColorTranslator.FromHtml method.
cell.BackgroundColor = New BaseColor(System.Drawing.ColorTranslator.FromHtml("#008000"))
 
PdfPCell.BackgroundColor property type is BaseColor. You can create a BaseColor from a Color value, such as that returned from ColorTranslator.FromHtml method.
cell.BackgroundColor = New BaseColor(System.Drawing.ColorTranslator.FromHtml("#008000"))

Hi JohnH

Thx for reply.
No errors now thx. only problem now is my db connection about the
VB.NET:
        Finally              myAccessConnection.Close()             myAccessConnection.Dispose()          End Try

that there is no value given to one or more of the of the used paramets.
but thx..
 
Back
Top