{"id":529,"date":"2022-01-13T18:10:02","date_gmt":"2022-01-13T23:10:02","guid":{"rendered":"http:\/\/kennywu.info\/?p=529"},"modified":"2022-01-17T09:58:05","modified_gmt":"2022-01-17T14:58:05","slug":"how-to-remove-watermark-in-a-pdf-file","status":"publish","type":"post","link":"http:\/\/kennywu.info\/?p=529","title":{"rendered":"How to remove watermark in a pdf file"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>def remove_watermark(wm_text, inputFile, outputFile):\n    from PyPDF4 import PdfFileReader, PdfFileWriter\n    from PyPDF4.pdf import ContentStream\n    from PyPDF4.generic import TextStringObject, NameObject\n    from PyPDF4.utils import b_\n    \n    with open(inputFile, \"rb\") as f:\n        source = PdfFileReader(f, \"rb\")\n        output = PdfFileWriter()\n\n        for page in range(source.getNumPages()):\n            page = source.getPage(page)\n            content_object = page&#91;\"\/Contents\"].getObject()\n            content = ContentStream(content_object, source)\n\n            for operands, operator in content.operations:\n                if operator == b_(\"Tj\"):\n                    text = operands&#91;0]\n\n                    if isinstance(text, str) and text.startswith(wm_text):\n                        operands&#91;0] = TextStringObject('')\n\n            page.__setitem__(NameObject('\/Contents'), content)\n            output.addPage(page)\n\n        with open(outputFile, \"wb\") as outputStream:\n            output.write(outputStream)\n            \nwm_text = 'wm_text'\ninputFile = r'input.pdf'\noutputFile = r\"output.pdf\"\nremove_watermark(wm_text, inputFile, outputFile)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/gist.github.com\/668\/2c8f936697ded94394ff4a6ffa4ae87e\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/github.com\/chazeon\/PDF-Watermark-Remover\/blob\/master\/remove.py\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-529","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=529"}],"version-history":[{"count":3,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions"}],"predecessor-version":[{"id":549,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions\/549"}],"wp:attachment":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}