引言
pdf中的水印注释是一种独特的注释类型,它通常以透明的文本或图片形式叠加在页面内容之上,为文档添加标识或信息提示。与传统的静态水印不同,水印注释并不会永久嵌入到pdf页面的内容中,而是以独立注释对象的形式存在。这种设计结合了水印的标记功能与注释的灵活性,使用户能够方便地删除水印,而无需影响文档的核心内容。非常适合标记 “草稿” 或 “保密” 文件,或为文档添加临时说明等场景。这篇博客将探讨如何使用c# 在pdf文档中添加和删除水印注释。
- c# 在pdf文档中添加水印注释
- c# 在pdf文档中删除水印注释
使用工具
要使用c# 在pdf文档中添加和删除水印注释,需要用到合适的pdf文档处理库。本文所使用的是spire.pdf for .net库。该库主要用于在 .net 应用程序中创建、读取、编辑、转换 和打印pdf 文档。
安装 spire.pdf for .net
你可以在 nuget 包管理器中运行以下命令安装 spire.pdf for .net:
pm> install-package spire.pdf
c# 在pdf文档中添加水印注释
spire.pdf for .net中的pdfwatermarkannotation类用于创建水印注释。创建后,你可以将其添加到你的pdf文档的页面中。以下是将水印注释添加到pdf文档的具体步骤:
- 加载pdf文档: 创建pdfdocument对象,并使用loadfromfile方法加载目标pdf文件。
- 设置水印文本的字体: 创建一个pdftruetypefont对象,设置所需的字体、大小和样式(例如,宋体、22pt、常规)。此字体将用于呈现水印文本。
- 生成水印外观模板: 使用pdftemplate对象定义一个与页面大小匹配的画布。在此模板上,使用pdftilingbrush对象绘制水印文本。
- 遍历页面并应用水印: 遍历pdf文档的所有页面。对于每一页:
- 使用rectanglef对象定义水印注释的区域,确保其覆盖整个页面。
- 创建pdfwatermarkannotation对象,并为其指定区域。
- 设置水印注释的外观为预定义的模板。
- 将水印注释添加到页面的annotations集合中。
- 保存修改后的pdf: 使用savetofile方法将添加水印注释后的pdf文档保存为新文件。
以下代码展示了如何使用c#为pdf文档添加水印注释:
using spire.pdf; using spire.pdf.annotations; using spire.pdf.annotations.appearance; using spire.pdf.graphics; using system.drawing; namespace watermarkannotation { internal class program { static void main(string[] args) { // 创建pdfdocument对象 pdfdocument pdf = new pdfdocument(); // 加载pdf文档 pdf.loadfromfile("测试.pdf"); // 创建pdftruetypefont对象,用于渲染水印文本 pdftruetypefont font = new pdftruetypefont(new font("宋体", 22.0f, fontstyle.regular), true); // 遍历所有页面 for (int i = 0; i < pdf.pages.count; i++) { pdfpagebase page = pdf.pages[i]; // 创建与页面大小匹配的pdftemplate对象 pdftemplate template = new pdftemplate(page.getclientsize().width, page.getclientsize().height); // 调用函数将水印文本插入模板 insertwatermark(template, font, "保密"); // 定义页面上应用水印注释的区域 rectanglef lorect = new rectanglef(0.0f, 0.0f, page.getclientsize().width, page.getclientsize().height); // 创建水印注释 pdfwatermarkannotation watermarkannotation = new pdfwatermarkannotation(lorect); // 设置水印注释的外观 pdfappearance appearance = new pdfappearance(watermarkannotation); appearance.normal = template; watermarkannotation.appearance = appearance; watermarkannotation.text = "id_0"; // 设置矩阵变换以控制水印的位置和缩放 watermarkannotation.setmatrix(new float[] { 1, 0, 0, 1, 0, 0 }); // 设置水平偏移量 watermarkannotation.sethorizontaltranslation(0.5f); // 设置垂直偏移量 watermarkannotation.setverticaltranslation(0.5f); // 将水印注释添加到当前页面 page.annotations.add(watermarkannotation); } // 保存更新后的pdf文档 pdf.savetofile("添加水印注释.pdf"); pdf.dispose(); } // 定义一个静态方法用于在pdf模板中绘制水印 static void insertwatermark(pdftemplate template, pdftruetypefont font, string watermark) { // 创建一个sizef对象,用于设置水印刷子的大小 sizef size = new sizef(template.width / 2, template.height / 3); // 创建pdftilingbrush对象,用于重复绘制水印图案 pdftilingbrush brush = new pdftilingbrush(size); // 设置水印透明度 brush.graphics.settransparency(0.3f); // 保存当前graphic状态 brush.graphics.save(); // 平移graphic,使其中心与水印刷子的中心对齐 brush.graphics.translatetransform(brush.size.width / 2, brush.size.height / 2); // 旋转graphic,目的是使水印倾斜45度 brush.graphics.rotatetransform(-45); // 在graphic上绘制水印文字,使用指定的字体、颜色和居中对齐方式 brush.graphics.drawstring(watermark, font, pdfbrushes.violet, 0, 0, new pdfstringformat(pdftextalignment.center)); brush.graphics.restore(); // 重置水印透明度为1 brush.graphics.settransparency(1f); // 将水印图案绘制到整个模板区域 rectanglef lorect = new rectanglef(0.0f, 0.0f, template.size.width, template.size.height); template.graphics.drawrectangle(brush, lorect); } } }
c# 在pdf文档中删除水印注释
在某些情况下,你可能需要从pdf文档中删除水印注释,特别是在准备最终版本文档时,或者当水印不再需要时。以下是从pdf文档中删除水印注释的具体步骤:
- 加载pdf文档: 创建pdfdocument对象,并使用loadfromfile方法加载包含水印注释的pdf文件。
- 遍历页面: 使用for循环遍历pdf文档中的所有页面。对于每一页:
- 访问页面的annotations集合,该集合包含所有应用于页面的注释。
- 识别并删除水印注释: 在每页的注释集合中:
- 循环遍历集合中的注释。
- 检查当前注释是否为pdfwatermarkannotation类型(水印注释)。
- 如果注释是pdfwatermarkannotation类型,则从页面的annotations集合中删除它。
- 保存更新后的pdf: 使用savetofile方法将删除水印注释后的pdf文档保存为新文件。
以下代码展示了如何使用c#从pdf文档中删除水印注释:
using spire.pdf; using spire.pdf.annotations; namespace removewatermarkannotation { internal class program { static void main(string[] args) { // 创建pdfdocument对象 pdfdocument pdf = new pdfdocument(); // 加载包含水印注释的pdf文档 pdf.loadfromfile("添加水印注释.pdf"); // 遍历所有页面 for (int i = 0; i < pdf.pages.count; i++) { var annotations = pdf.pages[i].annotations; // 遍历所有注释 for (int j = annotations.count - 1; j >= 0; j--) { // 如果是水印注释,则移除 if (annotations[j] is pdfwatermarkannotation) { annotations.remove(annotations[j]); } } } // 保存更新后的pdf文档 pdf.savetofile("删除水印注释.pdf"); pdf.dispose(); } } }
以上就是使用c# 在pdf中添加和删除水印注释的全部内容。感谢阅读!
到此这篇关于使用c#在pdf中添加和删除水印注释的文章就介绍到这了,更多相关c# pdf添加和删除水印内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论