Skip to content

Commit d0d62b5

Browse files
maibinpivovarit
authored andcommitted
PDF to X conversions (eugenp#799)
* PDF to X * PDF to X * Remove created doc
1 parent 4311cee commit d0d62b5

8 files changed

Lines changed: 319 additions & 4 deletions

File tree

‎pdf/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

‎pdf/pom.xml‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?xml version="1.0"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>parent-modules</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
<artifactId>pdf</artifactId>
12+
<name>pdf</name>
13+
<url>http://maven.apache.org</url>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>3.8.1</version>
25+
<scope>test</scope>
26+
</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 -->
34+
<dependency>
35+
<groupId>org.apache.pdfbox</groupId>
36+
<artifactId>pdfbox-tools</artifactId>
37+
<version>2.0.3</version>
38+
</dependency>
39+
<!-- https://mvnrepository.com/artifact/net.sf.cssbox/pdf2dom -->
40+
<dependency>
41+
<groupId>net.sf.cssbox</groupId>
42+
<artifactId>pdf2dom</artifactId>
43+
<version>1.6</version>
44+
</dependency>
45+
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
46+
<dependency>
47+
<groupId>com.itextpdf</groupId>
48+
<artifactId>itextpdf</artifactId>
49+
<version>5.5.10</version>
50+
</dependency>
51+
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
52+
<dependency>
53+
<groupId>org.apache.poi</groupId>
54+
<artifactId>poi</artifactId>
55+
<version>3.15</version>
56+
</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 -->
64+
<dependency>
65+
<groupId>org.apache.poi</groupId>
66+
<artifactId>poi-scratchpad</artifactId>
67+
<version>3.15</version>
68+
</dependency>
69+
<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-transcoder -->
70+
<dependency>
71+
<groupId>org.apache.xmlgraphics</groupId>
72+
<artifactId>batik-transcoder</artifactId>
73+
<version>1.8</version>
74+
</dependency>
75+
</dependencies>
76+
77+
<build>
78+
<finalName>pdf</finalName>
79+
<resources>
80+
<resource>
81+
<directory>src/main/resources</directory>
82+
<filtering>true</filtering>
83+
</resource>
84+
</resources>
85+
86+
<plugins>
87+
88+
<plugin>
89+
<groupId>org.apache.maven.plugins</groupId>
90+
<artifactId>maven-compiler-plugin</artifactId>
91+
<version>${maven-compiler-plugin.version}</version>
92+
<configuration>
93+
<source>1.8</source>
94+
<target>1.8</target>
95+
</configuration>
96+
</plugin>
97+
98+
</plugins>
99+
</build>
100+
</project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.pdf;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.PrintWriter;
6+
import java.io.Writer;
7+
8+
import javax.xml.parsers.ParserConfigurationException;
9+
10+
import org.apache.pdfbox.pdmodel.PDDocument;
11+
import org.fit.pdfdom.PDFDomTree;
12+
13+
public class PDF2HTMLExample {
14+
15+
private static final String FILENAME = "src/main/resources/pdf.pdf";
16+
17+
public static void main(String[] args) {
18+
try {
19+
generateHTMLFromPDF(FILENAME);
20+
} catch (IOException | ParserConfigurationException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
25+
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;
28+
try {
29+
// load the PDF file using PDFBox
30+
pdf = PDDocument.load(new File(filename));
31+
// create the DOM parser
32+
PDFDomTree parser = new PDFDomTree();
33+
// parse the file and get the DOM Document
34+
Writer output = new PrintWriter("src/output/pdf.html", "utf-8");
35+
parser.writeText(pdf, output);
36+
output.close();
37+
} finally {
38+
if (pdf != null) {
39+
try {
40+
pdf.close();
41+
} catch (IOException e) {
42+
System.err.println("Error: " + e.getMessage());
43+
}
44+
}
45+
}
46+
System.out.println("Done.");
47+
}
48+
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.pdf;
2+
3+
import java.awt.image.BufferedImage;
4+
import java.io.File;
5+
import java.io.IOException;
6+
7+
import org.apache.pdfbox.pdmodel.PDDocument;
8+
import org.apache.pdfbox.rendering.ImageType;
9+
import org.apache.pdfbox.rendering.PDFRenderer;
10+
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
11+
12+
public class PDF2ImageExample {
13+
14+
private static final String FILENAME = "src/main/resources/pdf.pdf";
15+
16+
public static void main(String[] args) {
17+
try {
18+
generateImageFromPDF(FILENAME, "png");
19+
generateImageFromPDF(FILENAME, "jpeg");
20+
generateImageFromPDF(FILENAME, "gif");
21+
} catch (IOException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
26+
private static void generateImageFromPDF(String filename, String extension) throws IOException {
27+
System.out.println("Creating " + extension + " image from a PDF file: " + filename);
28+
PDDocument document = PDDocument.load(new File(filename));
29+
PDFRenderer pdfRenderer = new PDFRenderer(document);
30+
for (int page = 0; page < document.getNumberOfPages(); ++page) {
31+
System.out.println("Page number: " + (page + 1) + " is being rendered");
32+
BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
33+
ImageIOUtil.writeImage(bim, "src/output/pdf" + "-" + (page + 1) + "." + extension, 300);
34+
}
35+
document.close();
36+
System.out.println("Done.");
37+
}
38+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.baeldung.pdf;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.PrintWriter;
6+
7+
import org.apache.pdfbox.cos.COSDocument;
8+
import org.apache.pdfbox.io.RandomAccessFile;
9+
import org.apache.pdfbox.pdfparser.PDFParser;
10+
import org.apache.pdfbox.pdmodel.PDDocument;
11+
import org.apache.pdfbox.text.PDFTextStripper;
12+
13+
public class PDF2TextExample {
14+
15+
private static final String FILENAME = "src/main/resources/pdf.pdf";
16+
17+
public static void main(String[] args) {
18+
try {
19+
generateTxtFromPDF(FILENAME);
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
25+
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+
35+
try {
36+
parser.parse();
37+
cosDoc = parser.getDocument();
38+
pdfStripper = new PDFTextStripper();
39+
pdDoc = new PDDocument(cosDoc);
40+
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 {
55+
PrintWriter pw = new PrintWriter("src/output/pdf.txt");
56+
pw.print(parsedText);
57+
pw.close();
58+
} catch (Exception e) {
59+
System.out.println("An exception occured in writing the pdf text to file.");
60+
e.printStackTrace();
61+
}
62+
System.out.println("Done.");
63+
}
64+
65+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.pdf;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.IOException;
5+
6+
import org.apache.poi.xwpf.usermodel.BreakType;
7+
import org.apache.poi.xwpf.usermodel.XWPFDocument;
8+
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
9+
import org.apache.poi.xwpf.usermodel.XWPFRun;
10+
11+
import com.itextpdf.text.pdf.PdfReader;
12+
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
13+
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
14+
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
15+
16+
public class PDF2WordExample {
17+
18+
private static final String FILENAME = "src/main/resources/pdf.pdf";
19+
20+
public static void main(String[] args) {
21+
try {
22+
generateDocFromPDF(FILENAME);
23+
} catch (IOException e) {
24+
e.printStackTrace();
25+
}
26+
}
27+
28+
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
31+
XWPFDocument doc = new XWPFDocument();
32+
33+
// Open the pdf file
34+
String pdf = filename;
35+
PdfReader reader = new PdfReader(pdf);
36+
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
37+
38+
// Read the PDF page by page
39+
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
40+
TextExtractionStrategy strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
41+
// Extract the text
42+
String text = strategy.getResultantText();
43+
// Create a new paragraph in the word document, adding the extracted
44+
// text
45+
XWPFParagraph p = doc.createParagraph();
46+
XWPFRun run = p.createRun();
47+
run.setText(text);
48+
// Adding a page break
49+
run.addBreak(BreakType.PAGE);
50+
}
51+
// Write the word document
52+
FileOutputStream out = new FileOutputStream("src/output/pdf.docx");
53+
doc.write(out);
54+
// Close all open files
55+
out.close();
56+
reader.close();
57+
doc.close();
58+
System.out.println("Done.");
59+
}
60+
61+
}

‎pdf/src/main/resources/pdf.pdf‎

810 KB
Binary file not shown.

‎pom.xml‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.baeldung</groupId>
55
<artifactId>parent-modules</artifactId>
@@ -144,6 +144,7 @@
144144
<module>xml</module>
145145
<module>xmlunit2</module>
146146
<module>xstream</module>
147-
</modules>
147+
<module>pdf</module>
148+
</modules>
148149

149-
</project>
150+
</project>

0 commit comments

Comments
 (0)