如何批量删除PDF文档中的”www.it-ebooks.info“

      经常在http://www.it-ebooks.info/下载电子教程,但是下载的教程每页都会打上www.it-ebooks.info的标签。之前也没太注意,最近每次看到这些标签都有点小烦躁,想着是不是可以批量去除,首先考虑是在Adobe Acrobat XI Pro下去除水印,但是很遗憾参考很多网上的教程都没有处理掉。萌生了写个脚本批量处理的想法,在谷歌搜到一个外国人写的相关分析和教程,我觉得挺好的,也就依葫芦画瓢演示一遍,算是留个笔记吧。

      大致思路是,用hex编辑器打开PDF文件,找到带有www.it-ebooks.info的文本,然后全部替换成为空即可。

      因为我的ubuntu14.04虚拟机不带Bless Hex编辑器,首先需要安装。命令如下:

apt-get install bless

       在ubuntu14.04中使用bless打开需要处理的PDF文档,如下图所示:

orig 
      接下来,我们来找这里面的www.it-ebooks.com字符串。在两个地方存在www.it-ebooks.com字符串,一个是在link中,一个是在text中。我们来看link中的www.it-ebooks.com。如下图所示:
link 
      图中选中的部分即是link中的www.it-ebooks.com。link中的结构如下:
<<
/Type /Annot
/Subtype /Link
/Rect [ 210 18.5 294 6.5 ]
/Border [ 0 0 0 ]
/A <<
/Type /Action
/S /URI
/URI (http://www.it-ebooks.info/)
>>
>>
      查看了一下《Developing with PDF》这本书,一对双角括号代表一个Dictionary对象,其中包含属性和对象的值。上面得到的就是link中的www.it-ebooks.com。接下来,我们来找text中的www.it-ebooks.com部分。如下图所示:
text 
      text中的结构如下:
BT
1 0 0 1 0 0 Tm
(www.it-ebooks.info)Tj
ET
      我们只要把以上两部分的内容替换为空字符就可以删除整个PDF文档中的www.it-ebooks.com水印了。
      接下来,我们编写一个python脚本来自动化进行这个过程:源码如下:
#!/usr/bin env python
# __author__ = 'hiccup'
import os
import sys
import binascii
import optparse

hex_link_pat = "".join("0A 3C 3C 0A 2F 54 79 70 65 20 2F 41 6E 6E 6F 74 0A 2F 53 75 "
                       "62 74 79 70 65 20 2F 4C 69 6E 6B 0A 2F 52 65 63 74 20 5B 20 "
                       "32 31 30 20 31 38 2E 35 20 32 39 34 20 36 2E 35 20 5D 0A 2F "
                       "42 6F 72 64 65 72 20 5B 20 30 20 30 20 30 20 5D 0A 2F 41 20 "
                       "3C 3C 0A 2F 54 79 70 65 20 2F 41 63 74 69 6F 6E 0A 2F 53 20 "
                       "2F 55 52 49 0A 2F 55 52 49 20 28 68 74 74 70 3A 2F 2F 77 77 "
                       "77 2E 69 74 2D 65 62 6F 6F 6B 73 2E 69 6E 66 6F 2F 29 0A 3E "
                       "3E 0A 3E 3E".split())

hex_text_pat = "".join("0A 42 54 0A 31 20 30 20 30 20 31 20 30 20 30 20 54 6D 0A 28 "
                       "77 77 77 2E 69 74 2D 65 62 6F 6F 6B 73 2E 69 6E 66 6F 29 54 "
                       "6A 0A 45 54".split())

def remove_watermark(path):
    pdf_bin_data=""
    if os.path.exists(path) and path.endswith(".pdf"):
        try:
            with open(path,"rb") as f:
                pdf_bin_data = f.read()
                pdf_bin_data = pdf_bin_data.replace(binascii.unhexlify(hex_link_pat),"")
                pdf_bin_data = pdf_bin_data.replace(binascii.unhexlify(hex_text_pat),"")
        except IOError:
            sys.stderr.write("Error in opening file")
    else:
        raise ValueError("Path invalid or file is not in PDF format")
    return pdf_bin_data

if __name__ == "__main__":
    parser = optparse.OptionParser("usage: %prog "+'-F <filepath>')
    parser.add_option('-F',dest='filepath',type='string',help='specify absolute path of pdf file')
    (options, args) = parser.parse_args()
    filepath = options.filepath
    if filepath == None:
        print parser.usage
        exit(0)
    newfile= 'new_'+os.path.split(filepath)[1]
    try:
        with open(newfile,'wb') as f:
            f.write(remove_watermark(filepath))
            f.close()
            print 'remove watermark done!'
    except IOError:
        sys.stderr.write("Problem in writing file.")
       参考文献:http://www.rawcoders.com/Thread-How-To-Remove-it-ebooks-info-watermark-from-pdfs
posted @ 2015-02-15 10:15  ShadonSniper  阅读(1017)  评论(0编辑  收藏  举报