Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7609915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:17:35+00:00 2026-05-31T01:17:35+00:00

MVC3 VB.NET application using Itextsharp. I have a section of code that generates a

  • 0

MVC3 VB.NET application using Itextsharp. I have a section of code that generates a pdf file everything looks great but I would like to alternate the line colors in that pdf file between 2 color so that the values are easy to follow for the person looking at it. Is there a way to set the background color of a whole line based on font size to a set color? A function I would be using this in is below:

    For Each _reg_ In _reg
                Dim _registrant As reg_info = _reg_
                If y_line1 <= 30 Then
                    doc.NewPage()
                    _Page = _Page + 1
                    y_line1 = 670
                End If

                If y_line1 = 670 Then
                    cb.BeginText()
                    cb.SetFontAndSize(BF_Times, 6)
                    cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _datePrinted + "  " + _timePrinted, 500, 770, 0)
                    cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, "Page Number" + " " + _Page, 600, 770, 0)
                    cb.SetFontAndSize(BF_Times, 8)
                    cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, _reportHead + " Overrides ", 304, 720, 0)
                    cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "First Name", 20, 700, 0)
                    cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Last Name", 80, 700, 0)
                    cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Last Four", 160, 700, 0)
                    cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Email Address", 300, 700, 0)

                    cb.EndText()
                End If

                cb.BeginText()
                cb.SetFontAndSize(BF_Times, 8)
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _registrant.first_name, 20, y_line1, 0)
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _registrant.last_name, 80, y_line1, 0)
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _registrant.last_four_social, 160, y_line1, 0)
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, _registrant.email, 300, y_line1, 0)
                _total += 1
                cb.EndText()
                y_line1 = y_line1 - 15
            Next

I thought about just setting the background color of the line by using the y_line1 and using a modulus to determine if the color should be grey or white. But I have found no code samples anywhere about how to set a whole line background color.. Any ideas????

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:17:36+00:00Added an answer on May 31, 2026 at 1:17 am

    There is no concept of “background color” in the PDF spec in relation to text. Anything that looks like a background color, even a table, is just text drawn on top of a rectangle (or some other shape).

    To draw a rectangle you just call the Rectangle method on your PdfContentByte object. It takes a lower left x,y and a width and a height. The color is determined by a previous call to one of the color fills such as SetColorFill().

    When working with the raw canvas its recommended that you also use SaveState() and RestoreState(). Since the fill commands are shared between objects but mean different things these can help avoid confusion. SaveState() sets a flag allowing you to undo all graphics state changes when you call RestoreState().

    The code below is a full working VB.Net 2010 WinForms app targeting iTextSharp 5.1.2.0 that shows off the above. It creates a sample file on the desktop with a line of text repeated 7 times. Each line toggles back and forth between two background colors. Additionally it draws a stroke around the line of text to simulate a border.

    Option Strict On
    Option Explicit On
    
    Imports System.IO
    Imports iTextSharp.text
    Imports iTextSharp.text.pdf
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ''//Test file that we'll create
            Dim TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TestFile.pdf")
            ''//Test String that we'll repeat
            Dim TestString = "It was the best of times..."
            ''//Create an array of our test string
            Dim TestArray = {TestString, TestString, TestString, TestString, TestString, TestString, TestString}
    
            ''//Create our generic font
            Dim BF_Times = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED)
    
            ''//Standard PDF setup, change as needed for your stream type
            Using FS As New FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None)
                Using Doc As New Document(PageSize.LETTER)
                    Using writer = PdfWriter.GetInstance(Doc, FS)
                        Doc.Open()
    
                        ''//Grab the raw content object
                        Dim cb = writer.DirectContent
                        ''//Set our starter Y coordinate
                        Dim y = 670
                        ''//Loop through our string collection
                        For I = 0 To (TestArray.Count - 1)
                            ''//Store the current graphics state so that we can unwind it later
                            cb.SaveState()
                            ''//Set the fill color based on eve/odd
                            cb.SetColorFill(If(I Mod 2 = 0, BaseColor.GREEN, BaseColor.BLUE))
                            ''//Optional, set a border
                            cb.SetColorStroke(BaseColor.BLACK)
                            ''//Draw a rectangle. NOTE: I'm subtracting 5 from the y to account for padding
                            cb.Rectangle(0, y - 5, Doc.PageSize.Width, 15)
                            ''//Draw the rectangle with a border. NOTE: Use cb.Fill() to draw without the border
                            cb.FillStroke()
                            ''//Unwind the graphics state
                            cb.RestoreState()
    
                            ''//Flag to begin text
                            cb.BeginText()
                            ''//Set the font
                            cb.SetFontAndSize(BF_Times, 6)
                            ''//Write some text
                            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, TestArray(I), 0, y, 0)
                            ''//Done writing text
                            cb.EndText()
    
                            ''//Decrease the y accordingly
                            y -= 15
                        Next
    
    
                        Doc.Close()
                    End Using
                End Using
            End Using
    
            Me.Close()
        End Sub
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

MVC3 vb.net Application using built in session management. I have an mvc3 application that
I have develop small Asp.net MVC3 application using Telerik rad Controls with in that
I'm writing an ASP.net MVC3 application using Entity Framework Code First and SqlCe4. I
Here's the issue at hand: I have developed an ASP.NET MVC3 application using Razor.
i have develop a small mvc3 application using Telerik Extension for asp.net mvc3 in
I am currently refactoring my code for a web application developed using ASP.NET MVC3
I am developing an application using .Net MVC3 that executes some Javascript through a
I have created a web application using ASP.NET MVC3 in Visual studio (no SQL
MVC3 VB.NET Application. I have multiple file upload boxes on a form in my
I am using a proprietary IoC mechanism in my asp.net mvc3 application (on IIS7)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.