Add Watermark via C++

Add watermarks to PDF document programmatically using Aspose.PDF for C++

Add Watermark to PDF File Using C++ Tool

In order to add Watermark to PDF File, we’ll use Aspose.PDF for C++ API, which is a feature-rich, powerful, and easy-to-use document manipulation API for the C++ platform. Open NuGet package manager, search for Aspose.PDF.Cpp and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

Add Watermark via C++


You need Aspose.PDF for C++ to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Create an instance of WatermarkArtifact.
  3. Set properties of WatermarkArtifact object.
  4. Add watermark using method Add of Aspose.Pdf.Page.Artifacts collection class.
  5. Save PDF file

Add Watermark to PDF - C++


    // Read pdf file to Aspose Document
    doc = MakeObject<Document>(u"1.pdf");

    // Create artifact with image
    artifact = MakeObject<WatermarkArtifact>();
    artifact->SetImage(u"watermark.jpg");

    // Set artifact position and rotation
    artifact->set_ArtifactHorizontalAlignment(Aspose::Pdf::HorizontalAlignment::Center);
    artifact->set_ArtifactVerticalAlignment(Aspose::Pdf::VerticalAlignment::Center);
    artifact->set_Rotation(15);
    artifact->set_Opacity(1);
    artifact->set_IsBackground(true);

    // Add artifact to first page
    doc->get_Pages()->idx_get(1)->get_Artifacts()->Add(artifact);

    // save result pdf to file
    doc->Save(u"add_watermark.pdf",SaveFormat::Pdf);