Merge pull request #831 from maibin/master

X to PDF conversions
This commit is contained in:
Grzegorz Piwowarek 2016-11-16 10:29:13 +01:00 committed by GitHub
commit d1a12c2f6c
7 changed files with 200 additions and 18 deletions

View File

@ -0,0 +1,46 @@
package com.baeldung.hexToAscii;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HexToAscii {
@Test
public static void whenHexToAscii() {
String asciiString = "http://www.baeldung.com/jackson-serialize-dates";
String hexEquivalent = "687474703a2f2f7777772e6261656c64756e672e636f6d2f6a61636b736f6e2d73657269616c697a652d6461746573";
assertEquals(asciiString, hexToAscii(hexEquivalent));
}
@Test
public static void whenAsciiToHex() {
String asciiString = "http://www.baeldung.com/jackson-serialize-dates";
String hexEquivalent = "687474703a2f2f7777772e6261656c64756e672e636f6d2f6a61636b736f6e2d73657269616c697a652d6461746573";
assertEquals(hexEquivalent, asciiToHex(asciiString));
}
//
private static String asciiToHex(String asciiStr) {
char[] chars = asciiStr.toCharArray();
StringBuilder hex = new StringBuilder();
for (char ch : chars) {
hex.append(Integer.toHexString((int) ch));
}
return hex.toString();
}
private static String hexToAscii(String hexStr) {
StringBuilder output = new StringBuilder("");
for (int i = 0; i < hexStr.length(); i += 2) {
String str = hexStr.substring(i, i + 2);
output.append((char) Integer.parseInt(str, 16));
}
return output.toString();
}
}

View File

@ -40,9 +40,9 @@
<version>5.5.10</version> <version>5.5.10</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>com.itextpdf.tool</groupId>
<artifactId>poi</artifactId> <artifactId>xmlworker</artifactId>
<version>3.15</version> <version>5.5.10</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>

View File

@ -1,6 +1,8 @@
package com.baeldung.pdf; package com.baeldung.pdf;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.Writer; import java.io.Writer;
@ -10,14 +12,21 @@ import javax.xml.parsers.ParserConfigurationException;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.fit.pdfdom.PDFDomTree; import org.fit.pdfdom.PDFDomTree;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class PDF2HTMLExample { public class PDF2HTMLExample {
private static final String FILENAME = "src/main/resources/pdf.pdf"; private static final String PDF = "src/main/resources/pdf.pdf";
private static final String HTML = "src/main/resources/html.html";
public static void main(String[] args) { public static void main(String[] args) {
try { try {
generateHTMLFromPDF(FILENAME); generateHTMLFromPDF(PDF);
} catch (IOException | ParserConfigurationException e) { generatePDFFromHTML(HTML);
} catch (IOException | ParserConfigurationException | DocumentException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -32,4 +41,12 @@ public class PDF2HTMLExample {
pdf.close(); pdf.close();
} }
} }
private static void generatePDFFromHTML(String filename) throws ParserConfigurationException, IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("src/output/html.pdf"));
document.open();
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(filename));
document.close();
}
} }

View File

