Add Image Watermarks to PDF in C++

JonathonJonathon
3 min read

Image watermarks in PDF is the images shown beneath the document content. Adding image watermarks can help protect the copyright and confidentiality of the document. This article will share how to add image watermarks to PDF in C++ using Spire.PDF for C++.

  • Add a Single Image Watermark to PDF

  • Add a Tiled Image Watermark to PDF

Install the Library

Spire.PDF for C++ is a professional PDF API applied to creating, writing, editing, handling and reading PDF files without any external dependencies within C++ application.
There are two methods to install it and you can refer to the below tutorial for details.
Integrate Spire.PDF for C++ in a C++ Application

Add a Single Image Watermark to PDF in C++

Spire.PDF for C++ offers the PdfPageBase->GetCanvas()->DrawImage() method to draw images at any position of a PDF page. You can also set the transparency of the image to prevent it from overwriting the text. The complete sample code is shown below.

#include "Spire.Pdf.o.h";

using namespace std;
using namespace Spire::Pdf;

int main()
{
    //Specify input file and output file paths
    wstring inputFilePath = L"Data\\sample.pdf";
    wstring outputFilePath = L"Output\\ImageWatermark.pdf";

    //Create a PdfDocument object
    PdfDocument* doc = new PdfDocument();

    //Load a PDF file
    doc->LoadFromFile(inputFilePath.c_str());

    //Load an image
    PdfImage* image = PdfImage::FromFile(L"C:\\Users\\Administrator\\Desktop\\logo.png");

    //Get image height and width
    int imgHeight = image->GetHeight();
    int imgWidth = image->GetWidth();

    //Traverse through the pages in the document
    for (size_t i = 0; i < doc->GetPages()->GetCount(); i++)
    {
        //Get a specific page
        PdfPageBase* page = doc->GetPages()->GetItem(i);

        //Set the page transparency
        page->GetCanvas()->SetTransparency(0.5);

        //Get the page width and height
        float pageWidth = page->GetActualSize()->GetWidth();
        float pageHeight = page->GetActualSize()->GetHeight();

        //Draw image at the center of the page
        RectangleF* rect = new RectangleF((float)(pageWidth - imgWidth) / 2, (float)(pageHeight - imgHeight) / 2, imgWidth, imgHeight);
        page->GetCanvas()->DrawImage(image, rect);
    }

    //Save the document
    doc->SaveToFile(outputFilePath.c_str());
    doc->Close();
    delete doc;
}

Add a Tiled Image Watermark to PDF in C++

A tiled watermark effect can be achieved by using the PdfTilingBrush class. The tiling brush produces a tiled pattern that is repeated to fill a graphics area. The complete sample code is shown below.

#include "Spire.Pdf.o.h";

using namespace std;
using namespace Spire::Pdf;

static void InsertTiledImagetWatermark(PdfPageBase* page, PdfImage* image, int rowNum, int columnNum)
{
    //Get page height and width
    float height = page->GetActualSize()->GetHeight();
    float width = page->GetActualSize()->GetWidth();

    //Get image heigh and width
    float imgHeight = image->GetHeight();
    float imgWidth = image->GetWidth();

    //Create a tiling brush
    PdfTilingBrush* brush = new PdfTilingBrush(new SizeF(width / columnNum, height / rowNum));

    //Set transparency
    brush->GetGraphics()->SetTransparency(0.5f);

    //Translate coordinate system to a certain position
    brush->GetGraphics()->TranslateTransform((brush->GetSize()->GetWidth() - imgWidth) / 2, (brush->GetSize()->GetHeight() - imgHeight) / 2);

    //Draw image on the brush at the specified coordinates
    brush->GetGraphics()->DrawImage(image, 0.0, 0.0);

    //Draw rectangle that covers the whole page with the brush
    page->GetCanvas()->DrawRectangle(brush, 0, 0, width, height);
}

int main()
{
    //Specify input file and output file paths
    wstring inputFilePath = L"Data\\sample.pdf";
    wstring outputFilePath = L"Output\\TiledImageWatermark.pdf";

    //Create a PdfDocument object
    PdfDocument* doc = new PdfDocument();

    //Load a PDF file
    doc->LoadFromFile(inputFilePath.c_str());

    //Load an image
    PdfImage* image = PdfImage::FromFile(L"C:\\Users\\Administrator\\Desktop\\small-logo.png");

    //Traverse through the pages in the document
    for (size_t i = 0; i < doc->GetPages()->GetCount(); i++)
    {
        //Get a specific page
        PdfPageBase* page = doc->GetPages()->GetItem(i);

        //Add tiled image watermark to the page
        InsertTiledImagetWatermark(page, image, 4, 4);
    }

    //Save the document
    doc->SaveToFile(outputFilePath.c_str());
    doc->Close();
    delete doc;
}

0
Subscribe to my newsletter

Read articles from Jonathon directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jonathon
Jonathon