You create the document using objects such as Paragraph
s, PdfPTable
s, and so on. You draw the rectangles using PdfContentByte
methods. You obtain a PdfContentByte
instance from a PdfWriter
like this:
writer.getDirectContentUnder(); // Java
or
writer.DirectContentUnder; // C#
By using getDirectContentUnder()
instead of getDirectContent()
, the rectangles are drawn under the Paragraph
s, PdfPTable
s, and so on.
Your main problem is keeping track of the coordinates: you need to know the coordinate of the lower-left corner and of the upper-right corner.
Drawing the background for a full page is a no-brainer. I've answered this question yesterday: How to draw border for whole pdf pages using iText library 5.5.2
Granted, in that answer I defined a border color for the rectangle because the OP only needed a red border on each page. You need to define the fill color of the rectangle instead of the border.
By examining the answer to yesterday's question, you'll discover the concept of page events. You'll also discover other page event methods, such as onParagraph()
and onParagraphEnd()
. These methods receive the Y coordinate of the start of each paragraph and the end of each paragraph. You can use these coordinates to draw rectangles in the page event.
To solve your problem, you would add a BaseColor
member variable, a variable that keeps track of the initial Y value, and so on. It will require some programming, but with all the mechanisms explained in my answer, you should be able to meet your requirement.