@@ -82,7 +82,8 @@ public class Splitter
8282 private int startPage = Integer .MIN_VALUE ;
8383 private int endPage = Integer .MAX_VALUE ;
8484 private List <PDDocument > destinationDocuments ;
85- private Map <COSDictionary , COSDictionary > pageDictMap ;
85+ private Map <COSDictionary , COSDictionary > pageDictMap ; // map old page => new page for the current destination document
86+ private List <Map <COSDictionary , COSDictionary >> pageDictMaps ; // list of these maps for all destination documents
8687 private Map <COSDictionary , COSDictionary > structDictMap ;
8788 private Map <COSDictionary , COSDictionary > annotDictMap ;
8889 private Map <PDPageDestination ,PDPage > destToFixMap ;
@@ -129,16 +130,18 @@ public List<PDDocument> split(PDDocument document) throws IOException
129130 currentPageNumber = 0 ;
130131 destinationDocuments = new ArrayList <>();
131132 sourceDocument = document ;
132- pageDictMap = new HashMap <>();
133+ pageDictMaps = new ArrayList <>();
133134 destToFixMap = new HashMap <>();
134135 annotDictMap = new HashMap <>();
135136 idSet = new HashSet <>();
136137 roleSet = new HashSet <>();
137138
138139 processPages ();
139140
140- for (PDDocument destinationDocument : destinationDocuments )
141+ for (int i = 0 ; i < destinationDocuments . size (); ++ i )
141142 {
143+ PDDocument destinationDocument = destinationDocuments .get (i );
144+ pageDictMap = pageDictMaps .get (i );
142145 cloneStructureTree (destinationDocument );
143146 fixDestinations (destinationDocument );
144147 }
@@ -180,7 +183,8 @@ private void fixDestinations(PDDocument destinationDocument)
180183 }
181184
182185 /**
183- * Clone the structure tree from the source to the current destination document.
186+ * Clone the structure tree from the source to the current destination document. This must be
187+ * called after all pages are processed.
184188 *
185189 * @param destinationDocument
186190 * @throws IOException
@@ -677,6 +681,8 @@ private void createNewDocumentIfNecessary() throws IOException
677681 {
678682 currentDestinationDocument = createNewDocument ();
679683 destinationDocuments .add (currentDestinationDocument );
684+ pageDictMap = new HashMap <>();
685+ pageDictMaps .add (pageDictMap );
680686 }
681687 }
682688
0 commit comments