I am reading the text from PDF file using PDFBOX. I am able to read properly using "Rectangle2D". But issue where results shows one by one. Instead i want show in Transpose view.
================================================== ==================================
Current Output ( Line 1 and below line 2 and below line3 and goes on)
PO-00145678
Vendor : AQ-00067
Date...................................: 5/10/2021 Expected DeliveryDate...: 6/10/2021
================================================== ===================================
Expected output In single line(Like Transpose) PO-00145678 Vendor : AQ-00067 Date...................................: 5/10/2021 Expected DeliveryDate...: 6/10/2021
================================================== ===================================
Code using
public class PDFBoxReadFromFile { public static void main(String[] args) throws Exception { try (PDDocument document = PDDocument.load(new File("C:\\Users\\ed\\Documents\\test2.pdf"))) { if (!document.isEncrypted()) { PDFTextStripperByArea stripper = new PDFTextStripperByArea(); stripper.setSortByPosition(true); Rectangle2D rect4 = new Rectangle2D.Double(210, 160, 230, 25); Rectangle rect1 = new Rectangle(55, 290, 225, 17); Rectangle2D rect2 = new Rectangle2D.Double(281, 255, 255, 20); Rectangle2D rect3 = new Rectangle2D.Double(2, 365, 660, 1900); stripper.addRegion("class2", rect1); stripper.addRegion("class3", rect2); stripper.addRegion("class4", rect3); stripper.addRegion("class5", rect4); PDPage firstPage = document.getPages().get(0); stripper.extractRegions(firstPage); System.out.println(stripper.getTextForRegion("class5")); System.out.println(stripper.getTextForRegion("class2")); System.out.println(stripper.getTextForRegion("class3")); System.out.println(stripper.getTextForRegion("class4")); File file = new File("C:/Users/ed/eclipse-workspace/pdfboxreadfromfile/file.txt"); FileWriter fw = new FileWriter(file); PrintWriter pw = new PrintWriter(fw); pw.println(stripper.getTextForRegion("class5")); pw.println(stripper.getTextForRegion("class2")); pw.println(stripper.getTextForRegion("class3")); pw.println(stripper.getTextForRegion("class4")); pw.close(); } } catch (IOException e) { System.err.println("Exception while trying to read pdf document - " + e); } }