@@ -697,15 +697,6 @@ def _merge_pdfs_ng(pdf1_path, pdf2_path, output_path):
697
697
),
698
698
0 ,
699
699
)
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 = []
709
700
if "/Annots" in new_page :
710
701
annotations = new_page ["/Annots" ]
711
702
for i , annot in enumerate (annotations ):
@@ -720,7 +711,8 @@ def _merge_pdfs_ng(pdf1_path, pdf2_path, output_path):
720
711
if "/S" in action and action ["/S" ] == "/GoTo" :
721
712
# 内部链接:跳转到文档中的某个页面
722
713
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 :
724
716
# 获取原始文件中跳转信息,包括跳转页面
725
717
destination = pdf2_reader .named_destinations [
726
718
dest
@@ -732,24 +724,39 @@ def _merge_pdfs_ng(pdf1_path, pdf2_path, output_path):
732
724
)
733
725
# 更新跳转信息,跳转到对应的页面和,指定坐标 (100, 150),缩放比例为 100%
734
726
# “/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
+
753
760
rect = annot_obj .get ("/Rect" )
754
761
# 更新点击坐标
755
762
rect = ArrayObject (
@@ -773,7 +780,9 @@ def _merge_pdfs_ng(pdf1_path, pdf2_path, output_path):
773
780
): rect # 确保键和值是 PdfObject
774
781
}
775
782
)
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
+
777
786
# 获取原始文件中跳转信息,包括跳转页面
778
787
destination = pdf1_reader .named_destinations [
779
788
dest
@@ -785,21 +794,36 @@ def _merge_pdfs_ng(pdf1_path, pdf2_path, output_path):
785
794
)
786
795
# 更新跳转信息,跳转到对应的页面和,指定坐标 (100, 150),缩放比例为 100%
787
796
# “/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
+
803
827
rect = annot_obj .get ("/Rect" )
804
828
rect = ArrayObject (
805
829
[
@@ -820,14 +844,12 @@ def _merge_pdfs_ng(pdf1_path, pdf2_path, output_path):
820
844
elif "/S" in action and action ["/S" ] == "/URI" :
821
845
# 外部链接:跳转到某个URI
822
846
uri = action .get ("/URI" )
823
-
824
847
output_writer .addPage (new_page )
825
848
# Save the merged PDF file
826
849
with open (output_path , "wb" ) as output_file :
827
850
output_writer .write (output_file )
828
851
829
852
830
-
831
853
def _merge_pdfs_legacy (pdf1_path , pdf2_path , output_path ):
832
854
import PyPDF2 # PyPDF2这个库有严重的内存泄露问题,把它放到子进程中运行,从而方便内存的释放
833
855
0 commit comments