通过 C++ 添加水印

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

使用 C++ 库添加水印

為了將浮浮浮浮水印添加到PDF檔中,我們將使用[Aspose.PDF for C++](https://products.aspose.com/pdf/cpp)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於C++平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf.cpp) 包管理器,搜索“阿波斯.PDF.Cpp”並進行安裝。您也可以從程式包管理器主控台使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

使用 C++ 添加水印


您需要 [Aspose.PDF for C++](https://releases.aspose.com/pdf/cpp) 才能在您的環境中嘗試代碼。

  1. 載入包含文件實例的 PDF。
  2. 建立浮浮浮浮水印藝術實例。
  3. 設定浮浮浮浮水印藝術品屬性。
  4. 使用添加方法添加浮浮水印。
  5. 儲存檔

在 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);
}