Skip to content

Commit 169ebe6

Browse files
Braunchmairdl
authored andcommitted
Rewrite loading pdf file to use convenience method
1 parent e0983f2 commit 169ebe6

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

src/main/java/net/sf/jabref/gui/entryeditor/PdfCommentsTab.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ private void setUpPdfCommentsTab() {
6969
String pdfPath = field.get().replaceFirst(".*?:", System.getProperty("file.separator")).replaceAll(":PDF", "");
7070
HashMap<String, String> importedNotes = commentImporter.importNotes(pdfPath);
7171
importedNotes.values().stream().forEach((note) -> listModel.addElement(note));
72-
73-
//TODO add context information for importedNotes
7472
}
7573

7674
commentScrollPane.setPreferredSize(new Dimension(450,200));

src/main/java/net/sf/jabref/logic/pdf/PdfCommentImporter.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.util.ArrayList;
56
import java.util.HashMap;
67
import 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;
816
import org.apache.pdfbox.pdmodel.PDDocument;
917
import org.apache.pdfbox.pdmodel.PDPage;
1018
import 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

Comments
 (0)