22
33import java .io .File ;
44import java .io .IOException ;
5+ import java .util .ArrayList ;
56import java .util .HashMap ;
67import java .util .List ;
78
9+ import net .sf .jabref .BibDatabaseContext ;
10+ import net .sf .jabref .Globals ;
11+ import net .sf .jabref .logic .util .io .FileUtil ;
12+ import net .sf .jabref .model .entry .BibEntry ;
13+
14+ import org .apache .commons .logging .Log ;
15+ import org .apache .commons .logging .LogFactory ;
816import org .apache .pdfbox .pdmodel .PDDocument ;
917import org .apache .pdfbox .pdmodel .PDPage ;
1018import org .apache .pdfbox .pdmodel .interactive .annotation .PDAnnotation ;
@@ -13,38 +21,48 @@ public class PdfCommentImporter {
1321
1422 private List pdfPages ;
1523 private PDPage page ;
24+
25+ private Log logger = LogFactory .getLog (PdfCommentImporter .class );
26+
1627 public PdfCommentImporter () {
1728
1829 }
1930
2031 /**
2132 * Imports the comments from a pdf specified by its URI
22- * @param pathToPDF the URI specifying the document
23- * @return a hasmap with the unique name as key and the notes content as value
33+ *
34+ * @param document a PDDocument to get the annotations from
35+ * @return a hashmap with the unique name as key and the notes content as value
2436 */
25- public HashMap <String , String > importNotes (final String pathToPDF ) {
37+ public HashMap <String , String > importNotes (final PDDocument document ) {
2638
27- PDDocument pdf ;
2839 HashMap <String , String > annotationsMap = new HashMap <>();
29- try {
30- pdf = importPdfFile (pathToPDF );
3140
32- pdfPages = pdf .getDocumentCatalog ().getAllPages ();
33- for (int i = 0 ; i < pdfPages .size (); i ++){
34- page = (PDPage ) pdfPages .get (i );
35- for (PDAnnotation annotation : page .getAnnotations ()){
41+ pdfPages = document .getDocumentCatalog ().getAllPages ();
42+ for (int i = 0 ; i < pdfPages .size (); i ++) {
43+ page = (PDPage ) pdfPages .get (i );
44+ try {
45+ for (PDAnnotation annotation : page .getAnnotations ()) {
3646 annotationsMap .put (annotation .getAnnotationName (), annotation .getContents ());
3747 }
48+ } catch (IOException e1 ) {
49+ e1 .printStackTrace ();
3850 }
39-
40- } catch (IOException e ) {
41- e .printStackTrace ();
4251 }
4352 return annotationsMap ;
4453 }
4554
46- private PDDocument importPdfFile (final String pathToPDF ) throws IOException {
47- File file = new File (pathToPDF );
48- return PDDocument .load (file );
55+ private List <PDDocument > importPdfFile (final List <BibEntry > entryList , final BibDatabaseContext bibDatabaseContext ) throws IOException {
56+
57+ final List <File > files = FileUtil .getListOfLinkedFiles (entryList ,
58+ bibDatabaseContext .getFileDirectory (Globals .prefs .getFileDirectoryPreferences ()));
59+
60+ ArrayList <PDDocument > documents = new ArrayList <>();
61+ for (File linkedFile : files ){
62+ if (linkedFile .getName ().toLowerCase ().endsWith (".pdf" )){
63+ documents .add (PDDocument .load (linkedFile ));
64+ }
65+ }
66+ return documents ;
4967 }
5068}
0 commit comments