Skip to content

Commit 5ae8981

Browse files
authored
add the '/Fit' destination (#2009)
1 parent adbed04 commit 5ae8981

File tree

1 file changed

+68
-46
lines changed

1 file changed

+68
-46
lines changed

crazy_functions/latex_fns/latex_toolbox.py

Lines changed: 68 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -697,15 +697,6 @@ def _merge_pdfs_ng(pdf1_path, pdf2_path, output_path):
697697
),
698698
0,
699699
)
700-
if "/Annots" in page1:
701-
page1_annot_id = [annot.idnum for annot in page1["/Annots"]]
702-
else:
703-
page1_annot_id = []
704-
705-
if "/Annots" in page2:
706-
page2_annot_id = [annot.idnum for annot in page2["/Annots"]]
707-
else:
708-
page2_annot_id = []
709700
if "/Annots" in new_page:
710701
annotations = new_page["/Annots"]
711702
for i, annot in enumerate(annotations):
@@ -720,7 +711,8 @@ def _merge_pdfs_ng(pdf1_path, pdf2_path, output_path):
720711
if "/S" in action and action["/S"] == "/GoTo":
721712
# 内部链接:跳转到文档中的某个页面
722713
dest = action.get("/D") # 目标页或目标位置
723-
if dest and annot.idnum in page2_annot_id:
714+
# if dest and annot.idnum in page2_annot_id:
715+
if dest in pdf2_reader.named_destinations:
724716
# 获取原始文件中跳转信息,包括跳转页面
725717
destination = pdf2_reader.named_destinations[
726718
dest
@@ -732,24 +724,39 @@ def _merge_pdfs_ng(pdf1_path, pdf2_path, output_path):
732724
)
733725
# 更新跳转信息,跳转到对应的页面和,指定坐标 (100, 150),缩放比例为 100%
734726
# “/D”:[10,'/XYZ',100,100,0]
735-
annot_obj["/A"].update(
736-
{
737-
NameObject("/D"): ArrayObject(
738-
[
739-
NumberObject(page_number),
740-
destination.dest_array[1],
741-
FloatObject(
742-
destination.dest_array[2]
743-
+ int(
744-
page1.mediaBox.getWidth()
745-
)
746-
),
747-
destination.dest_array[3],
748-
destination.dest_array[4],
749-
]
750-
) # 确保键和值是 PdfObject
751-
}
752-
)
727+
if destination.dest_array[1] == "/XYZ":
728+
annot_obj["/A"].update(
729+
{
730+
NameObject("/D"): ArrayObject(
731+
[
732+
NumberObject(page_number),
733+
destination.dest_array[1],
734+
FloatObject(
735+
destination.dest_array[
736+
2
737+
]
738+
+ int(
739+
page1.mediaBox.getWidth()
740+
)
741+
),
742+
destination.dest_array[3],
743+
destination.dest_array[4],
744+
]
745+
) # 确保键和值是 PdfObject
746+
}
747+
)
748+
else:
749+
annot_obj["/A"].update(
750+
{
751+
NameObject("/D"): ArrayObject(
752+
[
753+
NumberObject(page_number),
754+
destination.dest_array[1],
755+
]
756+
) # 确保键和值是 PdfObject
757+
}
758+
)
759+
753760
rect = annot_obj.get("/Rect")
754761
# 更新点击坐标
755762
rect = ArrayObject(
@@ -773,7 +780,9 @@ def _merge_pdfs_ng(pdf1_path, pdf2_path, output_path):
773780
): rect # 确保键和值是 PdfObject
774781
}
775782
)
776-
if dest and annot.idnum in page1_annot_id:
783+
# if dest and annot.idnum in page1_annot_id:
784+
if dest in pdf1_reader.named_destinations:
785+
777786
# 获取原始文件中跳转信息,包括跳转页面
778787
destination = pdf1_reader.named_destinations[
779788
dest
@@ -785,21 +794,36 @@ def _merge_pdfs_ng(pdf1_path, pdf2_path, output_path):
785794
)
786795
# 更新跳转信息,跳转到对应的页面和,指定坐标 (100, 150),缩放比例为 100%
787796
# “/D”:[10,'/XYZ',100,100,0]
788-
annot_obj["/A"].update(
789-
{
790-
NameObject("/D"): ArrayObject(
791-
[
792-
NumberObject(page_number),
793-
destination.dest_array[1],
794-
FloatObject(
795-
destination.dest_array[2]
796-
),
797-
destination.dest_array[3],
798-
destination.dest_array[4],
799-
]
800-
) # 确保键和值是 PdfObject
801-
}
802-
)
797+
if destination.dest_array[1] == "/XYZ":
798+
annot_obj["/A"].update(
799+
{
800+
NameObject("/D"): ArrayObject(
801+
[
802+
NumberObject(page_number),
803+
destination.dest_array[1],
804+
FloatObject(
805+
destination.dest_array[
806+
2
807+
]
808+
),
809+
destination.dest_array[3],
810+
destination.dest_array[4],
811+
]
812+
) # 确保键和值是 PdfObject
813+
}
814+
)
815+
else:
816+
annot_obj["/A"].update(
817+
{
818+
NameObject("/D"): ArrayObject(
819+
[
820+
NumberObject(page_number),
821+
destination.dest_array[1],
822+
]
823+
) # 确保键和值是 PdfObject
824+
}
825+
)
826+
803827
rect = annot_obj.get("/Rect")
804828
rect = ArrayObject(
805829
[
@@ -820,14 +844,12 @@ def _merge_pdfs_ng(pdf1_path, pdf2_path, output_path):
820844
elif "/S" in action and action["/S"] == "/URI":
821845
# 外部链接:跳转到某个URI
822846
uri = action.get("/URI")
823-
824847
output_writer.addPage(new_page)
825848
# Save the merged PDF file
826849
with open(output_path, "wb") as output_file:
827850
output_writer.write(output_file)
828851

829852

830-
831853
def _merge_pdfs_legacy(pdf1_path, pdf2_path, output_path):
832854
import PyPDF2 # PyPDF2这个库有严重的内存泄露问题,把它放到子进程中运行,从而方便内存的释放
833855

0 commit comments

Comments
 (0)