4545
4646public class PDFWriter {
4747
48- private static final Map <PDFLayer , PDOptionalContentGroup > optionalContents = new HashMap <>();
49-
50- private static final List <List <PDAnnotation >> annotations = new ArrayList <>();
5148 private static final Logger LOGGER = Logger .getLogger (PDFWriter .class .getCanonicalName ());
52- private static final List <BoundingBox > pageBoundingBoxes = new ArrayList <>();
5349
54- public static void updatePDF (File inputPDF , String password , String outputFolder , List <List <IObject >> contents ) throws IOException {
50+ private final Map <PDFLayer , PDOptionalContentGroup > optionalContents = new HashMap <>();
51+ private final List <List <PDAnnotation >> annotations = new ArrayList <>();
52+ private final List <BoundingBox > pageBoundingBoxes = new ArrayList <>();
53+
54+ public void updatePDF (File inputPDF , String password , String outputFolder , List <List <IObject >> contents ) throws IOException {
5555 try (PDDocument document = Loader .loadPDF (inputPDF , password )) {
5656 for (int pageNumber = 0 ; pageNumber < StaticContainers .getDocument ().getNumberOfPages (); pageNumber ++) {
5757 annotations .add (new ArrayList <>());
@@ -74,14 +74,16 @@ public static void updatePDF(File inputPDF, String password, String outputFolder
7474 inputPDF .getName ().substring (0 , inputPDF .getName ().length () - 4 ) + "_annotated.pdf" ;
7575 document .save (outputFileName );
7676 LOGGER .log (Level .INFO , "Created {0}" , outputFileName );
77+ } catch (Exception ex ) {
78+ LOGGER .log (Level .WARNING , "Unable to create annotated PDF output: " + ex .getMessage ());
7779 }
7880 }
7981
80- private static void drawContent (IObject content , PDFLayer layer ) throws IOException {
82+ private void drawContent (IObject content , PDFLayer layer ) throws IOException {
8183 drawContent (content , layer , null );
8284 }
8385
84- private static void drawContent (IObject content , PDFLayer layer , PDAnnotation linkedAnnot ) throws IOException {
86+ private void drawContent (IObject content , PDFLayer layer , PDAnnotation linkedAnnot ) throws IOException {
8587 if ((content instanceof LineChunk )) {
8688 return ;
8789 }
@@ -98,7 +100,7 @@ private static void drawContent(IObject content, PDFLayer layer, PDAnnotation li
98100 }
99101 }
100102
101- private static void drawTableCells (TableBorder table , PDAnnotation annot ) throws IOException {
103+ private void drawTableCells (TableBorder table , PDAnnotation annot ) throws IOException {
102104 if (table .isTextBlock ()) {
103105 for (IObject content : table .getCell (0 , 0 ).getContents ()) {
104106 drawContent (content , PDFLayer .TEXT_BLOCK_CONTENT );
@@ -127,7 +129,7 @@ private static void drawTableCells(TableBorder table, PDAnnotation annot) throws
127129 }
128130 }
129131
130- private static void drawListItems (PDFList list , PDAnnotation annot ) throws IOException {
132+ private void drawListItems (PDFList list , PDAnnotation annot ) throws IOException {
131133 for (ListItem listItem : list .getListItems ()) {
132134 String contentValue = String .format ("List item: text content \" %s\" " , listItem .toString ());
133135 draw (listItem .getBoundingBox (), getColor (SemanticType .LIST ), contentValue , null , annot , listItem .getLevel (), PDFLayer .LIST_ITEMS );
@@ -137,7 +139,7 @@ private static void drawListItems(PDFList list, PDAnnotation annot) throws IOExc
137139 }
138140 }
139141
140- public static PDAnnotation draw (BoundingBox boundingBox , float [] colorArray ,
142+ public PDAnnotation draw (BoundingBox boundingBox , float [] colorArray ,
141143 String contents , Long id , PDAnnotation linkedAnnot , String level , PDFLayer layerName ) {
142144 if (!Objects .equals (boundingBox .getPageNumber (), boundingBox .getLastPageNumber ())) {
143145 if (boundingBox instanceof MultiBoundingBox ) {
@@ -273,7 +275,7 @@ public static float[] getColor(SemanticType semanticType) {
273275 return null ;
274276 }
275277
276- private static void createOptContentsForAnnotations (PDDocument document ) {
278+ private void createOptContentsForAnnotations (PDDocument document ) {
277279 if (optionalContents .isEmpty ()) {
278280 return ;
279281 }
@@ -291,7 +293,7 @@ private static void createOptContentsForAnnotations(PDDocument document) {
291293 optionalContents .clear ();
292294 }
293295
294- public static PDOptionalContentGroup getOptionalContent (PDFLayer layer ) {
296+ public PDOptionalContentGroup getOptionalContent (PDFLayer layer ) {
295297 PDOptionalContentGroup group = optionalContents .get (layer );
296298 if (group == null ) {
297299 COSDictionary cosDictionary = new COSDictionary ();
0 commit comments