Can we contact two pdf files side by side #1383
-
Hi @JorjMcKie , Is there any option to show two pdf files side by side as shown in the attachment. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
Sure, use Using a few simplifying assumptions:
... the following snippet will do the job w, h = fitz.paper_size("a3-l") # width, height of output page (format ISO A3 landscape)
rleft = fitz.paper_rect("a4") # rectangle of left output page half (A4 portrait)
rright = rleft + (rleft.width, 0, rleft.width, 0) # the right side rectangle
src1 = fitz.open("file1.pdf") # contains pages for the left side
src2 = fitz.open("file2.pdf") # contains page sfor the right side
n = len(src1) # number of input pages in both files
doc = fitz.open() # output file
for i in range(n):
page = doc.new_page(width=w, height=h) # make new output page, A3-landscape
page.show_pdf_page(rleft, src1, n) # put input page on left output page part
page.show_pdf_page(rright, src2, n)
doc.save("side-by-side.pdf") With a few more or less obvious adaptions, you can also handle cases where input files have unequal page numbers, or to dynamically adapt the output page format, etc. You may also want to look at this discussion to see how an input file can be reformatted to show two pages in a row side-by-side on output ("2-up" formatting). |
Beta Was this translation helpful? Give feedback.
-
I think I don't understand: which previously opened files? Annotations cannot be shown by this technique. Remember that annotations are technically not a (permanent) part of a PDF. An annotation is valid for the PDF page on which it is shown. So input annots would have to be re-inserted (using diefferent coordinates etc., etc.) in the newly created, large target page. There is no support for this. pdfbytes = src1.convert_to_pdf() # doc is any document type, need not be PDF
newpdf1 = fitz.open("pdf", pdfbytes) # the document (re-) converted to PDF
# this newpdf will no longer have annotations, technically, but show them as normal, permanent content Whether this works or not, depends on the fonts used in the PDF: not all font types are supported for a PDF-to-PDF conversion. Another approach is to make temporary page images and show these side-by-side. This would roughly look like this: ...
dpi = 300 # choose quality of the page images
mat = fitz.Matrix(dpi/72, dpi/72) # make a zoom mattrix
for i in range(n):
page = doc.new_page(width=w, height=h)
pageleft = src1[i] # page for left image
pageright = src2[i] # page for right image
pixleft = pageleft.get_pixmap(matrix=mat) # make images
pixright = pageright.get_pixmap(matrix=mat)
page.insert_image(rleft, pixmap=pixleft)
page.insert_image(rright, pixmap=pixright)
... |
Beta Was this translation helpful? Give feedback.
-
When saving make sure you use garbage>=3 and deflate=True. Also experiment with the dpi. |
Beta Was this translation helpful? Give feedback.
-
what you could also try:
Page.insert_image(annotrect, pixmap=annot_pixmap) for all annotation pxmaps. |
Beta Was this translation helpful? Give feedback.
I think I don't understand: which previously opened files?
Annotations cannot be shown by this technique. Remember that annotations are technically not a (permanent) part of a PDF. An annotation is valid for the PDF page on which it is shown. So input annots would have to be re-inserted (using diefferent coordinates etc., etc.) in the newly created, large target page. There is no support for this.
But in many cases, you can convert a PDF such that annotations are converted to normal page content. Then the snippet above also works for the conversion result.
Here is how to do the conversion:
p…