- 使用 C++ 在 PDF 文件中插入文本水印
- 使用 C++ 在 PDF 文件中插入图像水印
- 使用 C++ 从 PDF 文件中删除文本或图像水印
使用 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 documentauto pdfDocument = MakeObject(u"SourceDirectory\Sample 1.pdf");// Create an instance of the TextStamp classSystem::SharedPtrtextStamp = MakeObject(u"CONFIDENTIAL");// Set the position of the watermarktextStamp->set_XIndent(70);textStamp->set_YIndent(300);// Set text propertiestextStamp->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 pagepdfDocument->get_Pages()->idx_get(1)->AddStamp(textStamp);// Save the PDF filepdfDocument->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 documentauto pdfDocument = MakeObject(u"SourceDirectory\Sample 1.pdf");// Create an instance of the ImageStamp classSystem::SharedPtrimageStamp = MakeObject(u"SourceDirectory\aspose.png");// Set the position of the watermarkimageStamp->set_XIndent(150);imageStamp->set_YIndent(350);// Set other propertiesimageStamp->set_Height(100);imageStamp->set_Width(300);imageStamp->set_RotateAngle(45);imageStamp->set_Opacity(0.4);imageStamp->setStampId(12345678);// Add watermark to the PDF pagepdfDocument->get_Pages()->idx_get(1)->AddStamp(imageStamp);// Save the PDF filepdfDocument->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 classSystem::SharedPtrcontentEditor = MakeObject();// Open the PDF file containing the watermarkcontentEditor->BindPdf(u"SourceDirectory\SampleImageWatermark.pdf");// Delete watermark by idcontentEditor->DeleteStampById(12345678);// Save the PDF filepdfDocument->Save(u"OutputDirectory\Remove-Watermark-Out.pdf", SaveFormat::Pdf);
如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【Aspose最新资源在线文库】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!