文檔中的水印用機密、草稿等文本標識文檔的狀態,使原始文檔難以複製。水印可以是基於圖像或文本的,並且經常用於PDF文檔中。有時可能需要在 C++ 應用程序中爲 PDF 文檔添加水印。爲此,本文將介紹如何使用 C++ 在 PDF 文檔中添加和刪除文本和圖像水印。
- 使用 C++ 在 PDF 文件中插入文本水印
- 使用 C++ 在 PDF 文件中插入圖像水印
- 使用 C++ 從 PDF 文件中刪除文本或圖像水印
Aspose.PDF for C++是一個 C++ 庫,允許您創建、閱讀和更新 PDF 文檔。此外,該 API 支持在 PDF 文檔中插入和刪除圖像/文本水印。點擊下方按鈕可下載試用。下載最新版Aspose.PDF for C++
使用 C++ 在 PDF 文件中插入文本水印
使用 Aspose.PDF for C++ API,您可以控制文本水印的字體樣式、文本、顏色、角度、不透明度等。以下是在 PDF 文件中添加文本水印的步驟。
- 使用Document 類加載 PDF 文件。
- 使用水印文本創建TextStamp類的實例。
- 設置水印的位置和其他屬性。
- 使用Document->get_Pages()->idx_get(1)->AddStamp(System::SharedPtrstamp)方法將水印添加到頁面。
- 使用Document->Save(System::String outputFileName, SaveFormat format)方法保存 PDF 文件 。
以下示例代碼顯示瞭如何使用 C++ 在 PDF 文件中添加文本水印。
// Open the source PDF document auto pdfDocument = MakeObject(u"SourceDirectory\\Sample 1.pdf"); // Create an instance of the TextStamp class System::SharedPtrtextStamp = MakeObject(u"CONFIDENTIAL"); // Set the position of the watermark textStamp->set_XIndent(70); textStamp->set_YIndent(300); // Set text properties textStamp->get_TextState()->set_Font(FontRepository::FindFont(u"Arial")); textStamp->get_TextState()->set_FontSize(72.0F); textStamp->get_TextState()->set_ForegroundColor(Aspose::Pdf::Color::get_Red()); textStamp->set_Opacity(0.4); textStamp->set_RotateAngle(45); textStamp->setStampId(123456); // Add watermark to the PDF page pdfDocument->get_Pages()->idx_get(1)->AddStamp(textStamp); // Save the PDF file pdfDocument->Save(u"OutputDirectory\\Text-Watermark-Out.pdf", SaveFormat::Pdf);
使用 C++ 在 PDF 文件中插入圖像水印
圖像水印通常用於通過使用徽標或任何其他可識別圖像來顯示文檔的所有權。您可以使用以下步驟爲 PDF 文件添加圖像水印。
- 使用Document 類加載 PDF 文件。
- 使用水印圖像創建ImageStamp類的實例。
- 設置水印的位置和其他屬性。
- 使用Document->get_Pages()->idx_get(1)->AddStamp(System::SharedPtrstamp)方法將水印添加到頁面。
- 使用Document->Save(System::String outputFileName, SaveFormat format)方法保存 PDF 文件 。
以下示例代碼演示瞭如何使用 C++ 在 PDF 文件中添加圖像水印。
// Open the source PDF document auto pdfDocument = MakeObject(u"SourceDirectory\\Sample 1.pdf"); // Create an instance of the ImageStamp class System::SharedPtrimageStamp = MakeObject(u"SourceDirectory\\aspose.png"); // Set the position of the watermark imageStamp->set_XIndent(150); imageStamp->set_YIndent(350); // Set other properties imageStamp->set_Height(100); imageStamp->set_Width(300); imageStamp->set_RotateAngle(45); imageStamp->set_Opacity(0.4); imageStamp->setStampId(12345678); // Add watermark to the PDF page pdfDocument->get_Pages()->idx_get(1)->AddStamp(imageStamp); // Save the PDF file pdfDocument->Save(u"OutputDirectory\\Image-Watermark-Out.pdf", SaveFormat::Pdf);
使用 C++ 從 PDF 文件中刪除文本或圖像水印
在某些情況下,您可能需要從 PDF 文檔中刪除水印。在前面的示例中,我們爲文本和圖像水印定義了 id。我們將使用這些 id 從 PDF 文檔中刪除水印。以下是從 PDF 文件中去除水印的步驟。
- 創建PdfContentEditor類的實例。
- 使用PdfContentEditor->BindPdf(System::String inputFile)方法加載 PDF 文檔。
- 使用PdfContentEditor->DeleteStampById(int32_t stampId)方法刪除水印。
- 使用Document->Save(System::String outputFileName, SaveFormat format)方法保存 PDF 文件 。
以下示例代碼顯示瞭如何使用 C++ 從 PDF 文件中刪除文本和圖像水印。
// Create an instance of the PdfContentEditor class System::SharedPtrcontentEditor = MakeObject(); // Open the PDF file containing the watermark contentEditor->BindPdf(u"SourceDirectory\\SampleImageWatermark.pdf"); // Delete watermark by id contentEditor->DeleteStampById(12345678); // Save the PDF file pdfDocument->Save(u"OutputDirectory\\Remove-Watermark-Out.pdf", SaveFormat::Pdf);
如果您有任何疑問或需求,請隨時加入Aspose技術交流羣(761297826),我們很高興爲您提供查詢和諮詢。