leostiw
leostiw

Reputation: 1155

Background pattern with Itext

I am generating a document wiht itext.

My iText Version is: itext-2.0.8

I need following final product: enter image description here

I have a PDF Table with 2 Columns.

I added a background color for each cell. But this is the outcome:

enter image description here

Is it possible to get the pattern from the first picture?

Some come sample:

private static Color firstBGColColor = new Color(255,222,166);
  ...
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100);
table.setWidths(new float[]{160,400});
PdfPCell cell;
cell = new PdfPCell();
cell.setPaddingLeft(7);
cell.setPaddingTop(10);
cell.setBorder(Rectangle.NO_BORDER);
cell.setBackgroundColor(firstBGColColor);

Upvotes: 0

Views: 1251

Answers (1)

mkl
mkl

Reputation: 96029

I have not seriously worked with such an ancient iText version and, therefore, can only point to more current code. A quick look at the svn annotate results seems to indicate, though, that most of the central functionality has been present in iText for a long time. Thus, this code should at least show you how to get started.

PDF knows the concept of a tiling pattern, confer section 8.7.3 Tiling Patterns of ISO 32000-1, which should be what you need to create the proper background. iText supports such tiling patterns as demonstrated in the example TilingPatternColor.java using a helper method from DeviceColor.java. An excerpt:

PdfContentByte canvas = writer.getDirectContent();

PdfPatternPainter square = canvas.createPattern(15, 15);
square.setColorFill(new BaseColor(0xFF, 0xFF, 0x00));
square.setColorStroke(new BaseColor(0xFF, 0x00, 0x00));
square.rectangle(5, 5, 5, 5);
square.fillStroke();

Having defined a PdfPatternPainter like this, you can generate a color instance from it as new PatternColor(square) and use this color.

The samples uses it like this:

canvas.saveState();
canvas.setColorFill(new PatternColor(square));
canvas.rectangle(36, 696, 126, 126);
canvas.fillStroke();
canvas.restoreState();

You of course have to design your PdfPatternPainter differently. As it essentially is a PdfTemplate, though, you have all the means required to create any tiling pattern you want.

Upvotes: 1

Related Questions