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

  • SEARCH
  • Home
  • 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 6652923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:12:08+00:00 2026-05-26T01:12:08+00:00

Is there way to set gradient background to pdfcell or paragraph? Or do I

  • 0

Is there way to set gradient background to pdfcell or paragraph? Or do I have to use image?

  • 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-26T01:12:08+00:00Added an answer on May 26, 2026 at 1:12 am

    Yes, iText and iTextSharp support gradient colors. The PdfShading object has several static methods that create different types of PdfShading objects for you. The two that you are probably most interested in are SimpleAxial and SimpleRadial. There’s three others named Type1, Type2 and Type3 that I haven’t explored yet.

    Once you have a PdfShading object you can create a PdfShadingPattern directly from it and once you have that you can create a ShadingColor from it. ShadingColor is ultimately derived from BaseColor so you should be able to use it wherever that’s used. In your case you want to assign it to a BackgroundColor.

    Below is a complete working WinForms app targeting iTextSharp 5.1.1.0 that shows created a table with two columns, each with their own gradient background colors.

    NOTE: The (x,y) coordinates of the PdfShading static methods are document-level and not cell-level. What this means is that you might not be able to re-use PdfShading ojbects depending on the gradient’s size. After this sample below I’ll show you how to overcome this limitation using cell events.

    using System;
    using System.Text;
    using System.Windows.Forms;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //Test file name
                string TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
    
                //Standard iTextSharp setup
                using (FileStream fs = new FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    using (Document doc = new Document(PageSize.LETTER))
                    {
                        using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                        {
                            //Open the document for writing
                            doc.Open();
    
                            //Create a shading object. The (x,y)'s appear to be document-level instead of cell-level so they need to be played with
                            PdfShading shading = PdfShading.SimpleAxial(w, 0, 700, 300, 700, BaseColor.BLUE, BaseColor.RED);
    
                            //Create a pattern from our shading object
                            PdfShadingPattern pattern = new PdfShadingPattern(shading);
    
                            //Create a color from our patter
                            ShadingColor color = new ShadingColor(pattern);
    
                            //Create a standard two column table
                            PdfPTable t = new PdfPTable(2);
    
                            //Add a text cell setting the background color through object initialization
                            t.AddCell(new PdfPCell(new Phrase("Hello")) { BackgroundColor = color });
    
                            //Add another cell with everything inline. Notice that the (x,y)'s have been updated
                            t.AddCell(new PdfPCell(new Phrase("World")) { BackgroundColor = new ShadingColor(new PdfShadingPattern(PdfShading.SimpleAxial(w, 400, 700, 600, 700, BaseColor.PINK, BaseColor.CYAN))) });
    
    
    
                            //Add the table to the document
                            doc.Add(t);
    
                            //Close the document
                            doc.Close();
                        }
                    }
                }
    
                this.Close();
            }
    
        }
    }
    

    Example 2

    As noted above, the method above uses document-level position which often isn’t good enough. To overcome this you need to use cell-level positioning and to do that you need to use cell events because cell positions aren’t known until the table itself is rendered. To use a cell event you need to create a new class that implements IPdfPCellEvent and handle the CellLayout method. Below is updated code that does all of this:

    using System;
    using System.Text;
    using System.Windows.Forms;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //Test file name
                string TestFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
    
                //Standard iTextSharp setup
                using (FileStream fs = new FileStream(TestFile, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    using (Document doc = new Document(PageSize.LETTER))
                    {
                        using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                        {
                            //Open the document for writing
                            doc.Open();
    
                            //Create a standard two column table
                            PdfPTable t = new PdfPTable(2);
    
                            //Create an instance of our custom cell event class, passing in our main writer which is needed by the PdfShading object
                            var CE = new GradientBackgroundEvent(w);
    
                            //Set the default cell's event to our handler
                            t.DefaultCell.CellEvent = CE;
    
                            //Add cells normally
                            t.AddCell("Hello");
                            t.AddCell("World");
    
    
                            //Add the table to the document
                            doc.Add(t);
    
                            //Close the document
                            doc.Close();
                        }
                    }
                }
    
                this.Close();
            }
    
            public class GradientBackgroundEvent : IPdfPCellEvent
            {
                //Holds pointer to main PdfWriter object
                private PdfWriter w;
    
                //Constructor
                public GradientBackgroundEvent(PdfWriter w)
                {
                    this.w = w;
                }
    
                public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases)
                {
                    //Create a shading object with cell-specific coords
                    PdfShading shading = PdfShading.SimpleAxial(w, position.Left, position.Bottom, position.Right, position.Top, BaseColor.BLUE, BaseColor.RED);
    
                    //Create a pattern from our shading object
                    PdfShadingPattern pattern = new PdfShadingPattern(shading);
    
                    //Create a color from our patter
                    ShadingColor color = new ShadingColor(pattern);
    
                    //Get the background canvas. NOTE, If using an older version of iTextSharp (4.x) you might need to get the canvas in a different way
                    PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
    
                    //Set the background color of the given rectable to our shading pattern
                    position.BackgroundColor = color;
    
                    //Fill the rectangle
                    cb.Rectangle(position);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to set something as global in a class and have
Is there a way set Subversive to use --ignore-externals on future updates for a
Is there a way to set the background color of a highlighted row of
I know that the best way to make a gradient image is to have
Is there a way set flags on a per-file basis with automake? In particular,
Is there any way to set the same icon to all my forms without
Is there a way to set a different value for service startup timeout per
Is there a way to set the StartPosition of a Windows Forms form using
Is there a way to set the initial path for the FileUpload widget in
Is there a way to set the default buffer size for JSPs in Tomcat?

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.