X to PDF conversions
This commit is contained in:
parent
e356d28325
commit
6a98064795
|
@ -40,9 +40,9 @@
|
|||
<version>5.5.10</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi</artifactId>
|
||||
<version>3.15</version>
|
||||
<groupId>com.itextpdf.tool</groupId>
|
||||
<artifactId>xmlworker</artifactId>
|
||||
<version>5.5.10</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package com.baeldung.pdf;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.Writer;
|
||||
|
@ -10,14 +12,21 @@ import javax.xml.parsers.ParserConfigurationException;
|
|||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
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 {
|
||||
|
||||
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) {
|
||||
try {
|
||||
generateHTMLFromPDF(FILENAME);
|
||||
} catch (IOException | ParserConfigurationException e) {
|
||||
generateHTMLFromPDF(PDF);
|
||||
generatePDFFromHTML(HTML);
|
||||
} catch (IOException | ParserConfigurationException | DocumentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -32,4 +41,12 @@ public class PDF2HTMLExample {
|
|||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,24 +1,36 @@
|
|||
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.rendering.ImageType;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import com.itextpdf.text.BadElementException;
|
||||
import com.itextpdf.text.Document;
|
||||
import com.itextpdf.text.DocumentException;
|
||||
import com.itextpdf.text.Image;
|
||||
import com.itextpdf.text.pdf.PdfWriter;
|
||||
|
||||
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) {
|
||||
try {
|
||||
generateImageFromPDF(FILENAME, "png");
|
||||
generateImageFromPDF(FILENAME, "jpeg");
|
||||
generateImageFromPDF(FILENAME, "gif");
|
||||
} catch (IOException e) {
|
||||
generateImageFromPDF(PDF, "png");
|
||||
generateImageFromPDF(PDF, "jpeg");
|
||||
generateImageFromPDF(PDF, "gif");
|
||||
generatePDFFromImage(JPG, "jpg");
|
||||
generatePDFFromImage(GIF, "gif");
|
||||
} catch (IOException | DocumentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -32,4 +44,19 @@ public class PDF2ImageExample {
|
|||
}
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package com.baeldung.pdf;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
|
@ -10,14 +13,24 @@ import org.apache.pdfbox.pdfparser.PDFParser;
|
|||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
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 {
|
||||
|
||||
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) {
|
||||
try {
|
||||
generateTxtFromPDF(FILENAME);
|
||||
} catch (IOException e) {
|
||||
generateTxtFromPDF(PDF);
|
||||
generatePDFFromTxt(TXT);
|
||||
} catch (IOException | DocumentException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
@ -45,4 +58,27 @@ public class PDF2TextExample {
|
|||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>A very simple webpage</title>
|
||||
</head>
|
||||
|
||||
<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>
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Test
|
||||
Text
|
||||
Test TEST
|
Loading…
Reference in New Issue