Search code examples
pythonpdfpikepdf

deleting and moving elements in pikepdf


I'm trying to create a kind of page break function for pdfs using pikepdf.

Given some vertical position (dotted grey line in the image) I want to split the page into two pages. I want to keep the page size and move the elements under the line to the top of the next page.

enter image description here

I basically need to do 3 things:

  • List elements on page
  • Find the position of given elements
  • Move elements around the page

I'm struggling to find the relevant documentation on the pikepdf docs page. Could someone point me in the right direction?


Solution

  • You should be able to do this in cpdf (or its Python library version pycpdflib). From the command line, you might do (untested):

    cpdf in.pdf 1-3,3,4-end -o out.pdf
    

    (duplicate page three as new page 4)

    cpdf -trimbox "x y w h" out.pdf 3 AND -trimbox "x2 y2 w2 h2" -range 4 -o out2.pdf
    

    (crop the duplicate pages for suitable x y w h values)

    cpdf -hard-box /TrimBox out2.pdf 3,4 -o out3.pdf
    

    (actually trim the page content to the new trim boxes, baking it in)

    cpdf -remove-trim out3.pdf 3,4 -o out4.pdf
    

    (remove the trim box, restoring the original page dimensions but leaving the hard box in place)

    cpdf -shift "dx dy" out4.pdf 4 -o out5.pdf
    

    (shift the lower part up to the top of the page for some dx dy)