C++: Add Text Watermarks to PDF Documents

As increasingly diverse types of documents are created in PDF, you may find yourself with a need to protect confidential information. Although many other PDF security options are available to keep confidential information secure, the most common approach is to add a custom watermark to the PDF document. In this article, you will learn how to add a single-line or multi-line text watermark to PDF in C++ using Spire.PDF for C++.

Install Spire.PDF for C++

There are two ways to integrate Spire.PDF for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.PDF for C++ in a C++ Application

Add a Single-Line Text Watermark to PDF in C++

Spire.PDF for C++ does not provide an interface or a class responsible for inserting watermarks into PDF files. You could, however, draw text like "confidential", "do not copy", or "draft" on each page to mimic the watermark effect. The following are the steps to add a single-line text watermark to a PDF document.

  • Create a PdfDocument object.
  • Load a sample PDF document using PdfDocument->LoadFromFile() method.
  • Get a specific page from the document using PdfDocument->GetPages()->GetItem() method.
  • Translate the coordinate system to the specified coordinate using PdfPageBase->GetCanvas()->TraslateTransform() method.
  • Rotate the coordinate system 45 degrees counterclockwise using PdfPageBase->GetCanvas()->RotateTransform() method. This step and the step above make sure that the watermark will appear in the middle of the page with an angle of 45 degrees.
  • Draw a text watermark on the page using PdfPageBase->GetCanvas()->DrawString() method.
  • Save the document to a different PDF file using PdfDocument->SaveToFile() method.
  • C++
#include "Spire.Pdf.o.h";

using namespace std;
using namespace Spire::Pdf;
using namespace Spire::Common;

int main()
{
	//Specify input file and output file paths
	wstring inputFilePath= L"C:\\Users\\Administrator\\Desktop\\sample.pdf";
	wstring outputFilePath = L"Output\\SingleLineTextWatermark.pdf";

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

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

	//Create a true type font
	PdfTrueTypeFont* font = new PdfTrueTypeFont(L"Arial", 50.0f, PdfFontStyle::Bold, true);

	//Create a brush
	PdfBrush* brush = PdfBrushes::GetDarkGray();

	//Specify watermark text
	wstring text = L"CONFIDENTIAL";

	//Measure the text size
	SizeF textSize = font->MeasureString(text.c_str());

	//Calculate two offsets, which are used to calculate the translation amount of coordinate system
	float offset1 = (float)(textSize.GetWidth() * sqrt(2) / 4);
	float offset2 = (float)(textSize.GetHeight()*sqrt(2) / 4);

	//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.8);

		//Translate the coordinate system to a specified coordinate
		page->GetCanvas()->TranslateTransform(page->GetCanvas()->GetSize()->GetWidth() / 2 - offset1 - offset2, page->GetCanvas()->GetSize()->GetHeight() / 2 + offset1 - offset2);

		//Rotate the coordinate system 45 degrees counterclockwise
		page->GetCanvas()->RotateTransform(-45);

		//Draw watermark text on the page
		page->GetCanvas()->DrawString(text.c_str(), font, brush, 0, 0, new PdfStringFormat(PdfTextAlignment::Left));
	}	

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

C++: Add Text Watermarks to PDF Documents

Add a Tiled Text Watermark to PDF in C++

To achieve the tiled watermark effect, you can make use of the PdfTilingBrush class. The tiling brush produces a tiled pattern that is repeated to fill a graphics area. The following are the steps to add a tiled text watermark to a PDF document.

  • Create a custom method InsertTiledTextWatermark(PdfPageBase* page, wstring watermarkText, PdfTrueTypeFont* font, int rowNum, int columnNum) to add a tiled watermark to a PDF page. The parameter rowNum and columnNum specify the row number and column number of the tiled watermark.
  • Create a PdfDocument object.
  • Load a sample PDF document using PdfDocument->LoadFromFile() method.
  • Traverse through all pages in the document, and call the custom method InsertTiledTextWatermark() to apply watermark to each page.
  • Save the document to another file using PdfDocument->SaveToFile() method.
  • C++
#include "Spire.Pdf.o.h";

using namespace std;
using namespace Spire::Pdf;
using namespace Spire::Common;

static void InsertTiledTextWatermark(PdfPageBase* page, wstring watermarkText, PdfTrueTypeFont* font, int rowNum, int columnNum)
{
	//Measure text size
	SizeF textSize = font->MeasureString(watermarkText.c_str());

	//Calculate two offsets, which are used to calculate the translation amount of coordinate system
	float offset1 = (float)(textSize.GetWidth() * sqrt(2) / 4);
	float offset2 = (float)(textSize.GetHeight() * sqrt(2) / 4);

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

	//Create a tiling brush
	PdfTilingBrush* brush = new PdfTilingBrush(new SizeF(width / columnNum, height / rowNum));
	brush->GetGraphics()->SetTransparency(0.5f);
	brush->GetGraphics()->TranslateTransform(brush->GetSize()->GetWidth() / 2 - offset1 - offset2, brush->GetSize()->GetHeight() / 2 + offset1 - offset2);
	brush->GetGraphics()->RotateTransform(-45);

	//Draw watermark text on the brush
	brush->GetGraphics()->DrawString(watermarkText.c_str(), font, PdfBrushes::GetRed(), 0, 0, new PdfStringFormat(PdfTextAlignment::Left));

	//Draw a rectangle (that covers the whole page) using the tiling brush
	page->GetCanvas()->DrawRectangle(brush, new RectangleF(new PointF(0, 0), page->GetActualSize()));
}

int main()
{
	//Specify input file and output file paths
	wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\sample.pdf";
	wstring outputFilePath = L"Output\\MultiLineTextWatermark.pdf";

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

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

	//Specify watermark text
	wstring text = L"CONFIDENTIAL";

	//Creat a true type font
	PdfTrueTypeFont* font = new PdfTrueTypeFont(L"Arial", 20.0f, PdfFontStyle::Bold, true);

	//Traverse through the pages in the document
	for (size_t i = 0; i < doc->GetPages()->GetCount(); i++)
	{
		//Call the custom method to insert multi-line text watermark
		InsertTiledTextWatermark(doc->GetPages()->GetItem(i), text.c_str(), font, 3, 3);
	}

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

C++: Add Text Watermarks to PDF Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.