I'm using PDF4NET to draw watermark on a pdf pages. Now I want to repeat the watermark text all over the page in tile mode. Something like this watermark.
Below is what I do to repeat the watermark text:
var text= string.Concat(Enumerable.Repeat("Sample ", 800));
PDFTextFormatOptions format= new PDFTextFormatOptions();
format.Align = PDFTextAlign.TopLeft;
// -----
// ....
page.Canvas.DrawTextBox(text, fontText, null, redBrush, 0, 0, page.Width, page.Height, format);
How can I make the text shown in tile mode regardless of the font size and length of text?
The expected layout cannot be generated automatically.
The code below shows a possible way to get the layout you need. You can play with X, Y to adjust the output as needed.
PDFFixedDocument document = new PDFFixedDocument();
PDFPage page = document.Pages.Add();
PDFBrush lightGrayBrush = new PDFBrush(PDFRgbColor.LightGray);
PDFStandardFont helvetica = new PDFStandardFont(PDFStandardFontFace.Helvetica, 12);
string watermarkText = "Sample Watermark";
double watermarkTextWidth = PDFTextEngine.MeasureString(watermarkText, helvetica).Width;
double y = 0;
double startX = watermarkTextWidth / 2;
int sign = -1;
while (y < page.Height)
{
double x = startX;
while (x < page.Width)
{
page.Canvas.DrawString(watermarkText, helvetica, lightGrayBrush, x, y);
x = x + watermarkTextWidth + watermarkTextWidth / 4;
}
startX = startX + sign * watermarkTextWidth / 2;
sign = -sign;
y = y + 2 * helvetica.Size;
}
document.Save("InterleavedWatermak.pdf");
The output document is below:
Disclaimer: I work for the company that develops the PDF4NET library.