How to set the background color of a PDF document using iTextSharp

You can create a Rectangle object and set its BackgroundColor property. Use your Rectangle to initialize your Document.

Imports System
Imports System.IO

Imports iTextSharp.text
Imports iTextSharp.text.pdf

Public Class Chap0102

    Public Shared Sub Main()
        Console.WriteLine("Chapter 1 example 2: PageSize")

        ' step 1: creation of a document-object
        Dim pageSize As New Rectangle(144, 720)
        pageSize.BackgroundColor = New Color(&Hff, &Hff, &Hde) 
     '// pageSize.BackgroundColor.BackgroundColor = BaseColor.WHITE
        Dim document As New Document(pageSize)

        Try

            ' step 2:
            ' we create a writer that listens to the document
            ' and directs a PDF-stream to a file

            PdfWriter.getInstance(document, New FileStream("Chap0102.pdf", FileMode.Create))

            ' step 3: we open the document
            document.Open()

            ' step 4: we Add some paragraphs to the document
            For i As Integer = 0 To 4
                document.Add(New Paragraph("Hello World"))

            Next
        Catch de As DocumentException
            Console.[Error].WriteLine(de.Message)
        Catch ioe As IOException
            Console.[Error].WriteLine(ioe.Message)
        End Try

        ' step 5: we close the document
        document.Close()
    End Sub
End Class

Give it a try.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.