Skip to content

Commit 0ea29df

Browse files
maibinpivovarit
authored andcommitted
Code cleanup for PDF module (eugenp#800)
* PDF to X * PDF to X * Remove created doc * Code fixes and cleanup for PDF module
1 parent 8628a63 commit 0ea29df

4 files changed

Lines changed: 19 additions & 56 deletions

File tree

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

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

2525
private static void generateHTMLFromPDF(String filename) throws ParserConfigurationException, IOException {
26-
System.out.println("Creating HTML file from a PDF file: " + filename);
27-
PDDocument pdf = null;
2826
try {
29-
// load the PDF file using PDFBox
30-
pdf = PDDocument.load(new File(filename));
31-
// create the DOM parser
27+
PDDocument pdf = PDDocument.load(new File(filename));
3228
PDFDomTree parser = new PDFDomTree();
33-
// parse the file and get the DOM Document
3429
Writer output = new PrintWriter("src/output/pdf.html", "utf-8");
3530
parser.writeText(pdf, output);
3631
output.close();
37-
} finally {
3832
if (pdf != null) {
39-
try {
40-
pdf.close();
41-
} catch (IOException e) {
42-
System.err.println("Error: " + e.getMessage());
43-
}
33+
pdf.close();
4434
}
35+
} catch (IOException e) {
36+
e.printStackTrace();
4537
}
46-
System.out.println("Done.");
4738
}
48-
4939
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ public static void main(String[] args) {
2424
}
2525

2626
private static void generateImageFromPDF(String filename, String extension) throws IOException {
27-
System.out.println("Creating " + extension + " image from a PDF file: " + filename);
2827
PDDocument document = PDDocument.load(new File(filename));
2928
PDFRenderer pdfRenderer = new PDFRenderer(document);
3029
for (int page = 0; page < document.getNumberOfPages(); ++page) {
31-
System.out.println("Page number: " + (page + 1) + " is being rendered");
3230
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
3331
ImageIOUtil.writeImage(bim, "src/output/pdf" + "-" + (page + 1) + "." + extension, 300);
3432
}
3533
document.close();
36-
System.out.println("Done.");
3734
}
3835
}

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

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

2525
private static void generateTxtFromPDF(String filename) throws IOException {
26-
System.out.println("Parsing text from PDF file " + filename);
27-
String parsedText = null;
28-
PDFTextStripper pdfStripper;
29-
PDDocument pdDoc = null;
30-
COSDocument cosDoc = null;
31-
32-
File f = new File(filename);
33-
PDFParser parser = new PDFParser(new RandomAccessFile(f, "r"));
34-
3526
try {
27+
File f = new File(filename);
28+
String parsedText;
29+
PDFParser parser = new PDFParser(new RandomAccessFile(f, "r"));
3630
parser.parse();
37-
cosDoc = parser.getDocument();
38-
pdfStripper = new PDFTextStripper();
39-
pdDoc = new PDDocument(cosDoc);
31+
32+
COSDocument cosDoc = parser.getDocument();
33+
34+
PDFTextStripper pdfStripper = new PDFTextStripper();
35+
PDDocument pdDoc = new PDDocument(cosDoc);
36+
4037
parsedText = pdfStripper.getText(pdDoc);
41-
} catch (Exception e) {
42-
System.err.println("An exception occured in parsing the PDF Document.");
43-
e.printStackTrace();
44-
try {
45-
if (cosDoc != null)
46-
cosDoc.close();
47-
if (pdDoc != null)
48-
pdDoc.close();
49-
} catch (Exception e1) {
50-
e.printStackTrace();
51-
}
52-
}
53-
System.out.println("Writing PDF text to output text file");
54-
try {
38+
39+
if (cosDoc != null)
40+
cosDoc.close();
41+
if (pdDoc != null)
42+
pdDoc.close();
43+
5544
PrintWriter pw = new PrintWriter("src/output/pdf.txt");
5645
pw.print(parsedText);
5746
pw.close();
5847
} catch (Exception e) {
59-
System.out.println("An exception occured in writing the pdf text to file.");
6048
e.printStackTrace();
6149
}
62-
System.out.println("Done.");
6350
}
6451

6552
}

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,25 @@ public static void main(String[] args) {
2626
}
2727

2828
private static void generateDocFromPDF(String filename) throws IOException {
29-
System.out.println("Creating a docx file from a PDF file: " + filename);
30-
// Create the word document
3129
XWPFDocument doc = new XWPFDocument();
3230

33-
// Open the pdf file
3431
String pdf = filename;
3532
PdfReader reader = new PdfReader(pdf);
3633
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
3734

38-
// Read the PDF page by page
3935
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
4036
TextExtractionStrategy strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
41-
// Extract the text
4237
String text = strategy.getResultantText();
43-
// Create a new paragraph in the word document, adding the extracted
44-
// text
4538
XWPFParagraph p = doc.createParagraph();
4639
XWPFRun run = p.createRun();
4740
run.setText(text);
48-
// Adding a page break
4941
run.addBreak(BreakType.PAGE);
5042
}
51-
// Write the word document
5243
FileOutputStream out = new FileOutputStream("src/output/pdf.docx");
5344
doc.write(out);
54-
// Close all open files
5545
out.close();
5646
reader.close();
5747
doc.close();
58-
System.out.println("Done.");
5948
}
6049

6150
}

0 commit comments

Comments
 (0)