如何加密文档,使其不允许编辑文本或不允许从 pdf 文件复制内容?
我尝试设置不同的用户和管理员密码,但我仍然能够在 pdf 编辑器中编辑文本。
import pikepdf
from pikepdf import Pdf
pdf = Pdf.open("document.pdf")
pdf.save('output_filename.pdf', encryption=pikepdf.Encryption(owner="password", user="123", R=6))
pdf.close()
基本上,如果有没有密码的方法,如果我可以加密要编辑的文档,那就太好了。预先感谢。
您需要 PyPDF2(pip install PyPDF2)才能使用此脚本。试试这个:
import PyPDF2
#pdf_in_file = open("document.pdf",'rb')
pdf_in_file = open("document.pdf",'rb')
inputpdf = PyPDF2.PdfFileReader(pdf_in_file)
pages_no = inputpdf.numPages
output = PyPDF2.PdfFileWriter()
for i in range(pages_no):
inputpdf = PyPDF2.PdfFileReader(pdf_in_file)
output.addPage(inputpdf.getPage(i))
output.encrypt('password_u_want')
#with open("simple_password_protected.pdf", "wb") as outputStream:
with open("output_filename.pdf", "wb") as outputStream:
output.write(outputStream)
pdf_in_file.close()
这里是 PyPDF2 的详细信息:https://pythonhosted.org/PyPDF2/PdfFileWriter.html
@BroserBros 中的一些函数已被弃用 以下是所需的修复:
import PyPDF2
# pdf_in_file = open("document.pdf",'rb')
pdf_in_file = open("document.pdf", 'rb')
input_pdf = PyPDF2.PdfReader(pdf_in_file)
pages_no = len(input_pdf.pages)
output = PyPDF2.PdfWriter()
for i in range(pages_no):
input_pdf = PyPDF2.PdfReader(pdf_in_file)
output.add_page(input_pdf.pages[i])
output.encrypt("password_u_want")
# with open("simple_password_protected.pdf", "wb") as outputStream:
with open("output_protected_file.pdf", "wb") as outputStream:
output.write(outputStream)
pdf_in_file.close()
基本上你可以为那些不想像盲人一样复制的人设置权限,但你不能为那些想要复制可见内容的人设置权限。
您可以设置 Adobe 内部权限,以限制 Acrobat 和任何符合 Adobe 标准的阅读器(例如 Microsoft Edge 或类似阅读器)中的复制工作。
但是,任何非 Adobe PDF 编辑器(例如 Mozilla / Firefox 浏览器等)不需要尊重这些 Adobe 首选项。
根据您签署的开放源代码许可使用或编写 PDF 的标准(有趣的是,它本身受到限制),所以我需要从 Firefox 的浏览编辑器进行复制。
一旦文档成功“打开和解密”,合格的读者在技术上就可以访问文档的全部内容。 PDF 加密中没有任何固有的内容来强制执行加密字典中指定的文档权限。合规读者应尊重文档创建者的意图,根据文件中包含的权限限制用户对加密 PDF 文件的访问。
看