Add TextWatermark doesnt show, but PictureWatermark does

Good afternoon.

I’m trying to add a watermark to all the pages in my document.
What I’m doing in the document is:

public void Generate_PDF(byte[] Template, byte[] QrCode)
{
    ComponentInfo.SetLicense("....");

    using (MemoryStream memoryDocStream = new MemoryStream(ssInvoiceTemplate))
    {
        try{
            DocumentModel document = DocumentModel.Load(memoryDocStream);

            //Replaces and stuff
            using (MemoryStream qrCodeStream = new MemoryStream(ssQrCode))
            {
                var picturePlaceholder = document.Content.Find("{QR}").First();
                var qrCodeImage = new Picture(document, qrCodeStream, 100, 100);

                picturePlaceholder.Set(qrCodeImage.Content);

                Section section = document.Sections[0];

                Table Line = ConvertToLineTable(LinesData, document); //Helper method to create and stylize Table

                document.Sections[0].Blocks.Add(Line);

                //add more stuff

                var paginator = document.GetPaginator();

                int numPages = paginator.Pages.Count;

                var destination = new DocumentModel();

                for (int i = 0; i < numPages; i++)
                {
                    DocumentModel page = paginator.Pages[i].ConvertToDocument();
                    Section pageSection = page.Sections[0];
                    var header = pageSection.HeadersFooters.GetOrAdd(HeaderFooterType.HeaderFirst);

                    if (i > 0)
                    {
                        header.Blocks.Add(
                            new Paragraph(page, $"Add Stuff: {value}")
                                { ParagraphFormat = new ParagraphFormat() { Alignment = HorizontalAlignment.Right } }
                        );
                    }

                    //This works
                    var imageWatermark = new PictureWatermark(page, new Picture(page, "Canceledd.png", 275,230));
                    header.Watermark = imageWatermark;
                    imageWatermark.AutoScale();
                    imageWatermark.Washout = true;


                    //This doesnt work
                    var textWatermark = new TextWatermark(page, "Canceled");
                    header.Watermark = textWatermark;
                    textWatermark.SetDiagonal();
                    textWatermark.Color = Color.Red;
                    textWatermark.Semitransparent = true;

                    destination.Import(page, true, true);
                }

                destination.Save("TestTemplateChanged.pdf");
            }
        }
    }
}

Naturally, I test one watermark at a time and not both at the same time.

any particular reason why the text isn’t working?
alternatively, if you could put the watermark above the content instead of below it, that would be great, because the QR image ends up above the watermark in this case.

Hi,

You don’t need to use DocumentModel.GetPaginator() for this.
Instead, try the following:

foreach (var headersFooters in document.Sections.Select(s => s.HeadersFooters))
{
    headersFooters.GetOrAdd(HeaderFooterType.HeaderDefault);

    foreach (var header in headersFooters.Where(hf => hf.IsHeader))
    {
        var textWatermark = new TextWatermark(document, "Canceled");
        header.Watermark = textWatermark;
        textWatermark.SetDiagonal();
        textWatermark.Color = Color.Red;
        textWatermark.Semitransparent = true;
    }
}

document.Save("TestTemplateChanged.pdf");

Does this solve your issue?

Regards,
Mario

I’m using paginator for another reason, I have to include a custom footer on each page that does specific calculations per page, and since it has conditions per page…
But yes, there was no need to complicate things.
This is simple and it works.
Thank you