Skip to content

Commit

Permalink
research on PDF files merge in horizontally
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoqi Zhao committed Dec 24, 2023
1 parent 5805d73 commit 264705f
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 9 deletions.
Binary file modified Python-with-Office-Video_Covers.pptx
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from PyPDF2 import PdfReader

reader = PdfReader("./a.pdf")

meta = reader.metadata

print(meta.author)

print(meta.creator)

print(meta.producer)

print(meta.title)

print(meta.subject)

for i in range(len(reader.pages)):
page = reader.pages[i].rotate(90)
page = reader.pages[i]
print(page.mediabox.lower_left, page.mediabox.lower_right, page.mediabox.upper_left, page.mediabox.upper_right)
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
# Purpose: Merge two PDFs (with same total pages) in side by side horizontally
# Author: Xiaoqi Zhao
# Version 2: 2023/12/24, remove dependency from PageObject
# Version 1: 2023/12/23, initialization

from PyPDF3 import PdfFileReader, PdfFileWriter
from PyPDF3.pdf import PageObject

pdf_filenames = ["./a.pdf", "./b.pdf"]
pdf_filenames = ["./a1.pdf", "./b1.pdf"]

reader1 = PdfFileReader(open(pdf_filenames[0], "rb"), strict=False)
reader2 = PdfFileReader(open(pdf_filenames[1], "rb"), strict=False)

writer = PdfFileWriter()

page1 = reader1.getPage(0)
page2 = reader2.getPage(0)

for i in range(len(reader1.pages)):

page1 = reader1.getPage(i)

page2 = reader2.getPage(i)

total_width = page1.mediaBox.upperRight[0] + page2.mediaBox.upperRight[0]
total_height = max([page1.mediaBox.upperRight[1], page2.mediaBox.upperRight[1]])
# print(total_width, total_height)
print(total_width, total_height)

new_page = writer.insertBlankPage(total_width, total_height, i)

new_page.mergePage(page1)
new_page.mergeTranslatedPage(page2, page1.mediaBox.upperRight[0], 0)

# writer.addPage(new_page)

writer.write(open("./h-merge_pypdf3-multipage.pdf", "wb"))
writer.write(open("./h-a-b.pdf", "wb"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Purpose: Merge two PDFs (with same total pages) in side by side horizontally
# Author: Xiaoqi Zhao
# Version 3: 2023/12/24, support merging of two PDFs with different total pages
# Version 2: 2023/12/24, remove dependency from PageObject
# Version 1: 2023/12/23, initialization

from PyPDF3 import PdfFileReader, PdfFileWriter

pdf_filenames = ["./grabit.pdf", "./High_Level_TQRDC.pdf"]

reader1 = PdfFileReader(open(pdf_filenames[0], "rb"), strict=False)
reader2 = PdfFileReader(open(pdf_filenames[1], "rb"), strict=False)

writer = PdfFileWriter()

for i in range(max(len(reader1.pages), len(reader2.pages))):

if i < len(reader1.pages):
page1 = reader1.getPage(i)

if i < len(reader2.pages):
page2 = reader2.getPage(i)

total_width = page1.mediaBox.upperRight[0] + page2.mediaBox.upperRight[0]
total_height = max([page1.mediaBox.upperRight[1], page2.mediaBox.upperRight[1]])
print(total_width, total_height)

new_page = writer.insertBlankPage(total_width, total_height, i)

if len(reader1.pages) > len(reader2.pages):
if i > len(reader2.pages):
new_page.mergePage(page1)
else:
new_page.mergePage(page1)
new_page.mergeTranslatedPage(page2, page1.mediaBox.upperRight[0], 0)
else:
if i > len(reader1.pages):
new_page.mergePage(page1)
else:
new_page.mergePage(page1)
new_page.mergeTranslatedPage(page2, page1.mediaBox.upperRight[0], 0)

writer.write(open("./test.pdf", "wb"))
Binary file not shown.

0 comments on commit 264705f

Please sign in to comment.