Results 1 to 4 of 4

Thread: Problem with backgroundcolor for iTextSharp ex in VB.

  1. #1
    siraero is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2012
    Posts
    32
    Reputation
    19

    Problem with backgroundcolor for iTextSharp ex in VB.

    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
    Code:
    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
    Code:
    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)
    Code:
    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))
                cell.BackgroundColor = New Color(System.Drawing.ColorTranslator.FromHtml("#008000"))
                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
                            cell.BackgroundColor = New Color(System.Drawing.ColorTranslator.FromHtml("#C2D69B"))
                        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

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,209
    Reputation
    2369
    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"))

  3. #3
    siraero is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2012
    Posts
    32
    Reputation
    19
    Quote Originally Posted by JohnH View Post
    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
    Code:
            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..

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,209
    Reputation
    2369
    Quote Originally Posted by siraero View Post
    problem now is my db connection
    Try this forum: MS Access

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking