Viewer Preferences¶
A PDF document may include viewer preferences instructing a PDF reader on how it should display a document either on screen or in print.
Accessing the viewer preferences can be done by using the PdfDocument.viewer_preferences attribute. Note that this field may be absent, in which case, the PDF reader decides how to display the document.
from pdfnaut import PdfDocument
pdf = PdfDocument.from_filename("sample.pdf")
print(pdf.viewer_preferences) # ViewerPreferences(display_doc_title=True)
The ViewerPreferences class includes a list of all available viewer preferences. They can be grouped as follows:
Viewer preferences for interactive PDF processors, such as
hide_menubar,display_doc_title, anddirection.Pre-press viewer preferences, such as
view_areaandprint_area. These were deprecated in PDF 2.0Viewer preferences when printing, such as
print_scalingornum_copies.
PDF 2.0 conforming documents may also enforce viewer preferences by means of the ViewerPreferences.enforce attribute. Currently, the only enforceable preference is ViewerPreferences.print_scaling.
Preferences may be modified via their attribute. Setting them to None removes the viewer preference from the document. It is worth noting that all viewer preferences are optional.
from pdfnaut import PdfDocument
pdf = PdfDocument.from_filename("sample.pdf")
pdf.viewer_preferences.center_window = True
To remove the viewer preferences dictionary entirely, you can set PdfDocument.viewer_preferences to None.
Adding viewer preferences to a document can be done by creating an instance of ViewerPreferences.
from pdfnaut import PdfDocument
from pdfnaut.objects import ViewerPreferences
pdf = PdfDocument.from_filename("sample.pdf")
pdf.viewer_preferences = ViewerPreferences(display_doc_title=True)
pdf.save("view-prefs-sample.pdf")
Page Mode and Page Layout¶
Though not directly part of the viewer preferences, PdfDocument.page_mode and PdfDocument.page_layout also influence how a document is shown.
If a document’s page mode is set to “FullScreen”, the ViewerPreferences.non_full_screen_page_mode may be used to specify the page mode used when full screen mode is exited.