Add Watermark to PowerPoint Slides using C++

A watermark identifies the state of the document with texts like confidential, draft, etc., and makes it difficult for the original document to be copied. Watermarks are also used to specify the ownership of a document by showing the company name or logo. Watermarks in PowerPoint files can be both image or text-based. In this article, you will learn how to add text and image watermarks to PowerPoint slides using C++.

Info: You may want to check out Aspose.Slides free Add Watermark to PowerPoint and Remove Watermark from PowerPoint online tools.

C++ API for Adding Watermark to PowerPoint Slides

We will use the Aspose.Slides for C++ API to add watermark to PowerPoint slides. It is a robust and feature-rich API that supports creating, reading, and updating PowerPoint files without requiring additional software. You can either install the API through NuGet or download it directly from the Downloads section.

PM> Install-Package Aspose.Slides.Cpp

Add Text Watermark to PowerPoint Slides using C++

You can add a text watermark to PowerPoint slides by following the steps given below.

The following sample code shows how to add text watermark to PowerPoint slides using C++.

// File paths
const String sourceFilePath = u"SourceDirectory\\Slides\\Presentation2.pptx";
const String outputFilePath = u"OutputDirectory\\AddTextWatermark_out.pptx";
// Load the presentation file
auto presentation = System::MakeObject<Presentation>(sourceFilePath);
// Access the master slide
auto master = presentation->get_Masters()->idx_get(0);
System::Drawing::PointF center(presentation->get_SlideSize()->get_Size().get_Width() / 2, presentation->get_SlideSize()->get_Size().get_Height() / 2);
float width = 300.0f;
float height = 300.0f;
float x = center.get_X() - width / 2;
float y = center.get_Y() - height / 2;
// Add shape
auto watermarkShape = master->get_Shapes()->AddAutoShape(ShapeType::Rectangle, x, y, width, height);
// Set fill type
watermarkShape->get_FillFormat()->set_FillType(FillType::NoFill);
watermarkShape->get_LineFormat()->get_FillFormat()->set_FillType(FillType::NoFill);
// Set rotation angle
watermarkShape->set_Rotation(-45);
// Set text
auto watermarkTextFrame = watermarkShape->AddTextFrame(u"Watermark");
// Set font and color
auto watermarkPortion = watermarkTextFrame->get_Paragraphs()->idx_get(0)->get_Portions()->idx_get(0);
watermarkPortion->get_PortionFormat()->set_FontHeight(52.0f);
int32_t alpha = 150, red = 200, green = 200, blue = 200;
watermarkPortion->get_PortionFormat()->get_FillFormat()->set_FillType(FillType::Solid);
watermarkPortion->get_PortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::FromArgb(alpha, red, green, blue));
// Lock Shapes from modifying
watermarkShape->get_AutoShapeLock()->set_SelectLocked(true);
watermarkShape->get_AutoShapeLock()->set_SizeLocked(true);
watermarkShape->get_AutoShapeLock()->set_TextLocked(true);
watermarkShape->get_AutoShapeLock()->set_PositionLocked(true);
watermarkShape->get_AutoShapeLock()->set_GroupingLocked(true);
// Save Presentation
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
Image of the output generated by the sample code

Image of the output generated by the sample code

Add Image Watermark to PowerPoint Slides using C++

In order to add an image watermark to PowerPoint slides, please follow the steps given below.

The following sample code shows how to add an image watermark to PowerPoint slides using C++.

// File paths
const String sourceFilePath = u"SourceDirectory\\Slides\\Presentation2.pptx";
const String outputFilePath = u"OutputDirectory\\AddImageWatermark_out.pptx";
// Load the presentation file
auto presentation = System::MakeObject<Presentation>(sourceFilePath);
// Access the maser slide
auto master = presentation->get_Masters()->idx_get(0);
System::Drawing::PointF center(presentation->get_SlideSize()->get_Size().get_Width() / 2, presentation->get_SlideSize()->get_Size().get_Height() / 2);
float width = 300.0f;
float height = 300.0f;
float x = center.get_X() - width / 2;
float y = center.get_Y() - height / 2;
// Add shape
auto watermarkShape = master->get_Shapes()->AddAutoShape(ShapeType::Rectangle, x, y, width, height);
auto image = presentation->get_Images()->AddImage(File::ReadAllBytes(u"SourceDirectory\\Images\\AsposeLogo.png"));
// Set fill type
watermarkShape->get_FillFormat()->set_FillType(FillType::Picture);
watermarkShape->get_FillFormat()->get_PictureFillFormat()->get_Picture()->set_Image(image);
watermarkShape->get_FillFormat()->get_PictureFillFormat()->set_PictureFillMode(PictureFillMode::Stretch);
// Lock Shapes from modifying
watermarkShape->get_AutoShapeLock()->set_SelectLocked(true);
watermarkShape->get_AutoShapeLock()->set_SizeLocked(true);
watermarkShape->get_AutoShapeLock()->set_TextLocked(true);
watermarkShape->get_AutoShapeLock()->set_PositionLocked(true);
watermarkShape->get_AutoShapeLock()->set_GroupingLocked(true);
// Save Presentation
presentation->Save(outputFilePath, Aspose::Slides::Export::SaveFormat::Pptx);
Image of the output generated by the sample code

Image of the output generated by the sample code

Get a Free License

In order to try the API without evaluation limitations, you can request a free temporary license.

Conclusion

In this article, you have learned how to add a watermark to PowerPoint slides using C++. The shared code samples show how to easily add image and text watermarks to PowerPoint slides using the Aspose.Slides to C++ API. It is a powerful API that provides a bunch of additional features for working with PowerPoint PPTX/PPT files. You can explore the API in detail by visiting the official documentation. In case of any queries, please feel free to reach us at our free support forum.

See Also