@ -1,24 +1,36 @@
package com.baeldung.pdf; package com.baeldung.pdf;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil; import org.apache.pdfbox.tools.imageio.ImageIOUtil;
import java.awt.image.BufferedImage; import com.itextpdf.text.BadElementException;
import java.io.File; import com.itextpdf.text.Document;
import java.io.IOException; import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
public class PDF2ImageExample { public class PDF2ImageExample {
private static final String FILENAME = "src/main/resources/pdf.pdf"; private static final String PDF = "src/main/resources/pdf.pdf";
private static final String JPG = "http://cdn2.baeldung.netdna-cdn.com/wp-content/uploads/2016/05/baeldung-rest-widget-main-1.2.0";
private static final String GIF = "https://media.giphy.com/media/l3V0x6kdXUW9M4ONq/giphy";
public static void main(String[] args) { public static void main(String[] args) {
try { try {
generateImageFromPDF(FILENAME, "png"); generateImageFromPDF(PDF, "png");
generateImageFromPDF(FILENAME, "jpeg"); generateImageFromPDF(PDF, "jpeg");
generateImageFromPDF(FILENAME, "gif"); generateImageFromPDF(PDF, "gif");
} catch (IOException e) { generatePDFFromImage(JPG, "jpg");
generatePDFFromImage(GIF, "gif");
} catch (IOException | DocumentException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -32,4 +44,19 @@ public class PDF2ImageExample {
} }
document.close(); document.close();
} }
private static void generatePDFFromImage(String filename, String extension)
throws IOException, BadElementException, DocumentException {
Document document = new Document();
String input = filename + "." + extension;
String output = "src/output/" + extension + ".pdf";
FileOutputStream fos = new FileOutputStream(output);
PdfWriter writer = PdfWriter.getInstance(document, fos);
writer.open();
document.open();
document.add(Image.getInstance((new URL(input))));
document.close();
writer.close();
}
} }

View File

@ -1,6 +1,9 @@
package com.baeldung.pdf; package com.baeldung.pdf;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
@ -10,14 +13,24 @@ import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper; import org.apache.pdfbox.text.PDFTextStripper;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class PDF2TextExample { public class PDF2TextExample {
private static final String FILENAME = "src/main/resources/pdf.pdf"; private static final String PDF = "src/main/resources/pdf.pdf";
private static final String TXT = "src/main/resources/txt.txt";
public static void main(String[] args) { public static void main(String[] args) {
try { try {
generateTxtFromPDF(FILENAME); generateTxtFromPDF(PDF);
} catch (IOException e) { generatePDFFromTxt(TXT);
} catch (IOException | DocumentException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
@ -45,4 +58,27 @@ public class PDF2TextExample {
pw.close(); pw.close();
} }
private static void generatePDFFromTxt(String filename) throws IOException, DocumentException {
Document pdfDoc = new Document(PageSize.A4);
PdfWriter.getInstance(pdfDoc, new FileOutputStream("src/output/txt.pdf"))
.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
pdfDoc.open();
Font myfont = new Font();
myfont.setStyle(Font.NORMAL);
myfont.setSize(11);
pdfDoc.add(new Paragraph("\n"));
BufferedReader br = new BufferedReader(new FileReader(filename));
String strLine;
while ((strLine = br.readLine()) != null) {
Paragraph para = new Paragraph(strLine + "\n", myfont);
para.setAlignment(Element.ALIGN_JUSTIFIED);
pdfDoc.add(para);
}
pdfDoc.close();
br.close();
}
} }

View File

@ -0,0 +1,53 @@
<!DOCTYPE html>
<html>
<head>
<title>A very simple webpage</title>
</head>
<body>
<h1>A very simple webpage. This is an "h1" level header.</h1>
<h2>This is a level h2 header.</h2>
<h6>This is a level h6 header. Pretty small!</h6>
<p>This is a standard paragraph.</p>
<p align=center>Now I've aligned it in the center of the screen.</p>
<p align=right>Now aligned to the right</p>
<p><b>Bold text</b></p>
<p><strong>Strongly emphasized text</strong> Can you tell the difference vs. bold?</p>
<p><i>Italics</i></p>
<p><em>Emphasized text</em> Just like Italics!</p>
<h2>How about a nice ordered list!</h2>
<ol>
<li>This little piggy went to market</li>
<li>This little piggy went to SB228 class</li>
<li>This little piggy went to an expensive restaurant in Downtown Palo Alto</li>
<li>This little piggy ate too much at Indian Buffet.</li>
<li>This little piggy got lost</li>
</ol>
<h2>Unordered list</h2>
<ul>
<li>First element</li>
<li>Second element</li>
<li>Third element</li>
</ul>
<p>And finally, how about some</p><a href="http://www.google.com/">Links?</a>
<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>
</body>
</html>

View File

@ -0,0 +1,3 @@
Test
Text
Test TEST