-
| How can I change the rect of a page? (Pdb) newpage
page 2 of <new PDF, doc# 2>
(Pdb) page
{'rect': Rect(0.0, 0.0, 720.0, 540.0), 'image': <_io.BytesIO object at 0x7fe48694cf68>}
(Pdb) newpage.rect
Rect(0.0, 0.0, 595.0, 842.0)
(Pdb) newpage.rect = page["rect"]
*** AttributeError: can't set attribute | 
Beta Was this translation helpful? Give feedback.
      
      
          Answered by
          
            JorjMcKie
          
      
      
        Jan 20, 2021 
      
    
    Replies: 1 comment 1 reply
-
| 
 Sure you can do that  ... at the risk of displacing stuff already on the page. But if the page is empty, simply do  >>> import fitz
>>> doc=fitz.open()
>>> page=doc.new_page()
>>> page.rect
Rect(0.0, 0.0, 595.0, 842.0)
>>> page.setMediaBox(fitz.Rect(fitz.PaperRect("a3")))
>>> page.rect
Rect(0.0, 0.0, 842.0, 1191.0)
>>> In addition, once you have inserted a few things of which you know which rectangle  rect = fitz.Rect(100, 100, 400, 500)  # assuming this contains the stuff that should be visible
page.setCropBoc(rect)
>>> page.rect
Rect(0.0, 0.0, 300.0, 400.0)
>>> # but the MediaBox is left unchanged!
>>> page.MediaBox
Rect(0.0, 0.0, 842.0, 1191.0)
>>> If you print the page or make a pixmap of it, then only the cropbox is shown ... | 
Beta Was this translation helpful? Give feedback.
                  
                    1 reply
                  
                
            
      Answer selected by
        jloehel
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Sure you can do that ... at the risk of displacing stuff already on the page. But if the page is empty, simply do
page.setMediaBox(rect):In addition, once you have inserted a few things of which you know which rectangle
rectcontains them all, you can "shrink" the visible page rectangle to that rect: