翻译|使用教程|编辑:李显亮|2021-06-04 11:21:55.567|阅读 319 次
概述:水印可以是基于图像或文本的,并且经常用于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@evget.com