88import java .util .stream .Collectors ;
99import java .util .stream .IntStream ;
1010
11+ import org .apache .commons .logging .Log ;
12+ import org .apache .commons .logging .LogFactory ;
1113import org .apache .pdfbox .pdmodel .PDDocument ;
1214
1315import javafx .beans .property .SimpleListProperty ;
1416import javafx .collections .FXCollections ;
1517import javafx .collections .ObservableList ;
1618
1719public class PagesManager {
20+ private static final Log log = LogFactory .getLog (PagesManager .class );
1821 public static final String [] SUPPORTED_IMG_EXTS = { ".jpg" , ".jpeg" , ".png" };
1922
20- // TODO add pdf closing
2123 private final SimpleListProperty <DocumentPage > documentPages = new SimpleListProperty <>(
2224 FXCollections .observableArrayList ());
2325
@@ -67,14 +69,14 @@ public void unpackPDF(int itemIndex) {
6769 // convert them first and add all at once to update UI only once
6870 WholeDocumentPage pdfPage = ((WholeDocumentPage ) page );
6971 PDDocument pdf = pdfPage .getPdf ();
70- List <PDFPage > newPages = IntStream .range (0 , pdf .getNumberOfPages ()). mapToObj (( i ) -> new PDFPage ( pdfPage . toString (), pdf , i ))
71- .collect (Collectors .toList ());
72+ List <SinglePDFPage > newPages = IntStream .range (0 , pdf .getNumberOfPages ())
73+ .mapToObj (( i ) -> new SinglePDFPage ( pdfPage . toString (), pdf , i )). collect (Collectors .toList ());
7274 documentPages .remove (itemIndex );
7375 documentPages .addAll (itemIndex , newPages );
7476 }
7577
7678 public void save (File selectedFile ) throws IOException {
77- if (documentPages .size ()== 0 )
79+ if (documentPages .size () == 0 )
7880 throw new IOException ("You need to add at least one page" );
7981 PDDocument pdf = new PDDocument ();
8082 for (DocumentPage documentPage : documentPages ) {
@@ -83,4 +85,40 @@ public void save(File selectedFile) throws IOException {
8385 pdf .save (selectedFile );
8486 }
8587
88+ public void remove (int i ) {
89+ if (i < 0 || i >= documentPages .size ())
90+ throw new IndexOutOfBoundsException ();
91+ PDDocument referencedDoc ;
92+ DocumentPage page = documentPages .get (i );
93+ if (page instanceof PDFReference )
94+ referencedDoc = ((PDFReference ) page ).getPdf ();
95+ else
96+ referencedDoc = null ;
97+
98+ documentPages .remove (i );
99+
100+ // if the removed page referenced a PDDocument, we need to make sure to close it if no references
101+ // to it are left
102+ if (referencedDoc != null && !documentPages .stream ()
103+ .anyMatch ((p ) -> p instanceof PDFReference && ((PDFReference ) p ).getPdf () == referencedDoc )) {
104+ try {
105+ referencedDoc .close ();
106+ } catch (IOException e ) {
107+ log .debug ("Error while closing PDF document" , e );
108+ }
109+ }
110+ }
111+
112+ public void close () {
113+ for (DocumentPage page : documentPages ) {
114+ if (page instanceof PDFReference ) {
115+ try {
116+ ((PDFReference ) page ).getPdf ().close ();
117+ } catch (IOException e ) {
118+ log .debug ("Error while closing PDF document" , e );
119+ }
120+ }
121+ }
122+ }
123+
86124}
0 commit comments