Is there way to set gradient background to pdfcell or paragraph? Or do I have to use image?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Yes, iText and iTextSharp support gradient colors. The
PdfShadingobject has several static methods that create different types ofPdfShadingobjects for you. The two that you are probably most interested in areSimpleAxialandSimpleRadial. There’s three others namedType1,Type2andType3that I haven’t explored yet.Once you have a
PdfShadingobject you can create aPdfShadingPatterndirectly from it and once you have that you can create aShadingColorfrom it.ShadingColoris ultimately derived fromBaseColorso you should be able to use it wherever that’s used. In your case you want to assign it to aBackgroundColor.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
PdfShadingstatic methods are document-level and not cell-level. What this means is that you might not be able to re-usePdfShadingojbects depending on the gradient’s size. After this sample below I’ll show you how to overcome this limitation using cell events.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
IPdfPCellEventand handle theCellLayoutmethod. Below is updated code that does all of this: