文档中的水印用机密、草稿等文本标识文档的状态,使原始文档难以复制。水印可以是基于图像或文本的,并且经常用于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),我们很高兴为您提供查询和咨询。