Skip to content

Commit d1a12c2

Browse files
authored
Merge pull request eugenp#831 from maibin/master
X to PDF conversions
2 parents 4e1be85 + 3a83664 commit d1a12c2

7 files changed

Lines changed: 200 additions & 18 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.hexToAscii;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class HexToAscii {
8+
9+
@Test
10+
public static void whenHexToAscii() {
11+
String asciiString = "http://www.baeldung.com/jackson-serialize-dates";
12+
String hexEquivalent = "687474703a2f2f7777772e6261656c64756e672e636f6d2f6a61636b736f6e2d73657269616c697a652d6461746573";
13+
14+
assertEquals(asciiString, hexToAscii(hexEquivalent));
15+
}
16+
17+
@Test
18+
public static void whenAsciiToHex() {
19+
String asciiString = "http://www.baeldung.com/jackson-serialize-dates";
20+
String hexEquivalent = "687474703a2f2f7777772e6261656c64756e672e636f6d2f6a61636b736f6e2d73657269616c697a652d6461746573";
21+
22+
assertEquals(hexEquivalent, asciiToHex(asciiString));
23+
}
24+
25+
//
26+
27+
private static String asciiToHex(String asciiStr) {
28+
char[] chars = asciiStr.toCharArray();
29+
StringBuilder hex = new StringBuilder();
30+
for (char ch : chars) {
31+
hex.append(Integer.toHexString((int) ch));
32+
}
33+
34+
return hex.toString();
35+
}
36+
37+
private static String hexToAscii(String hexStr) {
38+
StringBuilder output = new StringBuilder("");
39+
for (int i = 0; i < hexStr.length(); i += 2) {
40+
String str = hexStr.substring(i, i + 2);
41+
output.append((char) Integer.parseInt(str, 16));
42+
}
43+
return output.toString();
44+
}
45+
46+
}

‎pdf/pom.xml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
<version>5.5.10</version>
4141
</dependency>
4242
<dependency>
43-
<groupId>org.apache.poi</groupId>
44-
<artifactId>poi</artifactId>
45-
<version>3.15</version>
43+
<groupId>com.itextpdf.tool</groupId>
44+
<artifactId>xmlworker</artifactId>
45+
<version>5.5.10</version>
4646
</dependency>
4747
<dependency>
4848
<groupId>org.apache.poi</groupId>

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.baeldung.pdf;
22

33
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
46
import java.io.IOException;
57
import java.io.PrintWriter;
68
import java.io.Writer;
@@ -10,14 +12,21 @@
1012
import org.apache.pdfbox.pdmodel.PDDocument;
1113
import org.fit.pdfdom.PDFDomTree;
1214

15+
import com.itextpdf.text.Document;
16+
import com.itextpdf.text.DocumentException;
17+
import com.itextpdf.text.pdf.PdfWriter;
18+
import com.itextpdf.tool.xml.XMLWorkerHelper;
19+
1320
public class PDF2HTMLExample {
1421

15-
private static final String FILENAME = "src/main/resources/pdf.pdf";
22+
private static final String PDF = "src/main/resources/pdf.pdf";
23+
private static final String HTML = "src/main/resources/html.html";
1624

1725
public static void main(String[] args) {
1826
try {
19-
generateHTMLFromPDF(FILENAME);
20-
} catch (IOException | ParserConfigurationException e) {
27+
generateHTMLFromPDF(PDF);
28+
generatePDFFromHTML(HTML);
29+
} catch (IOException | ParserConfigurationException | DocumentException e) {
2130
e.printStackTrace();
2231
}
2332
}
@@ -32,4 +41,12 @@ private static void generateHTMLFromPDF(String filename) throws ParserConfigurat
3241
pdf.close();
3342
}
3443
}
44+
45+
private static void generatePDFFromHTML(String filename) throws ParserConfigurationException, IOException, DocumentException {
46+
Document document = new Document();
47+
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("src/output/html.pdf"));
48+
document.open();
49+
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(filename));
50+
document.close();
51+
}
3552
}
Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
package com.baeldung.pdf;
22

3+
import java.awt.image.BufferedImage;
4+
import java.io.File;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.net.URL;
8+
39
import org.apache.pdfbox.pdmodel.PDDocument;
410
import org.apache.pdfbox.rendering.ImageType;
511
import org.apache.pdfbox.rendering.PDFRenderer;
612
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
713

8-
import java.awt.image.BufferedImage;
9-
import java.io.File;
10-
import java.io.IOException;
14+
import com.itextpdf.text.BadElementException;
15+
import com.itextpdf.text.Document;
16+
import com.itextpdf.text.DocumentException;
17+
import com.itextpdf.text.Image;
18+
import com.itextpdf.text.pdf.PdfWriter;
1119

