前言
今天帮领导干了一个私活:pdf去水印,行政人员搞不定,非让我试试。我这该死的好胜欲!活该闲不住。
需求
加密PDF去水印,PDF文档有2000页,是的你没看错,是2000页,文档大小15MB。
常用方法尝试过了,都不行。
一、去水印可能会遇到如下问题:
1.因插入水印的工具不同,会有所区别。换个别的工具可能检测不到水印
2.大多加水印的PDF是加密的,删除水印会提示输入密码,需要先解密才行
3.大多情况下“Adobe Acrobat DC”工具都没啥问题。
二、我去水印的过程,和遇到的问题:
今为一个超大PDF文件去水印,文件有2000页,PDF水印是图片,而且PDF是加密的,以下是尝试处理过程:
1.这里推荐一款超好用的PDF:云上PDF,软件名:iPDFSetup (不是广告哦),它可以对PDF进行编辑,删除空白页,甚至还可以解密,目前版本完全免费:v1062070
2.使用云上PDF解密之后,使用“Adobe Acrobat DC”工具进行编辑-去水印,提示没有检测到水印。如果你使用云上PDF无法解密,到这里就放弃吧!哈哈
3.尝试下载:福昕、迅捷PDF,软件收费,放弃了
4.网页搜索在线PDF编辑:PDF365、Smallpdf/迅捷、风云水印管家等,失败!!
5.将PDF另存为word,使用word去除水印,也失败了。分别尝试了office和WPS
6.AI生成Python脚本,调试需要时间,未尝试
7.word和PDF都可手动删除,但2000的PDF滚动一下要等2秒钟,即使转化为word切换网页视图,效率也极差,而且相当一部分水印在表格的下方,删除水印需要移走表格。效率慢
三、最终使用word的“宏”进行删除,方法如下:
1.将2000页PDF转化为word文档,先复制其中的四五页(含水印),新建word并粘贴进去,命名为abc
2.进入abc文档,进入“视图”-“宏”-“查看”
3.随便创建一个aaa,如果有就选择编辑
4.或者你也可以按Alt+F11组合键,新建模块
5.粘贴如下脚本:(此脚本的目的是提取出水印图片在文档中的坐标,因为2000页的水印,有的坐标不一样(十分恶心))
Sub ListAllPictures()
Dim shp As Shape
Dim i As Long
Dim output As String
output = "图片编号,类型,左位置,上位置,宽度,高度" & vbCrLf
For i = 1 To ActiveDocument.Shapes.Count
Set shp = ActiveDocument.Shapes(i)
If shp.Type = msoPicture Or shp.Type = msoLinkedPicture Or shp.Type = msoEmbeddedOLEObject Then
output = output & i & "," & shp.Type & "," & shp.Left & "," & shp.Top & "," & shp.Width & "," & shp.Height & vbCrLf
End If
Next i
' 将结果输出到一个新文档
Dim newDoc As Document
Set newDoc = Documents.Add
newDoc.Content.Text = output
MsgBox "所有图片的位置和大小已列出在新文档中。"
End Sub
6.点击运行,会弹出如下页面:
根据图片位置信息,水印图片的位置和大小特征如下:
左位置:约 133.35 点
上位置:约 15.15 点到 20.45 点之间(有轻微变化)
宽度:345.25 点
高度:316.9 点
7.有了坐标,就可以继续编辑宏,做删除操作:
以下步骤在源文档操作(非abc).
Sub DeleteWatermarkPictures()
Dim shp As Shape
Dim i As Long
Dim targetLeft As Single
Dim targetTopMin As Single
Dim targetTopMax As Single
Dim targetWidth As Single
Dim targetHeight As Single
' 根据提供的位置和大小信息设置参数
targetLeft = 133.35 ' 左位置
targetTopMin = 15.15 ' 上位置的最小值
targetTopMax = 20.45 ' 上位置的最大值
targetWidth = 345.25 ' 宽度
targetHeight = 316.9 ' 高度
' 遍历文档中的所有形状
For i = ActiveDocument.Shapes.Count To 1 Step -1
Set shp = ActiveDocument.Shapes(i)
' 检查形状是否为图片
If shp.Type = msoPicture Or shp.Type = msoLinkedPicture Or shp.Type = msoEmbeddedOLEObject Then
' 检查图片的位置和大小是否匹配
If Abs(shp.Left - targetLeft) < 10 And _
shp.Top >= targetTopMin And shp.Top <= targetTopMax And _
Abs(shp.Width - targetWidth) < 10 And _
Abs(shp.Height - targetHeight) < 10 Then
shp.Delete
End If
End If
Next i
MsgBox "水印图片已删除完成!"
End Sub
8.点击运行,等待一段时间,会弹出:
9.点击确定,回到目标文档,发现水印少了一大半(只删除一部分额原因是其它水印的坐标不同)
10.继续按上面方法在abc中执行取坐标的操作:
图片编号,类型,左位置,上位置,宽度,高度
1,13,133.35,-58.45,345.25,316.9
2,13,133.35,-37.45,345.25,316.9
3,13,133.35,-58.45,345.25,316.9
4,13,133.35,7.8,345.25,316.9
5,13,133.35,7.8,345.25,316.9
6,13,133.35,-37.45,345.25,316.9
7,13,133.35,-37.45,345.25,316.9
修改脚本
Sub DeleteWatermarkPictures()
Dim shp As Shape
Dim i As Long
Dim targetLeft As Single
Dim targetTopMin As Single
Dim targetTopMax As Single
Dim targetWidth As Single
Dim targetHeight As Single
' 根据提供的位置和大小信息设置参数
targetLeft = 133.35 ' 左位置
targetTopMin = -58.45 ' 上位置的最小值
targetTopMax = 7.8 ' 上位置的最大值
targetWidth = 345.25 ' 宽度
targetHeight = 316.9 ' 高度
' 遍历文档中的所有形状
For i = ActiveDocument.Shapes.Count To 1 Step -1
Set shp = ActiveDocument.Shapes(i)
' 检查形状是否为图片
If shp.Type = msoPicture Or shp.Type = msoLinkedPicture Or shp.Type = msoEmbeddedOLEObject Then
' 检查图片的位置和大小是否匹配
If Abs(shp.Left - targetLeft) < 10 And _
shp.Top >= targetTopMin And shp.Top <= targetTopMax And _
Abs(shp.Width - targetWidth) < 10 And _
Abs(shp.Height - targetHeight) < 10 Then
shp.Delete
End If
End If
Next i
MsgBox "水印图片已删除完成!"
End Sub
剩下的零星未删除的水印,在源文档中,执行取坐标的操作,然后修改脚本,运行
最后将目标word另存为PDF,使用PDF查看有无遗漏水印(因为PDF阅读模式流畅,使用word相当卡)
剩下的零星几个,手动删除即可。
有个问题不知道什么原因:word最后生成的PDF,导航栏目录不见了,在word另存为PDF之前还是正常的