iText pdf library
Website search

How to add an x-offset to a text pattern with every x-step?

I am creating a text pattern within a rectangle with the following snippet:

var canvas = m_writer.DirectContent;
float fillTextSize = 6.0f;
string filltext = "this is the fill text!";
float filltextWidth = m_dejavuMonoBold.GetWidthPoint(filltext, fillTextSize);
PdfPatternPainter pattern = canvas.CreatePattern(px, py, filltextWidth, fillTextSize);
pattern.SetTextMatrix(0, 0);
pattern.SetRGBColorStroke(190, 190, 190);
pattern.SetRGBColorFill(190, 190, 190);
pattern.SetFontAndSize(m_dejavuMonoBold, 6.0f);
canvas.Rectangle(px, py, pw, ph);
The fill text is repeated regularly. I want to add with every printed line of the pattern to add a horizontal offset. Is there a way to increase the xStep parameter of the pattern?

I am looking for a pattern like this:

|this is the fill text! this is the fill text! this|
|his is the fill text! this is the fill text! this |
|is is the fill text! this is the fill text! this i|
|s is the fill text! this is the fill text! this is|
| is the fill text! this is the fill text! this is |
Posted on StackOverflow on Jul 7, 2015 by Code Clown

If I understand your question correctly, you need something like this: text_pattern.pdf

Text pattern example
Text pattern example

We are using tiled patterns and the problem you experience is that all tiles are regular. If you want an irregular pattern, you need to use a workaround. Such a workaround is shown in the TextPattern example.

In this example, I create the pattern like this:

// This corresponds with what you have
PdfContentByte canvas = writer.getDirectContent();
BaseFont bf = BaseFont.createFont();
String filltext = "this is the fill text! ";
float filltextWidth = bf.getWidthPoint(filltext, 6);
// I create a bigger "tile"
PdfPatternPainter pattern = canvas.createPattern(filltextWidth, 60, filltextWidth, 60);
pattern.setFontAndSize(bf, 6.0f);
// I start with an X offset of 0
float x = 0;
// I add 6 rows of text
// The font is 6, so I used a leading of 10:
for (float y = 0; y 

As far as I know, adding the text more than once to the pattern, is the only way to achieve the type of pattern you require.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now