Back to Blog

    Developer Blog

    How to create Watermarks using Foxit PDF SDK (C++)

    Foxit PDF SDK for Windows

    A watermark is a type of PDF annotation and is widely used in PDF documents. A watermark is a visible embedded overlay on a document consisting of text, a logo, or a copyright notice. The purpose of a watermark is to brand your work and discourage its unauthorized use. Foxit PDF SDK provides APIs to work with watermarks, allowing applications to create, insert, release and remove watermarks.

    Example:

    How to create a text watermark and insert it into the first page

    #include "include/common/fs_common.h"
    #include "include/pdf/fs_pdfdoc.h"
    #include "include/pdf/fs_pdfpage.h"
    #include "include/pdf/fs_watermark.h"
    using namespace foxit;
    using namespace foxit::common;
    using foxit::common::Library;
    using namespace pdf;
    ...
    // Assuming PDFDoc doc has been loaded.
    WatermarkSettings settings;
    settings.flags = WatermarkSettings::e_FlagASPageContents | WatermarkSettings::e_FlagOnTop;
    settings.offset_x = 0;
    settings.offset_y = 0;
    settings.opacity = 90;
    settings.position = common::e_PosTopRight;
    settings.rotation = -45.f;
    settings.scale_x = 1.f;
    settings.scale_y = 1.f;
    WatermarkTextProperties text_properties;
    text_properties.alignment = CommonDefines::e_AlignmentCenter;
    text_properties.color = 0xF68C21;
    text_properties.font_style = WatermarkTextProperties::e_FontStyleNormal;
    text_properties.line_space = 1;
    text_properties.font_size = 12.f;
    text_properties.font = Font(Font::e_StdIDTimesB);
    Watermark watermark(doc, L"Foxit PDF SDK\nwww.foxit.com", text_properties, settings);
    watermark.InsertToPage(doc.GetPage(0));
    // Save document to file
    ...
    

    How to create an image watermark and insert it into the first page

    #include "include/common/fs_common.h"
    #include "include/pdf/fs_pdfdoc.h"
    #include "include/pdf/fs_pdfpage.h"
    #include "include/pdf/fs_watermark.h"
    using namespace foxit;
    using namespace foxit::common;
    using foxit::common::Library;
    using namespace pdf;
    ...
    // Assuming PDFDoc doc has been loaded.
    WatermarkSettings settings;
    settings.flags = WatermarkSettings::e_FlagASPageContents | WatermarkSettings::e_FlagOnTop;
    settings.offset_x = 0.f;
    settings.offset_y = 0.f;
    settings.opacity = 20;
    settings.position = common::e_PosCenter;
    settings.rotation = 0.0f;
    Image image(image_file);
    Bitmap bitmap = image.GetFrameBitmap(0);
    settings.scale_x = page.GetWidth() * 0.618f / bitmap.GetWidth();
    settings.scale_y = settings.scale_x;
    Watermark watermark(doc, image, 0, settings);
    watermark.InsertToPage(doc.GetPage(0));
    // Save document to file.
    ...
    

    How to remove all watermarks from a page

    #include "include/common/fs_common.h"
    #include "include/pdf/fs_pdfdoc.h"
    #include "include/pdf/fs_pdfpage.h"
    using namespace foxit;
    using namespace foxit::common;
    using foxit::common::Library;
    using namespace pdf;
    ...
    // Assuming PDFPage page has been loaded and parsed.
    ...
    page.RemoveAllWatermarks();
    ...
    // Save document to file
    ...