@@ -367,7 +367,6 @@ def _write_dests(self):
367367 self .output .addNamedDestinationObject (v )
368368
369369 def _write_bookmarks (self , bookmarks = None , parent = None ):
370-
371370 if bookmarks is None :
372371 bookmarks = self .bookmarks
373372
@@ -382,64 +381,68 @@ def _write_bookmarks(self, bookmarks=None, parent=None):
382381 if '/Page' in b :
383382 for i , p in enumerate (self .pages ):
384383 if p .id == b ['/Page' ]:
385- # b[NameObject('/Page')] = p.out_pagedata
386- args = [NumberObject (p .id ), NameObject (b ['/Type' ])]
387- # nothing more to add
388- # if b['/Type'] == '/Fit' or b['/Type'] == '/FitB'
389- if b ['/Type' ] == '/FitH' or b ['/Type' ] == '/FitBH' :
390- if '/Top' in b and not isinstance (b ['/Top' ], NullObject ):
391- args .append (FloatObject (b ['/Top' ]))
392- else :
393- args .append (FloatObject (0 ))
394- del b ['/Top' ]
395- elif b ['/Type' ] == '/FitV' or b ['/Type' ] == '/FitBV' :
396- if '/Left' in b and not isinstance (b ['/Left' ], NullObject ):
397- args .append (FloatObject (b ['/Left' ]))
398- else :
399- args .append (FloatObject (0 ))
400- del b ['/Left' ]
401- elif b ['/Type' ] == '/XYZ' :
402- if '/Left' in b and not isinstance (b ['/Left' ], NullObject ):
403- args .append (FloatObject (b ['/Left' ]))
404- else :
405- args .append (FloatObject (0 ))
406- if '/Top' in b and not isinstance (b ['/Top' ], NullObject ):
407- args .append (FloatObject (b ['/Top' ]))
408- else :
409- args .append (FloatObject (0 ))
410- if '/Zoom' in b and not isinstance (b ['/Zoom' ], NullObject ):
411- args .append (FloatObject (b ['/Zoom' ]))
412- else :
413- args .append (FloatObject (0 ))
414- del b ['/Top' ], b ['/Zoom' ], b ['/Left' ]
415- elif b ['/Type' ] == '/FitR' :
416- if '/Left' in b and not isinstance (b ['/Left' ], NullObject ):
417- args .append (FloatObject (b ['/Left' ]))
418- else :
419- args .append (FloatObject (0 ))
420- if '/Bottom' in b and not isinstance (b ['/Bottom' ], NullObject ):
421- args .append (FloatObject (b ['/Bottom' ]))
422- else :
423- args .append (FloatObject (0 ))
424- if '/Right' in b and not isinstance (b ['/Right' ], NullObject ):
425- args .append (FloatObject (b ['/Right' ]))
426- else :
427- args .append (FloatObject (0 ))
428- if '/Top' in b and not isinstance (b ['/Top' ], NullObject ):
429- args .append (FloatObject (b ['/Top' ]))
430- else :
431- args .append (FloatObject (0 ))
432- del b ['/Left' ], b ['/Right' ], b ['/Bottom' ], b ['/Top' ]
433-
434- b [NameObject ('/A' )] = DictionaryObject ({NameObject ('/S' ): NameObject ('/GoTo' ), NameObject ('/D' ): ArrayObject (args )})
435-
436- pageno = i
437- pdf = p .src # noqa: F841
384+ pageno , pdf = self ._write_bookmark_on_page (b , p , i )
438385 break
439386 if pageno is not None :
440387 del b ['/Page' ], b ['/Type' ]
441388 last_added = self .output .addBookmarkDict (b , parent )
442389
390+ def _write_bookmark_on_page (self , b , p , i ):
391+ # b[NameObject('/Page')] = p.out_pagedata
392+ args = [NumberObject (p .id ), NameObject (b ['/Type' ])]
393+ # nothing more to add
394+ # if b['/Type'] == '/Fit' or b['/Type'] == '/FitB'
395+ if b ['/Type' ] == '/FitH' or b ['/Type' ] == '/FitBH' :
396+ if '/Top' in b and not isinstance (b ['/Top' ], NullObject ):
397+ args .append (FloatObject (b ['/Top' ]))
398+ else :
399+ args .append (FloatObject (0 ))
400+ del b ['/Top' ]
401+ elif b ['/Type' ] == '/FitV' or b ['/Type' ] == '/FitBV' :
402+ if '/Left' in b and not isinstance (b ['/Left' ], NullObject ):
403+ args .append (FloatObject (b ['/Left' ]))
404+ else :
405+ args .append (FloatObject (0 ))
406+ del b ['/Left' ]
407+ elif b ['/Type' ] == '/XYZ' :
408+ if '/Left' in b and not isinstance (b ['/Left' ], NullObject ):
409+ args .append (FloatObject (b ['/Left' ]))
410+ else :
411+ args .append (FloatObject (0 ))
412+ if '/Top' in b and not isinstance (b ['/Top' ], NullObject ):
413+ args .append (FloatObject (b ['/Top' ]))
414+ else :
415+ args .append (FloatObject (0 ))
416+ if '/Zoom' in b and not isinstance (b ['/Zoom' ], NullObject ):
417+ args .append (FloatObject (b ['/Zoom' ]))
418+ else :
419+ args .append (FloatObject (0 ))
420+ del b ['/Top' ], b ['/Zoom' ], b ['/Left' ]
421+ elif b ['/Type' ] == '/FitR' :
422+ if '/Left' in b and not isinstance (b ['/Left' ], NullObject ):
423+ args .append (FloatObject (b ['/Left' ]))
424+ else :
425+ args .append (FloatObject (0 ))
426+ if '/Bottom' in b and not isinstance (b ['/Bottom' ], NullObject ):
427+ args .append (FloatObject (b ['/Bottom' ]))
428+ else :
429+ args .append (FloatObject (0 ))
430+ if '/Right' in b and not isinstance (b ['/Right' ], NullObject ):
431+ args .append (FloatObject (b ['/Right' ]))
432+ else :
433+ args .append (FloatObject (0 ))
434+ if '/Top' in b and not isinstance (b ['/Top' ], NullObject ):
435+ args .append (FloatObject (b ['/Top' ]))
436+ else :
437+ args .append (FloatObject (0 ))
438+ del b ['/Left' ], b ['/Right' ], b ['/Bottom' ], b ['/Top' ]
439+
440+ b [NameObject ('/A' )] = DictionaryObject ({NameObject ('/S' ): NameObject ('/GoTo' ), NameObject ('/D' ): ArrayObject (args )})
441+
442+ pageno = i
443+ pdf = p .src # noqa: F841
444+ return (pageno , pdf )
445+
443446 def _associate_dests_to_pages (self , pages ):
444447 for nd in self .named_dests :
445448 pageno = None
@@ -570,6 +573,6 @@ def add(self, title, pagenum):
570573 self .tree .addChild (bookmark )
571574
572575 def removeAll (self ):
573- for child in [ x for x in self .tree .children ()] :
576+ for child in self .tree .children ():
574577 self .tree .removeChild (child )
575578 self .pop ()
0 commit comments