Search code examples
pythonpython-3.xpdfadobeacrobat

How to tick "choose paper source by pdf page size" in adobe acrobat reader


I have a pdf. I'd like to be able to set the "Choose paper source by PDF page size" checkbox in adobe acrobat reader. I could've sworn some time ago, I'd found a way to set it to be on all the time by setting an option in the viewer preferences, but after scouring through the preferences and internet, I have yet to find it again. I have seen some posts about a java and c# SDK called "iTextSharp", but unfortunately, python has no iTextSharp install in pip (from what I could tell). Is there any python library that supports such a feature? There seems to be something that you could include in the metadata, per this answer, but I haven't been able to find an implementation of that nor what to set in the page that I am assuming they are referencing.

From a pure python standpoint, I am open to any library that is MIT, BSD, or of the like. No GPL. I have used pypdfium2, PyPDF2, and fitz to try and just get something to work with the help of a language model, but naturally, it's not very helpful.


Solution

  • This answer says that PyPDF2 is deprecated. Which prompted me to look into pypdf, per their suggestion. I found a viewer_preferences attribute on the writer (and the create_viewer_preferences method with it). When I ran dir(viewer_preferences) on the viewer_preferences object attached to it, it had other attributes available to set, such as pick_tray_by_pdfsize. Testing this on my PDF, it worked like a charm. The bottom of page 362 is the beginning of the Viewer Preferences data object from adobe.

    import pypdf
    
    writer = pypdf.PdfWriter(copy_from="/my/pdf.pdf")
    writer.create_viewer_preferences()
    writer.viewer_preferences.pick_tray_by_pdfsize = True # or False
    writer.write("some form of io - a string path, bytesio, etc.")
    

    Edit:

    While this works on my computer, I am having issues with it on another. I think it's because there were some preferences set in adobe reader that are preventing it from taking precedence. So I've been trying to figure that out since after discovering this, yesterday.

    Edit:

    Always restart the application, restart your computer, or both. It's Gandolf level wizardry.