It is necessary to copy the first page of the pdf file to the word file (doc, docx) using jp java.
1 answer
//Create the word document XWPFDocument doc = new XWPFDocument(); // Open the pdf file String pdf = "myfile.pdf"; PdfReader reader = new PdfReader(pdf); PdfReaderContentParser parser = new PdfReaderContentParser(reader); // Read the PDF page by page for (int i = 1; i <= reader.getNumberOfPages(); i++) { TextExtractionStrategy strategy = parser.processContent(i, new SimpleTextExtractionStrategy()); // Extract the text String text=strategy.getResultantText(); // Create a new paragraph in the word document, adding the extracted text XWPFParagraph p = doc.createParagraph(); XWPFRun run = p.createRun(); run.setText(text); // Adding a page break run.addBreak(BreakType.PAGE); } // Write the word document FileOutputStream out = new FileOutputStream("myfile.docx"); doc.write(out); // Close all open files out.close(); reader.close(); To help
- This is a good and most likely working method, but the problem is that: PdfReaderContentParser parser = new PdfReaderContentParser (reader); this class is not in the free version of itext7 what to do? - Anatoly
|