在本教程中,我们将学习如何使用 C++ 在 PPTX 中添加图像水印。您可以使用 C++ 中的简单 API 接口在 PowerPoint 演示文稿中插入徽标、签名或任何视觉信息作为图像水印。该功能不依赖于 Microsoft PowerPoint 或任何用于在 Windows 或 Linux 平台上添加图像水印的应用程序。
使用 C++ 在 PPTX 中添加图像水印的步骤
- 从 NuGet 包管理器工具安装 Aspose.Slides for C++
- 添加对 Aspose::Slides 命名空间的引用
- 使用 Presentation Class 加载输入 PPTX 以添加图像水印
- 在所有主幻灯片中添加 PictureFrame 以添加图像水印
- 锁定添加的图像水印以避免更改
- 保存输出带水印的 PPTX 演示文件
您可以使用几行简单的代码将图像水印插入 C++ 中的 PowerPoint。此示例也适用于 PPT 文件格式。
使用 C++ 在 PPTX 中添加图像水印的代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <DOM/Presentation.h> | |
#include <DOM/IMasterSlideCollection.h> | |
#include <DOM/IMasterSlide.h> | |
#include <DOM/IShapeCollection.h> | |
#include <DOM/IAutoShape.h> | |
#include <DOM/ShapeType.h> | |
#include <DOM/SlideSizeScaleType.h> | |
#include <DOM/SlideSize.h> | |
#include <DOM/ISlideSize.h> | |
#include <DOM/IFillFormat.h> | |
#include <DOM/IPictureFillFormat.h> | |
#include <DOM/PictureFillMode.h> | |
#include <DOM/ITextFrame.h> | |
#include <DOM/IPortionFormat.h> | |
#include <DOM/IParagraphCollection.h> | |
#include <DOM/IParagraph.h> | |
#include <DOM/IPortionCollection.h> | |
#include <DOM/IPortion.h> | |
#include <DOM/FillType.h> | |
#include <DOM/NullableBool.h> | |
#include <DOM/IColorFormat.h> | |
#include <DOM/AutoShapeLock.h> | |
#include <Export/SaveFormat.h> | |
#include <DOM/IPPImage.h> | |
#include <DOM/IImageCollection.h> | |
#include <DOM/IPictureFrame.h> | |
#include <drawing/Color.h> | |
#include <drawing/Size.h> | |
#include <drawing/size_f.h> | |
#include <system/io/file_mode.h> | |
#include <system/io/file_stream.h> | |
using namespace Aspose::Slides; | |
using namespace System; | |
using namespace System::Drawing; | |
using namespace System::IO; | |
class AddWatermarkEx | |
{ | |
public: | |
static void AddImageWatermark() | |
{ | |
// Load the input presentation for adding image watermark | |
SharedPtr<Presentation> WatermarkPresentation = MakeObject<Presentation>(u"SourceInput.pptx"); | |
String imageFilePath = u"Tulips.jpg"; | |
//SharedPtr<System::Drawing::Bitmap> bitmap = MakeObject<System::Drawing::Bitmap>(imageFilePath); | |
SharedPtr<FileStream> ImageStream = MakeObject<IO::FileStream>(imageFilePath, System::IO::FileMode::Open); | |
SharedPtr<IPPImage> image = WatermarkPresentation->get_Images()->AddImage(ImageStream); | |
// Accessing the master slides for adding image watermark | |
for (SharedPtr <IMasterSlide> masterSlide : WatermarkPresentation->get_Masters()) | |
{ | |
// Adding a Pptx image watermark shape | |
SharedPtr <IPictureFrame> PptxWatermark = masterSlide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, | |
WatermarkPresentation->get_SlideSize()->get_Size().get_Width() / 2 - 50, | |
WatermarkPresentation->get_SlideSize()->get_Size().get_Height() / 2 - 50, | |
200, 50, image); | |
// Set the fill type, fill format, and rotation angle of the shape | |
PptxWatermark->set_Rotation(325); | |
PptxWatermark->get_FillFormat()->set_FillType(FillType::Picture); | |
PptxWatermark->get_FillFormat()->get_PictureFillFormat()->set_PictureFillMode(PictureFillMode::Stretch); | |
// Locking Pptx image watermarked shape to be uneditable in PowerPoint | |
PptxWatermark->get_ShapeLock()->Lock(); | |
} | |
// Save output presentation with watermark image | |
WatermarkPresentation->Save(u"ImageWatermarkPresentation.pptx", Export::SaveFormat::Pptx); | |
} | |
}; |
在上一个主题中,我们学习了 如何使用 C++ 在 PPTX 中添加草稿水印。本主题重点介绍如何使用 C++ 在 pptx 中放置图像水印。