1220
public class PDF2ImageExample {
1321

14-
private static final String FILENAME = "src/main/resources/pdf.pdf";
15-
22+
private static final String PDF = "src/main/resources/pdf.pdf";
23+
private static final String JPG = "http://cdn2.baeldung.netdna-cdn.com/wp-content/uploads/2016/05/baeldung-rest-widget-main-1.2.0";
24+
private static final String GIF = "https://media.giphy.com/media/l3V0x6kdXUW9M4ONq/giphy";
25+
1626
public static void main(String[] args) {
1727
try {
18-
generateImageFromPDF(FILENAME, "png");
19-
generateImageFromPDF(FILENAME, "jpeg");
20-
generateImageFromPDF(FILENAME, "gif");
21-
} catch (IOException e) {
28+
generateImageFromPDF(PDF, "png");
29+
generateImageFromPDF(PDF, "jpeg");
30+
generateImageFromPDF(PDF, "gif");
31+
generatePDFFromImage(JPG, "jpg");
32+
generatePDFFromImage(GIF, "gif");
33+
} catch (IOException | DocumentException e) {
2234
e.printStackTrace();
2335
}
2436
}
@@ -32,4 +44,19 @@ private static void generateImageFromPDF(String filename, String extension) thro
3244
}
3345
document.close();
3446
}
47+
48+
private static void generatePDFFromImage(String filename, String extension)
49+
throws IOException, BadElementException, DocumentException {
50+
Document document = new Document();
51+
String input = filename + "." + extension;
52+
String output = "src/output/" + extension + ".pdf";
53+
FileOutputStream fos = new FileOutputStream(output);
54+
PdfWriter writer = PdfWriter.getInstance(document, fos);
55+
writer.open();
56+
document.open();
57+
document.add(Image.getInstance((new URL(input))));
58+
document.close();
59+
writer.close();
60+
}
61+
3562
}

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.baeldung.pdf;
22

3+
import java.io.BufferedReader;
34
import java.io.File;
5+
import java.io.FileOutputStream;
6+
import java.io.FileReader;
47
import java.io.IOException;
58
import java.io.PrintWriter;
69

@@ -10,14 +13,24 @@
1013
import org.apache.pdfbox.pdmodel.PDDocument;
1114
import org.apache.pdfbox.text.PDFTextStripper;
1215

16+
import com.itextpdf.text.Document;
17+
import com.itextpdf.text.DocumentException;
18+
import com.itextpdf.text.Element;
19+
import com.itextpdf.text.Font;
20+
import com.itextpdf.text.PageSize;
21+
import com.itextpdf.text.Paragraph;
22+
import com.itextpdf.text.pdf.PdfWriter;
23+
1324
public class PDF2TextExample {
1425

15-
private static final String FILENAME = "src/main/resources/pdf.pdf";
26+
private static final String PDF = "src/main/resources/pdf.pdf";
27+
private static final String TXT = "src/main/resources/txt.txt";
1628

1729
public static void main(String[] args) {
1830
try {
19-
generateTxtFromPDF(FILENAME);
20-
} catch (IOException e) {
31+
generateTxtFromPDF(PDF);
32+
generatePDFFromTxt(TXT);
33+
} catch (IOException | DocumentException e) {
2134
e.printStackTrace();
2235
}
2336
}
@@ -45,4 +58,27 @@ private static void generateTxtFromPDF(String filename) throws IOException {
4558
pw.close();
4659
}
4760

61+
private static void generatePDFFromTxt(String filename) throws IOException, DocumentException {
62+
Document pdfDoc = new Document(PageSize.A4);
63+
PdfWriter.getInstance(pdfDoc, new FileOutputStream("src/output/txt.pdf"))
64+
.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
65+
pdfDoc.open();
66+
67+
Font myfont = new Font();
68+
myfont.setStyle(Font.NORMAL);
69+
myfont.setSize(11);
70+
pdfDoc.add(new Paragraph("\n"));
71+
72+
BufferedReader br = new BufferedReader(new FileReader(filename));
73+
String strLine;
74+
while ((strLine = br.readLine()) != null) {
75+
Paragraph para = new Paragraph(strLine + "\n", myfont);
76+
para.setAlignment(Element.ALIGN_JUSTIFIED);
77+
pdfDoc.add(para);
78+
}
79+
80+
pdfDoc.close();
81+
br.close();
82+
}
83+
4884
}

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>A very simple webpage</title>
6+
</head>
7+
8+
<body>
9+
<h1>A very simple webpage. This is an "h1" level header.</h1>
10+
11+
<h2>This is a level h2 header.</h2>
12+
13+
<h6>This is a level h6 header. Pretty small!</h6>
14+
15+
<p>This is a standard paragraph.</p>
16+
17+
<p align=center>Now I've aligned it in the center of the screen.</p>
18+
19+
<p align=right>Now aligned to the right</p>
20+
21+
<p><b>Bold text</b></p>
22+
23+
<p><strong>Strongly emphasized text</strong> Can you tell the difference vs. bold?</p>
24+
25+
<p><i>Italics</i></p>
26+
27+
<p><em>Emphasized text</em> Just like Italics!</p>
28+
29+
<h2>How about a nice ordered list!</h2>
30+
<ol>
31+
<li>This little piggy went to market</li>
32+
<li>This little piggy went to SB228 class</li>
33+
<li>This little piggy went to an expensive restaurant in Downtown Palo Alto</li>
34+
<li>This little piggy ate too much at Indian Buffet.</li>
35+
<li>This little piggy got lost</li>
36+
</ol>
37+
38+
<h2>Unordered list</h2>
39+
<ul>
40+
<li>First element</li>
41+
<li>Second element</li>
42+
<li>Third element</li>
43+
</ul>
44+
45+
46+
<p>And finally, how about some</p><a href="http://www.google.com/">Links?</a>
47+
48+
<p>Remember, you can view the HTMl code from this or any other page by using the "View Page Source" command of your browser.</p>
49+
50+
</body>
51+
52+
</html>
53+

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Test
2+
Text
3+
Test TEST

0 commit comments

Comments
 (0)