Watermark annotations in C++
Create a watermark annotation containing three lines of text, inspect watermark rectangles, or remove watermark annotations from the first page. The creation example uses Arial at 25 points with blue text and 0.5 opacity.
The snippets retain the example namespaces and include files. They assume the Aspose.PDF for C++ SDK is configured. Functions that open an existing PDF generally use its first page; supply a PDF containing the expected text, annotations, or media resources. Coordinates are taken from the supplied fixtures and should be adjusted for other documents.
Add a watermark annotation
Place a three-line blue watermark on page 1 using Arial at 25 points and an opacity of 0.5.
- Open the input PDF with Document.
- Create a WatermarkAnnotation with the target rectangle.
- Add it with AnnotationCollection::Add().
- Configure TextState with the font, size, and color.
- Set opacity and supply the text lines with SetTextAndState().
- Save the result with Document::Save().
#include <system/array.h>
#include <system/exceptions.h>
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Point.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationCollection.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/WatermarkAnnotation.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/Text/FontRepository.h>
#include <Aspose.PDF.Cpp/Text/TextState.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Annotations;
using namespace Aspose::Pdf::Text;
namespace WorkingWithAnnotationsExamples::Watermark {
void WatermarkAdd(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto page = document->get_Pages()->idx_get(1);
auto watermarkAnnotation = MakeObject<WatermarkAnnotation>(
page,
MakeObject<Rectangle>(100, 100, 400, 200, true));
page->get_Annotations()->Add(watermarkAnnotation);
auto textState = MakeObject<TextState>();
textState->set_ForegroundColor(Color::get_Blue());
textState->set_FontSize(25);
textState->set_Font(FontRepository::FindFont(u"Arial"));
watermarkAnnotation->set_Opacity(0.5);
watermarkAnnotation->SetTextAndState(MakeArray<String>({u"HELLO", u"Line 1", u"Line 2"}), textState);
document->Save(outputFile);
}
} // namespace WorkingWithAnnotations::Watermark
Get watermark annotations
Find watermark annotations on the first page and print their bounding rectangles.
- Open the input PDF with Document.
- Iterate through the first page Annotations collection.
- Filter the annotations using AnnotationType.
- Read the matching Annotation rectangles and print them.
#include <iostream>
#include <system/exceptions.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Point.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationCollection.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationType.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Annotations;
using namespace Aspose::Pdf::Text;
namespace WorkingWithAnnotationsExamples::Watermark {
void WatermarkGet(System::String inputFile)
{
auto document = MakeObject<Document>(inputFile);
for (auto a : document->get_Pages()->idx_get(1)->get_Annotations()) {
if (a->get_AnnotationType() == AnnotationType::Watermark) {
std::cout << a->get_Rect()->ToString() << std::endl;
}
}
}
} // namespace WorkingWithAnnotations::Watermark
Delete watermark annotations
Collect watermark annotations on the first page, then delete the collected objects. The collection is not modified during enumeration.
- Open the input PDF with Document.
- Iterate through the first page Annotations collection and filter by AnnotationType.
- Collect the matching annotations before modifying the collection.
- Delete each collected annotation with AnnotationCollection::Delete().
- Save the result with Document::Save().
#include <vector>
#include <system/exceptions.h>
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Point.h>
#include <Aspose.PDF.Cpp/Rectangle.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/Annotation.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationCollection.h>
#include <Aspose.PDF.Cpp/InteractiveFeatures/Annotations/AnnotationType.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Annotations;
using namespace Aspose::Pdf::Text;
namespace WorkingWithAnnotationsExamples::Watermark {
void WatermarkDelete(System::String inputFile, System::String outputFile)
{
auto document = MakeObject<Document>(inputFile);
std::vector<SharedPtr<Annotation>> toDelete;
for (auto a : document->get_Pages()->idx_get(1)->get_Annotations()) {
if (a->get_AnnotationType() == AnnotationType::Watermark) {
toDelete.push_back(a);
}
}
for (auto a : toDelete) {
document->get_Pages()->idx_get(1)->get_Annotations()->Delete(a);
}
document->Save(outputFile);
}
} // namespace WorkingWithAnnotations::Watermark