Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Test PDF to docx conversion using Apache POI and itextpdf libraries
- * by @gaelikun (elcodedocle)
- *
- * This test was performed using poi-ooxml 3.11 and itextpdf 5.5.4
- *
- * Check out http://pastebin.com/6TXUiuQP for a basic pom.xml maven
- * project file including these dependencies
- */
- package info.synapp.tests.poi;
- import com.itextpdf.text.pdf.PdfReader;
- import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
- import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
- import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
- import org.apache.poi.xwpf.usermodel.BreakType;
- import org.apache.poi.xwpf.usermodel.XWPFDocument;
- import org.apache.poi.xwpf.usermodel.XWPFParagraph;
- import org.apache.poi.xwpf.usermodel.XWPFRun;
- import java.io.FileOutputStream;
- import java.io.IOException;
- /**
- * Test PDF to docx conversion using Apache POI and itextpdf libraries
- */
- public class PdfToDocXConversion {
- public static void main(String[] args){
- String pdf = "http://www.energy.umich.edu/sites/default/files/pdf-sample.pdf";
- String destinationDocxFilename = "javadomain1.docx";
- if (args.length > 0){
- pdf = args[0];
- }
- if (args.length > 1){
- destinationDocxFilename = args[1];
- }
- if (args.length > 2) {
- System.out.println("Usage: java route/to/PdfToDocXConversion [sourcepdffilename] [destinationdocxfilename] \n");
- System.exit(1);
- }
- XWPFDocument doc = new XWPFDocument();
- try {
- PdfReader reader = new PdfReader(pdf);
- PdfReaderContentParser parser = new PdfReaderContentParser(reader);
- for (int i = 1; i <= reader.getNumberOfPages(); i++) {
- TextExtractionStrategy strategy = parser.processContent(i,new SimpleTextExtractionStrategy());
- String text = strategy.getResultantText();
- XWPFParagraph p = doc.createParagraph();
- XWPFRun run = p.createRun();
- run.setText(text);
- run.addBreak(BreakType.PAGE);
- }
- FileOutputStream out1 = new FileOutputStream(destinationDocxFilename);
- doc.write(out1);
- out1.close();
- reader.close();
- System.out.println("Document converted successfully\n");
- } catch (IOException e) {
- System.out.println("Something went wrong\n");
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement