Home > Software engineering >  Repeating watermark text
Repeating watermark text

Time:09-25

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 enter image description here How can I make the text shown in tile mode regardless of the font size and length of text?

CodePudding user response:

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:

Interleaved watermark created with PDF4NET

Disclaimer: I work for the company that develops the PDF4NET library.

  • Related