Skip to content

Commit 9332eb5

Browse files
committed
Fixes to PDF article
1 parent c29601d commit 9332eb5

3 files changed

Lines changed: 27 additions & 53 deletions

File tree

‎pdf/pom.xml‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,31 @@
2424
<version>3.8.1</version>
2525
<scope>test</scope>
2626
</dependency>
27-
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
28-
<dependency>
29-
<groupId>org.apache.pdfbox</groupId>
30-
<artifactId>pdfbox</artifactId>
31-
<version>2.0.3</version>
32-
</dependency>
33-
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox-tools -->
3427
<dependency>
3528
<groupId>org.apache.pdfbox</groupId>
3629
<artifactId>pdfbox-tools</artifactId>
3730
<version>2.0.3</version>
3831
</dependency>
39-
<!-- https://mvnrepository.com/artifact/net.sf.cssbox/pdf2dom -->
4032
<dependency>
4133
<groupId>net.sf.cssbox</groupId>
4234
<artifactId>pdf2dom</artifactId>
4335
<version>1.6</version>
4436
</dependency>
45-
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
4637
<dependency>
4738
<groupId>com.itextpdf</groupId>
4839
<artifactId>itextpdf</artifactId>
4940
<version>5.5.10</version>
5041
</dependency>
51-
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
5242
<dependency>
5343
<groupId>org.apache.poi</groupId>
5444
<artifactId>poi</artifactId>
5545
<version>3.15</version>
5646
</dependency>
57-
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
58-
<dependency>
59-
<groupId>org.apache.poi</groupId>
60-
<artifactId>poi-ooxml</artifactId>
61-
<version>3.15</version>
62-
</dependency>
63-
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
6447
<dependency>
6548
<groupId>org.apache.poi</groupId>
6649
<artifactId>poi-scratchpad</artifactId>
6750
<version>3.15</version>
6851
</dependency>
69-
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-transcoder -->
7052
<dependency>
7153
<groupId>org.apache.xmlgraphics</groupId>
7254
<artifactId>batik-transcoder</artifactId>

‎pdf/src/main/java/com/baeldung/pdf/PDF2HTMLExample.java‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@ public static void main(String[] args) {
2323
}
2424

2525
private static void generateHTMLFromPDF(String filename) throws ParserConfigurationException, IOException {
26-
try {
27-
PDDocument pdf = PDDocument.load(new File(filename));
28-
PDFDomTree parser = new PDFDomTree();
29-
Writer output = new PrintWriter("src/output/pdf.html", "utf-8");
30-
parser.writeText(pdf, output);
31-
output.close();
32-
if (pdf != null) {
33-
pdf.close();
34-
}
35-
} catch (IOException e) {
36-
e.printStackTrace();
26+
PDDocument pdf = PDDocument.load(new File(filename));
27+
PDFDomTree parser = new PDFDomTree();
28+
Writer output = new PrintWriter("src/output/pdf.html", "utf-8");
29+
parser.writeText(pdf, output);
30+
output.close();
31+
if (pdf != null) {
32+
pdf.close();
3733
}
3834
}
3935
}

‎pdf/src/main/java/com/baeldung/pdf/PDF2TextExample.java‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,26 @@ public static void main(String[] args) {
2323
}
2424

2525
private static void generateTxtFromPDF(String filename) throws IOException {
26-
try {
27-
File f = new File(filename);
28-
String parsedText;
29-
PDFParser parser = new PDFParser(new RandomAccessFile(f, "r"));
30-
parser.parse();
31-
32-
COSDocument cosDoc = parser.getDocument();
33-
34-
PDFTextStripper pdfStripper = new PDFTextStripper();
35-
PDDocument pdDoc = new PDDocument(cosDoc);
36-
37-
parsedText = pdfStripper.getText(pdDoc);
38-
39-
if (cosDoc != null)
40-
cosDoc.close();
41-
if (pdDoc != null)
42-
pdDoc.close();
43-
44-
PrintWriter pw = new PrintWriter("src/output/pdf.txt");
45-
pw.print(parsedText);
46-
pw.close();
47-
} catch (Exception e) {
48-
e.printStackTrace();
49-
}
26+
File f = new File(filename);
27+
String parsedText;
28+
PDFParser parser = new PDFParser(new RandomAccessFile(f, "r"));
29+
parser.parse();
30+
31+
COSDocument cosDoc = parser.getDocument();
32+
33+
PDFTextStripper pdfStripper = new PDFTextStripper();
34+
PDDocument pdDoc = new PDDocument(cosDoc);
35+
36+
parsedText = pdfStripper.getText(pdDoc);
37+
38+
if (cosDoc != null)
39+
cosDoc.close();
40+
if (pdDoc != null)
41+
pdDoc.close();
42+
43+
PrintWriter pw = new PrintWriter("src/output/pdf.txt");
44+
pw.print(parsedText);
45+
pw.close();
5046
}
5147

5248
}

0 commit comments

Comments
 (0)