Add a Text Watermark using Stamper on UWP

108 views
Skip to first unread message

Suren Manawatta

unread,
Feb 13, 2018, 12:29:46 PM2/13/18
to PDFTron PDFNet SDK
I have added a watermark to my pdf using the following code. My actual requirement is to display the watermark when the user tries to print the pdf. (print preview and the printed copy should have the watermark and not
on the original doc on the viewer)

I set the flags ShowsOnScreen(false) and ShowsOnPrint(true) but it displays on the print preview but also display on the viewer as well.
Another issue is this watermark can be selected and do other annotations like highlighting.

Please suggest me a workaround. Either I need to hide the watermark from the viewer and display only on print preview/print
or
Show me a way that watermark displays on the viewer as well but it has to be read-only.



using (Stamper st = new Stamper(StamperSizeType.e_relative_scale, 0.9, 0.9))
{
pdfDoc.InitSecurityHandler();

st.SetAlignment(StamperHorizontalAlignment.e_horizontal_center, StamperVerticalAlignment.e_vertical_center);
st.SetFontColor(new ColorPt(0, 0, 0)); st.SetOpacity(0.3);
st.SetAsBackground(false);
st.ShowsOnScreen(false);

st.SetRotation(-45);
st.ShowsOnPrint(true);

st.SetAsAnnotation(false);

st.StampText(pdfDoc, "If you are reading this\nthis is an even page", new PageSet(1, pdfDoc.GetPageCount()));
}

Ryan

unread,
Feb 19, 2018, 5:26:44 PM2/19/18
to PDFTron PDFNet SDK
What version of PDFNet are you using.

Also, what program are you using to view the PDF, and where the watermark is appearing (what "viewer").

Suren Manawatta

unread,
Mar 7, 2018, 1:56:11 PM3/7/18
to PDFTron PDFNet SDK
This problem solved with the help from PDFTron team.
Please note that PDFDraw class require a separate license from PDFTron. If you already have a licensed version of PDFTron UWP, remove it and go demo to test this solution.

public async Task<PDFDoc> SetWatermark(PDFDoc pdfDoc)
        {
            try
            {
                #region Solution 1
                var text = "Sample Watermark\nSample Watermark";

                pdfDoc.InitSecurityHandler();
                Image img1 = null;

                // Create a dummy document
                using (PDFDoc tempDoc = new PDFDoc())
                using (Stamper st = new Stamper(StamperSizeType.e_relative_scale, 0.9, 0.9))
                {
    // Add a page to the dummy doc.
                    tempDoc.PagePushBack(tempDoc.PageCreate());
                    st.SetAlignment(StamperHorizontalAlignment.e_horizontal_center, StamperVerticalAlignment.e_vertical_center);

                    // Set text color to black
                    st.SetFontColor(new ColorPt(0, 0, 0));

                    // Add a text to the dummy page. This text will be the watermark you going to apply on your original document.
                    st.StampText(tempDoc, text, new PageSet(1, tempDoc.GetPageCount()));
    // Reduce PDF page to minimal size
                    tempDoc.GetPage(1).SetMediaBox(tempDoc.GetPage(1).GetVisibleContentBox());

    // Require a sepaprate license
                    PDFDraw draw = new PDFDraw();
                    // Adjust image output quality
                    draw.SetDPI(100);
                    draw.SetPageTransparent(true);

                    // Export the dummy page's text as an image
                    var pngImageData = await draw.ExportAsStreamAsync(tempDoc.GetPage(1), "PNG");
                    img1 = await Image.CreateAsync(pdfDoc.GetSDFDoc(), pngImageData);
                }

                // Create a new stamper to embed the above created image to the original doc
                using (Stamper st = new Stamper(StamperSizeType.e_relative_scale, 0.9, 0.9))
                {
                    // Adjust final stamp positioning
                    st.SetAlignment(StamperHorizontalAlignment.e_horizontal_center, StamperVerticalAlignment.e_vertical_center);
                    st.SetOpacity(0.3);
                    st.SetAsBackground(false);
                    st.ShowsOnScreen(true);
                    st.SetRotation(-45);
                    st.ShowsOnPrint(true);
                    st.SetAsAnnotation(false); //Make it true if you want to add this watermark to the XFDF file

                    st.StampImage(pdfDoc, img1, new PageSet(1, pdfDoc.GetPageCount()));
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw;
            }
            return pdfDoc; //Retrun the watermark embeded document
        }
Reply all
Reply to author
Forward
0 new messages