@@ -244,11 +244,13 @@ private static void copyTo(PdfDocument destDocument, int insertBeforePage, Map<P
244
244
* @param page2page association between original page and copied page.
245
245
* @param copyFromDestDocument indicates if <code>page2page</code> keys and values represent pages from {@code destDocument}.
246
246
*/
247
- private static void copyTo (PdfDocument destDocument , Map <PdfPage , PdfPage > page2page , PdfDocument callingDocument , boolean copyFromDestDocument ) {
247
+ private static void copyTo (PdfDocument destDocument , Map <PdfPage , PdfPage > page2page , PdfDocument callingDocument
248
+ , boolean copyFromDestDocument ) {
248
249
copyTo (destDocument , page2page , callingDocument , copyFromDestDocument , -1 );
249
250
}
250
251
251
- private static void copyTo (PdfDocument destDocument , Map <PdfPage , PdfPage > page2page , PdfDocument callingDocument , boolean copyFromDestDocument , int insertIndex ) {
252
+ private static void copyTo (PdfDocument destDocument , Map <PdfPage , PdfPage > page2page , PdfDocument callingDocument
253
+ , boolean copyFromDestDocument , int insertIndex ) {
252
254
CopyStructureResult copiedStructure = copyStructure (destDocument , page2page , callingDocument , copyFromDestDocument );
253
255
PdfStructTreeRoot destStructTreeRoot = destDocument .getStructTreeRoot ();
254
256
destStructTreeRoot .makeIndirect (destDocument );
@@ -283,7 +285,8 @@ private static void copyTo(PdfDocument destDocument, Map<PdfPage, PdfPage> page2
283
285
}
284
286
}
285
287
286
- private static CopyStructureResult copyStructure (PdfDocument destDocument , Map <PdfPage , PdfPage > page2page , PdfDocument callingDocument , boolean copyFromDestDocument ) {
288
+ private static CopyStructureResult copyStructure (PdfDocument destDocument , Map <PdfPage , PdfPage > page2page
289
+ , PdfDocument callingDocument , boolean copyFromDestDocument ) {
287
290
PdfDocument fromDocument = copyFromDestDocument ? destDocument : callingDocument ;
288
291
Map <PdfDictionary , PdfDictionary > topsToFirstDestPage = new HashMap <>();
289
292
Set <PdfObject > objectsToCopy = new HashSet <>();
@@ -380,16 +383,23 @@ private static PdfDictionary copyObject(PdfDictionary source, PdfDictionary dest
380
383
}
381
384
382
385
PdfObject k = source .get (PdfName .K );
386
+ PdfDictionary lastCopiedTrPage = null ;
383
387
if (k != null ) {
384
388
if (k .isArray ()) {
385
389
PdfArray kArr = (PdfArray ) k ;
386
390
PdfArray newArr = new PdfArray ();
387
391
for (int i = 0 ; i < kArr .size (); i ++) {
388
- PdfObject copiedKid = copyObjectKid (kArr .get (i ), copied , destPage , parentChangePg , copyingParams );
392
+ PdfObject copiedKid = copyObjectKid (kArr .get (i ), copied , destPage , parentChangePg , copyingParams
393
+ , lastCopiedTrPage );
389
394
if (copiedKid != null ) {
390
395
newArr .add (copiedKid );
396
+ if (copiedKid instanceof PdfDictionary
397
+ && PdfName .TR .equals (((PdfDictionary ) copiedKid ).getAsName (PdfName .S ))) {
398
+ lastCopiedTrPage = destPage ;
399
+ }
391
400
}
392
401
}
402
+
393
403
if (!newArr .isEmpty ()) {
394
404
if (newArr .size () == 1 ) {
395
405
copied .put (PdfName .K , newArr .get (0 ));
@@ -398,7 +408,8 @@ private static PdfDictionary copyObject(PdfDictionary source, PdfDictionary dest
398
408
}
399
409
}
400
410
} else {
401
- PdfObject copiedKid = copyObjectKid (k , copied , destPage , parentChangePg , copyingParams );
411
+ PdfObject copiedKid = copyObjectKid (k , copied , destPage , parentChangePg , copyingParams
412
+ , lastCopiedTrPage );
402
413
if (copiedKid != null ) {
403
414
copied .put (PdfName .K , copiedKid );
404
415
}
@@ -407,7 +418,9 @@ private static PdfDictionary copyObject(PdfDictionary source, PdfDictionary dest
407
418
return copied ;
408
419
}
409
420
410
- private static PdfObject copyObjectKid (PdfObject kid , PdfDictionary copiedParent , PdfDictionary destPage , boolean parentChangePg , StructElemCopyingParams copyingParams ) {
421
+ private static PdfObject copyObjectKid (PdfObject kid , PdfDictionary copiedParent , PdfDictionary destPage ,
422
+ boolean parentChangePg , StructElemCopyingParams copyingParams ,
423
+ PdfDictionary lastCopiedTrPage ) {
411
424
if (kid .isNumber ()) {
412
425
if (!parentChangePg ) {
413
426
copyingParams .getToDocument ().getStructTreeRoot ().getParentTreeHandler ()
@@ -416,11 +429,27 @@ private static PdfObject copyObjectKid(PdfObject kid, PdfDictionary copiedParent
416
429
}
417
430
} else if (kid .isDictionary ()) {
418
431
PdfDictionary kidAsDict = (PdfDictionary ) kid ;
419
- // if element is TD and its parent is TR which was copied, then we copy it in any case
432
+ //if element is TD and its parent is TR which was copied, then we copy it in any case
420
433
if (copyingParams .getObjectsToCopy ().contains (kidAsDict ) ||
421
434
shouldTableElementBeCopied (kidAsDict , copiedParent )) {
435
+ //if TR element is not connected to any page,
436
+ //it should be copied to the same page as the last copied TR which connected to page
437
+ PdfDictionary destination = destPage ;
438
+ if (PdfName .TR .equals (kidAsDict .getAsName (PdfName .S ))
439
+ && !copyingParams .getObjectsToCopy ().contains (kidAsDict )) {
440
+ if (McrCheckUtil .isTrContainsMcr (kidAsDict )){
441
+ return null ;
442
+ }
443
+
444
+ if (lastCopiedTrPage == null ) {
445
+ return null ;
446
+ } else {
447
+ destination = lastCopiedTrPage ;
448
+ }
449
+ }
422
450
boolean hasParent = kidAsDict .containsKey (PdfName .P );
423
- PdfDictionary copiedKid = copyObject (kidAsDict , destPage , parentChangePg , copyingParams );
451
+ PdfDictionary copiedKid = copyObject (kidAsDict , destination , parentChangePg , copyingParams );
452
+
424
453
if (hasParent ) {
425
454
copiedKid .put (PdfName .P , copiedParent );
426
455
} else {
@@ -446,8 +475,9 @@ private static PdfObject copyObjectKid(PdfObject kid, PdfDictionary copiedParent
446
475
}
447
476
448
477
static boolean shouldTableElementBeCopied (PdfDictionary obj , PdfDictionary parent ) {
449
- return (PdfName .TD .equals (obj .get (PdfName .S )) || PdfName .TH .equals (obj .get (PdfName .S )))
450
- && PdfName .TR .equals (parent .get (PdfName .S ));
478
+ PdfName role = obj .getAsName (PdfName .S );
479
+ return ((PdfName .TD .equals (role ) || PdfName .TH .equals (role )) && PdfName .TR .equals (parent .get (PdfName .S )))
480
+ || PdfName .TR .equals (role );
451
481
}
452
482
453
483
private static PdfDictionary copyNamespaceDict (PdfDictionary srcNsDict , StructElemCopyingParams copyingParams ) {
0 commit comments