|
| 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 | +``` |
0 commit comments