A watermark in a document identifies the state of the document with texts like confidential, draft, etc., and makes it difficult for the original document to be copied. Watermarks can be image or text-based and are frequently used in PDF documents. You might need to add watermarks to PDF documents within your C++ applications. For that, this article will teach you how to add and remove text and image watermarks from PDF documents using C++.
- C++ API for Inserting and Deleting Text/Image Watermarks
- Insert Text Watermark in a PDF File using C++
- Insert Image Watermark in a PDF File using C++
- Remove Text or Image Watermark from a PDF File using C++
C++ API for Inserting and Deleting Text/Image Watermarks
Aspose.PDF for C++ is a C++ library that allows you to create, read and update PDF documents. Furthermore, the API supports inserting and deleting Image/Text watermarks in PDF documents. You can either install the API through NuGet or download it directly from the downloads section.
PM> Install-Package Aspose.PDF.Cpp
Insert Text Watermark in a PDF File using C++
With Aspose.PDF for C++ API, you can control the font style, text, color, angle, opacity, etc of the text watermark. The following are the steps to add a text watermark in a PDF file.
- Load the PDF file using the Document class.
- Create an instance of the TextStamp class with the watermark text.
- Set the position and other properties of the watermark.
- Add the watermark to the page using the Document->get_Pages()->idx_get(1)->AddStamp(System::SharedPtr<Stamp> stamp) method.
- Save the PDF file using the Document->Save(System::String outputFileName, SaveFormat format) method.
The following sample code shows how to add a text watermark in a PDF file using C++.
Insert Image Watermark in a PDF File using C++
Image watermarks are usually used to show the ownership of the document by using the logo or any other identifiable image. You can add an image watermark to PDF files using the steps given below.
- Load the PDF file using the Document class.
- Create an instance of the ImageStamp class with the watermark image.
- Set the position and other properties of the watermark.
- Add the watermark to the page using the Document->get_Pages()->idx_get(1)->AddStamp(System::SharedPtr<Stamp> stamp) method.
- Save the PDF file using the Document->Save(System::String outputFileName, SaveFormat format) method.
The following sample code demonstrates how to add an image watermark in a PDF file using C++.
Remove Text or Image Watermark from a PDF File using C++
There might be scenarios where you need to remove watermarks from a PDF document. In the previous examples, we defined ids for both text and image watermarks. We will use those ids to remove the watermarks from the PDF documents. The following are the steps to remove watermarks from PDF files.
- Create an instance of the PdfContentEditor class.
- Load the PDF document using the PdfContentEditor->BindPdf(System::String inputFile) method.
- Delete the watermark using the PdfContentEditor->DeleteStampById(int32_t stampId) method.
- Save the PDF file using the Document->Save(System::String outputFileName, SaveFormat format) method.
The following sample code shows how to remove text and image watermarks from PDF files using C++.
Get a Free License
You can try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to work with watermarks in PDF files using C++. Specifically, you have learned how to add and remove image and text watermarks from PDF documents using Aspose.PDF for C++ API. The API provides many additional features for automating your PDF-related tasks. You can explore the API in detail by visiting the official documentation. In case of any questions, please feel free to reach us on our free support forum.