-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownLoadPDF.java
More file actions
53 lines (37 loc) · 1.57 KB
/
Copy pathdownLoadPDF.java
File metadata and controls
53 lines (37 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
class downLoadPDF {
public static void mergePDF(Map<String, String> links) {
// Set a pdf merger
PDFMergerUtility PDFmerger = new PDFMergerUtility();
try {
// Set a map iterator
Iterator linksList = links.entrySet().iterator();
// Iterate to merge all pdf files
while (linksList.hasNext()) {
Map.Entry link = (Map.Entry) linksList.next();
// Print out the titles of the pdfs to keep in track
System.out.println(link.getKey().toString());
// Convert the pdf url as an input stream
URL url = new URL(link.getValue().toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
// Add the pdfs in the merger
PDFmerger.addSource(is);
}
// Set a destintation to store the merged pdf
PDFmerger.setDestinationFileName("C:/Users/jeons/OneDrive/Desktop/CSE_Slide_Sample/CSE_Slides.pdf");
// Merge the pdf and create the pdf in the setted destination
PDFmerger.mergeDocuments();
} catch (Exception e) {
System.out.println("Download failed! Please try again");
}
}
public static void main(String[] args) {
}
}