@@ -25,11 +25,13 @@ This file is part of the iText (R) project.
25
25
import com .itextpdf .commons .utils .MessageFormatUtil ;
26
26
import com .itextpdf .kernel .exceptions .KernelExceptionMessageConstant ;
27
27
import com .itextpdf .kernel .exceptions .PdfException ;
28
+ import com .itextpdf .kernel .pdf .PdfDocument ;
28
29
import com .itextpdf .kernel .pdf .PdfPage ;
29
30
import com .itextpdf .kernel .pdf .annot .PdfAnnotation ;
30
31
import com .itextpdf .kernel .utils .annotationsflattening .IAnnotationFlattener ;
31
32
import com .itextpdf .kernel .utils .annotationsflattening .PdfAnnotationFlattenFactory ;
32
33
34
+ import java .util .ArrayList ;
33
35
import java .util .List ;
34
36
35
37
/**
@@ -63,36 +65,56 @@ public PdfAnnotationFlattener() {
63
65
* {@link IAnnotationFlattener}.
64
66
*
65
67
* @param annotationsToFlatten the annotations that should be flattened.
66
- * @param page the page where the annotations are located.
68
+ *
69
+ * @return the list of annotations that were not flattened successfully
67
70
*/
68
- public void flatten (List <PdfAnnotation > annotationsToFlatten , PdfPage page ) {
69
- if (page == null ) {
70
- throw new PdfException (
71
- MessageFormatUtil .format (KernelExceptionMessageConstant .ARG_SHOULD_NOT_BE_NULL , "page" ));
72
- }
71
+ public List <PdfAnnotation > flatten (List <PdfAnnotation > annotationsToFlatten ) {
73
72
if (annotationsToFlatten == null ) {
74
73
throw new PdfException (
75
74
MessageFormatUtil .format (KernelExceptionMessageConstant .ARG_SHOULD_NOT_BE_NULL ,
76
75
"annotationsToFlatten" ));
77
76
}
77
+ final List <PdfAnnotation > unFlattenedAnnotations = new ArrayList <>();
78
78
for (final PdfAnnotation pdfAnnotation : annotationsToFlatten ) {
79
+ if (pdfAnnotation == null ) {
80
+ continue ;
81
+ }
82
+ PdfPage page = pdfAnnotation .getPage ();
83
+ if (page == null ) {
84
+ continue ;
85
+ }
79
86
final IAnnotationFlattener worker = pdfAnnotationFlattenFactory .getAnnotationFlattenWorker (
80
87
pdfAnnotation .getSubtype ());
81
- worker .flatten (pdfAnnotation , page );
88
+ final boolean flattenedSuccessfully = worker .flatten (pdfAnnotation , page );
89
+ if (!flattenedSuccessfully ) {
90
+ unFlattenedAnnotations .add (pdfAnnotation );
91
+ }
92
+
82
93
}
94
+ return unFlattenedAnnotations ;
83
95
}
84
96
85
97
/**
86
- * Flattens all annotations on the page according to the defined implementation of
98
+ * Flattens the annotations on the page according to the defined implementation of
87
99
* {@link IAnnotationFlattener}.
88
100
*
89
- * @param page the page where the annotations are located.
101
+ * @param document the document that contains the annotations that should be flattened.
102
+ *
103
+ * @return the list of annotations that were not flattened successfully
90
104
*/
91
- public void flatten (PdfPage page ) {
92
- if (page == null ) {
105
+ public List < PdfAnnotation > flatten (PdfDocument document ) {
106
+ if (document == null ) {
93
107
throw new PdfException (
94
- MessageFormatUtil .format (KernelExceptionMessageConstant .ARG_SHOULD_NOT_BE_NULL , "page" ));
108
+ MessageFormatUtil .format (KernelExceptionMessageConstant .ARG_SHOULD_NOT_BE_NULL , "document" ));
109
+ }
110
+ final List <PdfAnnotation > annotations = new ArrayList <>();
111
+ // Process page by page to avoid loading a bunch of annotations into memory
112
+ final int documentNumberOfPages = document .getNumberOfPages ();
113
+ for (int i = 1 ; i <= documentNumberOfPages ; i ++) {
114
+ final PdfPage page = document .getPage (i );
115
+ final List <PdfAnnotation > failedFlatteningAnnotations = flatten (page .getAnnotations ());
116
+ annotations .addAll (failedFlatteningAnnotations );
95
117
}
96
- flatten ( page . getAnnotations (), page ) ;
118
+ return annotations ;
97
119
}
98
120
}
0 commit comments