Skip to content

Commit b899d3d

Browse files
authored
MAINT: Rename PdfWriter.create_viewer_preferences to PdfWriter.create_viewer_preferences (#2190)
Add documentation and fix a method name as discussed in #2105
1 parent 02f2aa6 commit b899d3d

File tree

6 files changed

+86
-8
lines changed

6 files changed

+86
-8
lines changed

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ history and [GitHubs 'Contributors' feature](https://github.com/py-pdf/pypdf/gra
5454
* [WevertonGomes](https://github.com/WevertonGomesCosta)
5555
* [Wilson, Huon](https://github.com/huonw)
5656
* ztravis
57+
* [Stober, Marc](https://github.com/marcstober)
5758

5859
## Adding a new contributor
5960

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ You can contribute to `pypdf on GitHub <https://github.com/py-pdf/pypdf>`_.
3535
user/add-watermark
3636
user/reading-pdf-annotations
3737
user/adding-pdf-annotations
38+
user/viewer-preferences
3839
user/forms
3940
user/streaming-data
4041
user/file-size

docs/user/viewer-preferences.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Adding Viewer Preferences
2+
3+
It is possible to set viewer preferences of the PDF file.
4+
These properties are described in Section 12.2 of the [PDF 1.7 specification](https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf).
5+
6+
Note that the `\ViewerPreferences` dictionary does not exist by default.
7+
If it's not already present, it must be created by calling the `create_viewer_preferences` method
8+
of the `PdfWriter` object.
9+
10+
If viewer preferences exist in a PDF file being read with `PdfReader`,
11+
you can access them as properties of the `viewer_preferences` properties.
12+
Otherwise, the `viewer_preferences` property will be set to `None`.
13+
14+
## Example
15+
16+
```python
17+
from pypdf import PdfWriter
18+
from pypdf.generic import ArrayObject, NumberObject
19+
20+
writer = PdfWriter()
21+
22+
writer.create_viewer_preferences()
23+
24+
# /HideToolbar
25+
writer.viewer_preferences.hide_toolbar = True
26+
# /HideMenubar
27+
writer.viewer_preferences.hide_menubar = True
28+
# /HideWindowUI
29+
writer.viewer_preferences.hide_windowui = True
30+
# /FitWindow
31+
writer.viewer_preferences.fit_window = True
32+
# /CenterWindow
33+
writer.viewer_preferences.center_window = True
34+
# /DisplayDocTitle
35+
writer.viewer_preferences.display_doctitle = True
36+
37+
# /NonFullScreenPageMode
38+
writer.viewer_preferences.non_fullscreen_pagemode = "/UseNone" # default
39+
writer.viewer_preferences.non_fullscreen_pagemode = "/UseOutlines"
40+
writer.viewer_preferences.non_fullscreen_pagemode = "/UseThumbs"
41+
writer.viewer_preferences.non_fullscreen_pagemode = "/UseOC"
42+
43+
# /Direction
44+
writer.viewer_preferences.direction = "/L2R" # default,
45+
writer.viewer_preferences.direction = "/R2L"
46+
47+
# /ViewArea
48+
writer.viewer_preferences.view_area = "/CropBox"
49+
# /ViewClip
50+
writer.viewer_preferences.view_clip = "/CropBox"
51+
# /PrintArea
52+
writer.viewer_preferences.print_area = "/CropBox"
53+
# /PrintClip
54+
writer.viewer_preferences.print_clip = "/CropBox"
55+
56+
# /PrintScaling
57+
writer.viewer_preferences.print_scaling = "/None"
58+
writer.viewer_preferences.print_scaling = "/AppDefault" # default according to PDF spec
59+
60+
# /Duplex
61+
writer.viewer_preferences.duplex = "/Simplex"
62+
writer.viewer_preferences.duplex = "/DuplexFlipShortEdge"
63+
writer.viewer_preferences.duplex = "/DuplexFlipLongEdge"
64+
65+
# /PickTrayByPDFSize
66+
writer.viewer_preferences.pick_tray_by_pdfsize = True
67+
# /PrintPageRange
68+
writer.viewer_preferences.print_pagerange = ArrayObject(
69+
[NumberObject("1"), NumberObject("10"), NumberObject("20"), NumberObject("30")]
70+
)
71+
# /NumCopies
72+
writer.viewer_preferences.num_copies = 2
73+
74+
for i in range(40):
75+
writer.add_blank_page(10, 10)
76+
77+
with open("output.pdf", "wb") as output_stream:
78+
writer.write(output_stream)
79+
```

pypdf/_writer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def viewer_preferences(self) -> Optional[ViewerPreferences]:
384384
self._root_object[NameObject(CD.VIEWER_PREFERENCES)] = o
385385
return o
386386

387-
def create_viewer_preference(self) -> ViewerPreferences:
387+
def create_viewer_preferences(self) -> ViewerPreferences:
388388
o = ViewerPreferences()
389389
self._root_object[NameObject(CD.VIEWER_PREFERENCES)] = self._add_object(o)
390390
return o

pypdf/generic/_viewerpref.py

-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ def _add_prop_int(key: str, deft: Optional[int]) -> property:
140140
cls.print_pagerange = _add_prop_arr("/PrintPageRange", None)
141141
cls.num_copies = _add_prop_int("/NumCopies", None)
142142

143-
# still to be done /PrintPageRange and /NumCopies
144-
145143
return DictionaryObject.__new__(cls)
146144

147145
def __init__(self, obj: Optional[DictionaryObject] = None) -> None:

tests/test_writer.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1774,10 +1774,7 @@ def test_damaged_pdf_length_returning_none():
17741774

17751775
@pytest.mark.enable_socket()
17761776
def test_viewerpreferences():
1777-
"""
1778-
Add Tests for ViewerPreferences
1779-
https://github.com/py-pdf/pypdf/issues/140#issuecomment-1685380549
1780-
"""
1777+
"""Add Tests for ViewerPreferences"""
17811778
url = "https://github.com/py-pdf/pypdf/files/9175966/2015._pb_decode_pg0.pdf"
17821779
name = "2015._pb_decode_pg0.pdf"
17831780
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
@@ -1828,8 +1825,10 @@ def test_viewerpreferences():
18281825
v.print_pagerange = ArrayObject()
18291826
assert len(v.print_pagerange) == 0
18301827

1831-
writer.create_viewer_preference()
1828+
writer.create_viewer_preferences()
18321829
assert len(writer._root_object["/ViewerPreferences"]) == 0
1830+
writer.viewer_preferences.direction = "/R2L"
1831+
assert len(writer._root_object["/ViewerPreferences"]) == 1
18331832

18341833
del reader.trailer["/Root"]["/ViewerPreferences"]
18351834
assert reader.viewer_preferences is None

0 commit comments

Comments
 (0)