How to add Watermark when export to PDF or print

Posted by: hyong on 4 August 2017, 3:06 pm EST

  • Posted 4 August 2017, 3:06 pm EST

    Hi


    I have a requirement to add watermark dynamically when a report is printed or exported to PDF,


    On preview window (in active report native format), the report does not have any watermark but


    watermark is added on some pages on the report when it is printed or exported to PDF.


    I have tried to use DrawPicture but could not make the image background transparent. Also I


    have tried to use DrawText on canvas to simulate watermark but the text printed on pages


    overlaped the report contents (the text printed on page was large text and cannot make the text


    50% transparent). Is there anyway to add watermark after a report is produced?


    My report is big so I cannot re-generate the report for a performance reason.


     


    Thanks in advance


    Hyong

  • Replied 4 August 2017, 3:06 pm EST

    Hello ,


    I would recommend to create a temporary report with "watermark" and overlay it with produced report.


    Here is a sample of code:


    Dim ar_temp As New ActiveReport
    Dim i
    For i = 0 To arMain.Pages.Count - 1
        ar_temp.Pages.Add
        ar_temp.Pages(i).DrawText "TEST", 1000, 1000, 1000, 1000
        ar_temp.Pages(i).Overlay arMain.Pages(i)
    Next
    ar_temp.Pages.Commit


    Dim pdfe As New ActiveReportsPDFExport.ARExportPDF
    pdfe.FileName = "c:\test.pdf"
    pdfe.SemiDelimitedNeverEmbedFonts = ""
    pdfe.Export ar_temp.Pages


    In this case the watermark text will be placed under data.


    Sergey Romanov.

  • Replied 4 August 2017, 3:06 pm EST

    Thanks Sergey. Your code works well but I am loosing TOC. To restore the TOC


    on the report, I have tried to use the following codes but the added TOC on my report


    points to the wrong position.


          For idx = 0 To arMain.TOC.Count - 1
             ar_temp.TOC.Add arMain.TOC(idx).Name
             ar_temp.TOC(idx).pageNumber = arMain.TOC(idx).pageNumber
             ar_temp.TOC(idx).PageOffset = arMain.TOC(idx).PageOffset
          Next


    Is there any problem on my codes?


    Hyong

  • Replied 4 August 2017, 3:06 pm EST

    Hello ,


    Unfortunately, the code you use won't work since the TOC items can't be added to the rendered report.


    The following workaround will keep the original TOC.


    Dim i, w, h
        h = ar.Pages(0).Height
        w = ar.Pages(0).Width
    Dim bc As Canvas
    For i = 0 To ar.Pages.Count - 1
        Set bc = New Canvas
        bc.Overlay ar.Pages(i)
        ar.Pages(i).Clear
        ar.Pages(i).Height = h
        ar.Pages(i).Width = w
        ar.Pages(i).Font.Size = 36
        ar.Pages(i).Font.Bold = True
        ar.Pages(i).ForeColor = &H8000000F
        ar.Pages(i).DrawText "TEST", 3000, 3000, 4000, 4000
        ar.Pages(i).Overlay bc
    Next


    ar.Pages.Commit


    Dim pdfe As New ActiveReportsPDFExport.ARExportPDF
    pdfe.FileName = "c:\test.pdf"
    pdfe.SemiDelimitedNeverEmbedFonts = ""
    pdfe.Export ar.Pages


    Sergey Romanov.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels