diff --git a/CHANGELOG.md b/CHANGELOG.md index cd4e1e021..1822fba42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,7 +38,7 @@ - Apply black - Typo in Changelog -Full Changelog: https://github.com/py-pdf/PyPDF2/compare/2.4.2...2.4.3 +Full Changelog: https://github.com/py-pdf/PyPDF2/compare/2.4.2...2.5.0 ## Version 2.4.2, 2022-07-05 diff --git a/PyPDF2/_writer.py b/PyPDF2/_writer.py index 7a469407d..843549ab0 100644 --- a/PyPDF2/_writer.py +++ b/PyPDF2/_writer.py @@ -67,6 +67,7 @@ from .constants import EncryptionDictAttributes as ED from .constants import ( FieldDictionaryAttributes, + FieldFlag, FileSpecificationDictionaryEntries, GoToActionArguments, InteractiveFormDictEntries, @@ -75,7 +76,7 @@ from .constants import PagesAttributes as PA from .constants import StreamAttributes as SA from .constants import TrailerKeys as TK -from .constants import TypFitArguments +from .constants import TypFitArguments, UserAccessPermissions from .generic import ( ArrayObject, BooleanObject, @@ -110,6 +111,10 @@ logger = logging.getLogger(__name__) +OPTIONAL_READ_WRITE_FIELD = FieldFlag(0) +ALL_DOCUMENT_PERMISSIONS = UserAccessPermissions((2**31 - 1) - 3) + + class PdfWriter: """ This class supports writing PDF files out, given pages produced by another @@ -576,7 +581,10 @@ def appendPagesFromReader( self.append_pages_from_reader(reader, after_page_append) def update_page_form_field_values( - self, page: PageObject, fields: Dict[str, Any], flags: int = 0 + self, + page: PageObject, + fields: Dict[str, Any], + flags: FieldFlag = OPTIONAL_READ_WRITE_FIELD, ) -> None: """ Update the form field values for a given page from a fields dictionary. @@ -627,7 +635,10 @@ def update_page_form_field_values( ) def updatePageFormFieldValues( - self, page: PageObject, fields: Dict[str, Any], flags: int = 0 + self, + page: PageObject, + fields: Dict[str, Any], + flags: FieldFlag = OPTIONAL_READ_WRITE_FIELD, ) -> None: # pragma: no cover """ .. deprecated:: 1.28.0 @@ -699,7 +710,7 @@ def encrypt( user_pwd: str, owner_pwd: Optional[str] = None, use_128bit: bool = True, - permissions_flag: int = -1, + permissions_flag: UserAccessPermissions = ALL_DOCUMENT_PERMISSIONS, ) -> None: """ Encrypt this PDF file with the PDF Standard encryption handler. diff --git a/PyPDF2/constants.py b/PyPDF2/constants.py index ab5b55e55..a195c22fe 100644 --- a/PyPDF2/constants.py +++ b/PyPDF2/constants.py @@ -8,6 +8,7 @@ PDF Reference, sixth edition, Version 1.7, 2006. """ +from enum import IntFlag from typing import Dict, Tuple @@ -47,6 +48,43 @@ class EncryptionDictAttributes: ENCRYPT_METADATA = "/EncryptMetadata" # boolean flag, optional +class UserAccessPermissions(IntFlag): + """TABLE 3.20 User access permissions""" + + R1 = 1 + R2 = 2 + PRINT = 4 + MODIFY = 8 + EXTRACT = 16 + ADD_OR_MODIFY = 32 + R7 = 64 + R8 = 128 + FILL_FORM_FIELDS = 256 + EXTRACT_TEXT_AND_GRAPHICS = 512 + ASSEMBLE_DOC = 1024 + PRINT_TO_REPRESENTATION = 2048 + R13 = 2**12 + R14 = 2**13 + R15 = 2**14 + R16 = 2**15 + R17 = 2**16 + R18 = 2**17 + R19 = 2**18 + R20 = 2**19 + R21 = 2**20 + R22 = 2**21 + R23 = 2**22 + R24 = 2**23 + R25 = 2**24 + R26 = 2**25 + R27 = 2**26 + R28 = 2**27 + R29 = 2**28 + R30 = 2**29 + R31 = 2**30 + R32 = 2**31 + + class Ressources: """TABLE 3.30 Entries in a resource dictionary.""" @@ -294,6 +332,14 @@ def attributes_dict(cls) -> Dict[str, str]: } +class FieldFlag(IntFlag): + """TABLE 8.70 Field flags common to all field types""" + + READ_ONLY = 1 + REQUIRED = 2 + NO_EXPORT = 4 + + class DocumentInformationAttributes: """TABLE 10.2 Entries in the document information dictionary."""