Because PDF documents have better stability and compatibility, more and more contracts, research papers, reports, etc. are now using PDF format. To further protect the content of these important documents from unauthorized copying or use, we can add a watermark to indicate status, ownership or purpose. In response to the need to add text watermarks to PDF documents in C++ applications that may arise at work, this article will show in detail how to use the third-party domestic library Spire.PDF for C++ < /strong>To achieve this function.
Table of Contents
C++ PDF library installation and introduction
Add single line text watermark to PDF document
Add multi-line text watermark to PDF document
C++ PDF library installation and introduction
Spire.PDF for C++ is a domestic independent PDF control that can create, read, operate, edit and convert PDF documents in C++ programs.
The recommended installation method is to search for “spire.pdf.cpp” through NuGet in Visual Studio, and then click Download to install it into the program.
Or download Spire.PDF for C++ locally, unzip it and manually copy the library into the program. If you have any questions about installation, please refer to:
How to integrate Spire.PDF for C++ into a C++ program
Add a single line of text watermark to the PDF document
Spire.PDF for C++ does not provide an interface or class for inserting watermarks directly into PDF, but you can simulate the effect of adding text watermarks by drawing text on the PDF page and setting its transparency. The main steps are as follows:
- Create a PdfDocument object.
- Load a sample PDF document using the PdfDocument->LoadFromFile() method.
- Use the PdfDocument->GetPages()->GetItem() method to get a specific page in the document.
- Use the PdfPageBase->GetCanvas()->TraslateTransform() method to translate the page coordinate system to the specified position.
- Use the PdfPageBase->GetCanvas()->RotateTransform() method to specify the angle of the coordinate system. Steps 4 and 5 are to ensure that the watermark is displayed in the center of the page at the specified angle.
- Use the PdfPageBase->GetCanvas()->DrawString() method to draw text watermarks on PDF pages.
- Save the resulting document using the PdfDocument->SaveToFile() method.
C++ code:
#include "Spire.Pdf.o.h"; using namespace std; using namespace Spire::Pdf; int main() { //Create PdfDocument object intrusive_ptr <PdfDocument> doc = new PdfDocument(); //Load PDF file doc->LoadFromFile(L"agreement.pdf"); //Create TrueType font intrusive_ptr <PdfTrueTypeFont> font = new PdfTrueTypeFont(L"黑体", 60.0f, PdfFontStyle::Bold, true); //Create brush intrusive_ptr <PdfBrush> brush = PdfBrushes::GetSkyBlue(); //Specify watermark text wstring text = L"internal file"; //Measure watermark text size SizeF textSize = font->MeasureString(text.c_str()); //Calculate two offsets, used to calculate the translation of the coordinate system float offset1 = (float)(textSize.GetWidth() * sqrt(2) / 4); float offset2 = (float)(textSize.GetHeight() * sqrt(2) / 4); //Traverse the pages in the document for (size_t i = 0; i < doc->GetPages()->GetCount(); i + + ) { //Get the specified page intrusive_ptr <PdfPageBase> page = doc->GetPages()->GetItem(i); //Set transparency page->GetCanvas()->SetTransparency(0.8); //Translate the page coordinate system to the specified position 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 a watermark on the page page->GetCanvas()->DrawString(text.c_str(), font, brush, 0, 0, new PdfStringFormat(PdfTextAlignment::Left)); } //Save the result document doc->SaveToFile(L"Output\text watermark.pdf"); doc->Close(); }
Single line watermark effect:
Add multi-line text watermark to PDF document
The tiling watermark effect can be achieved using the PdfTilingBrush class provided by Spire.PDF for C++. The tiling brush will generate a tiling pattern, which can be used to repeatedly fill the specified graphics area to add multiple lines of watermarks in PDF documents. The main steps are as follows:
- Create a custom method InsertTiledTextWatermark(intrusive_ptr
page, wstring watermarkText, intrusive_ptr to add tiling watermark to PDF pages. (The parameters rowNum and columnNum specify the number of rows and columns of the tiled watermark).font, int rowNum, int columnNum) - Create a PdfDocument object.
- Use the PdfDocument->LoadFromFile() method to load the PDF sample document.
- Iterate through all pages in the document and call the custom method InsertTiledTextWatermark() to add a watermark to each page.
- Use the PdfDocument->SaveToFile() method to save the resulting document.
C++ code:
#include "Spire.Pdf.o.h"; using namespace std; using namespace Spire::Pdf; static void InsertTiledTextWatermark(intrusive_ptr <PdfPageBase> page, wstring watermarkText, intrusive_ptr <PdfTrueTypeFont> font, int rowNum, int columnNum) { //Measure watermark text size SizeF textSize = font->MeasureString(watermarkText.c_str()); //Calculate two offsets, used to calculate the translation of the 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 tile brush intrusive_ptr <PdfTilingBrush> brush = new PdfTilingBrush(new SizeF(width / columnNum, height / rowNum)); brush->GetGraphics()->SetTransparency(0.3f); 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)); //Use the tiling brush to draw a rectangle covering the entire page page->GetCanvas()->DrawRectangle(brush, new RectangleF(new PointF(0, 0), page->GetActualSize())); } int main() { //Create PdfDocument object intrusive_ptr <PdfDocument> doc = new PdfDocument(); //Load PDF file doc->LoadFromFile(L"agreement.pdf"); //Specify watermark text wstring text = L"internal file"; //Create TrueType font intrusive_ptr <PdfTrueTypeFont> font = new PdfTrueTypeFont(L"黑体", 30.0f, PdfFontStyle::Bold, true); //Traverse the pages in the document for (size_t i = 0; i < doc->GetPages()->GetCount(); i + + ) { //Call custom method to insert multi-line text watermark InsertTiledTextWatermark(doc->GetPages()->GetItem(i), text.c_str(), font, 3, 3); } //Save the result file doc->SaveToFile(L"Output\Multi-line watermark.pdf"); doc->Close(); }
Multi-line watermark effect:
Spire.PDF for C++ is a multifunctional C++ PDF library. In addition to adding text watermarks to PDF documents, you can also add image watermarks. See how to add image watermarks to PDF.
—No need to worry about the red watermark, click to apply for a one-month free license for testing.