Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/5942aff54803299fd880ed7342db2ccf to your computer and use it in GitHub Desktop.
Save aspose-com-gists/5942aff54803299fd880ed7342db2ccf to your computer and use it in GitHub Desktop.
Insert or Delete Text/Image Watermark in a PDF File using C++
Insert or Delete Text/Image Watermark in a PDF File using C++
// Open the source PDF document
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample 1.pdf");
// Create an instance of the ImageStamp class
System::SharedPtr<ImageStamp> imageStamp = MakeObject<ImageStamp>(u"SourceDirectory\\aspose.png");
// Set the position of the watermark
imageStamp->set_XIndent(150);
imageStamp->set_YIndent(350);
// Set other properties
imageStamp->set_Height(100);
imageStamp->set_Width(300);
imageStamp->set_RotateAngle(45);
imageStamp->set_Opacity(0.4);
imageStamp->setStampId(12345678);
// Add watermark to the PDF page
pdfDocument->get_Pages()->idx_get(1)->AddStamp(imageStamp);
// Save the PDF file
pdfDocument->Save(u"OutputDirectory\\Image-Watermark-Out.pdf", SaveFormat::Pdf);
// Open the source PDF document
auto pdfDocument = MakeObject<Document>(u"SourceDirectory\\Sample 1.pdf");
// Create an instance of the TextStamp class
System::SharedPtr<TextStamp> textStamp = MakeObject<TextStamp>(u"CONFIDENTIAL");
// Set the position of the watermark
textStamp->set_XIndent(70);
textStamp->set_YIndent(300);
// Set text properties
textStamp->get_TextState()->set_Font(FontRepository::FindFont(u"Arial"));
textStamp->get_TextState()->set_FontSize(72.0F);
textStamp->get_TextState()->set_ForegroundColor(Aspose::Pdf::Color::get_Red());
textStamp->set_Opacity(0.4);
textStamp->set_RotateAngle(45);
textStamp->setStampId(123456);
// Add watermark to the PDF page
pdfDocument->get_Pages()->idx_get(1)->AddStamp(textStamp);
// Save the PDF file
pdfDocument->Save(u"OutputDirectory\\Text-Watermark-Out.pdf", SaveFormat::Pdf);
// Create an instance of the PdfContentEditor class
System::SharedPtr<PdfContentEditor> contentEditor = MakeObject<PdfContentEditor>();
// Open the PDF file containing the watermark
contentEditor->BindPdf(u"SourceDirectory\\SampleImageWatermark.pdf");
// Delete watermark by id
contentEditor->DeleteStampById(12345678);
// Save the PDF file
pdfDocument->Save(u"OutputDirectory\\Remove-Watermark-Out.pdf", SaveFormat::Pdf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment