翻譯|使用教程|編輯:李顯亮|2021-06-04 11:21:55.567|閱讀 378 次
概述:水印可以是基于圖像或文本的,并且經常用于PDF文檔中。有時可能需要在 C++ 應用程序中為 PDF 文檔添加水印。為此,本文將介紹如何使用 C++ 在 PDF 文檔中添加和刪除文本和圖像水印。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
文檔中的水印用機密、草稿等文本標識文檔的狀態,使原始文檔難以復制。水印可以是基于圖像或文本的,并且經常用于PDF文檔中。有時可能需要在 C++ 應用程序中為 PDF 文檔添加水印。為此,本文將介紹如何使用 C++ 在 PDF 文檔中添加和刪除文本和圖像水印。
Aspose.PDF for C++是一個 C++ 庫,允許您創建、閱讀和更新 PDF 文檔。此外,該 API 支持在 PDF 文檔中插入和刪除圖像/文本水印。點擊下方按鈕可下載試用。
使用 Aspose.PDF for C++ API,您可以控制文本水印的字體樣式、文本、顏色、角度、不透明度等。以下是在 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);
圖像水印通常用于通過使用徽標或任何其他可識別圖像來顯示文檔的所有權。您可以使用以下步驟為 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);
在某些情況下,您可能需要從 PDF 文檔中刪除水印。在前面的示例中,我們為文本和圖像水印定義了 id。我們將使用這些 id 從 PDF 文檔中刪除水印。以下是從 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的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn