通过 C++ 添加水印

如何使用 C++ 在 PDF 中添加水印

使用 C++ 库添加水印

为了将水印添加到 PDF 文件中,我们将使用 Aspose.PDF for C++ API,这是一款适用于 C++ 平台的功能丰富、功能强大且易于使用的文档操作 API。打开 NuGet 软件包管理器,搜索 aspose.pdf.cpp 然后安装。您也可以在软件包管理器控制台中使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

使用 C++ 添加水印


你需要 Aspose.PDF for C++ 才能在你的环境中试用代码。

1.加载带有文档实例的 PDF。 1.创建 WatermarkArtifact 的实例。 1.设置 WatermarkArtifact 对象的属性。 1.使用方法 Add of Aspose.Pdf.Page.Artics 集合类添加水印。 1.保存 PDF 文件

在 PDF 中添加水印-C++。

<% watermark.code-block.subtitle %>

Input file:

File not added

Output format:

Output file:

void GettingWatermarks() {

    String _dataDir("C:\\Samples\\");
    String inputFileName("watermark.pdf");
    String outputFileName("watermark_out.pdf");

    auto document = MakeObject<Document>(_dataDir + inputFileName);

    auto artifact = MakeObject<WatermarkArtifact>();
    auto textState = MakeObject<TextState>();
    textState->set_FontSize(72);
    textState->set_ForegroundColor(Color::get_Blue());
    textState->set_Font(FontRepository::FindFont(u"Courier"));
    artifact->SetTextAndState(u"WATERMARK", textState);
    artifact->set_ArtifactHorizontalAlignment (HorizontalAlignment::Center);
    artifact->set_ArtifactVerticalAlignment (VerticalAlignment::Center);
    artifact->set_Rotation(45);
    artifact->set_Opacity(0.5);
    artifact->set_IsBackground(true);

    document->get_Pages()->idx_get(1)->get_Artifacts()->Add(artifact);

    document->Save(_dataDir + outputFileName);
}