title: How to read the table inside PDF and print the table in HTML format or In Console?
body: <p>I need to read the table inside a <strong>pdf file</strong> and print the table in <strong>HTML format</strong> or in <strong>Console</strong> exactly in PDF. I have a sample code which reading the text inside the table, But I need to read the table column wise and row wise and print as we seen in the Image.I use PDFBox as Jar. Refer this sample image <a href="https://i.stack.imgur.com/r7t4R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r7t4R.png" alt="Sample Image"></a></p>
  
  <pre><code>import org.pdfbox.cos.COSDocument;
  import org.pdfbox.pdfparser.PDFParser;
  import org.pdfbox.pdmodel.PDDocument;
  import org.pdfbox.pdmodel.PDDocumentInformation;
  import org.pdfbox.util.PDFTextStripper;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.PrintWriter;
  public class PDFreader {
  PDFParser parser;
  String parsedText;
  PDFTextStripper pdfStripper;
  PDDocument pdDoc;
  COSDocument cosDoc;
  PDDocumentInformation pdDocInfo;
  // PDFTextParser Constructor 
  public PDFTextParser() {
  }
  // Extract text from PDF Document
  String pdftoText(String fileName) {
  System.out.println("Parsing text from PDF file " +  fileName +"....");     
      File f = new File(fileName);
      if (!f.isFile()) {
          System.out.println("File " + fileName + " does not exist.");
          return null;
      }
      try {
          parser = new PDFParser(new FileInputStream(f));
      } catch (Exception e) {
          System.out.println("Unable to open PDF Parser.");
          return null;
      }
      try {
          parser.parse();
          cosDoc = parser.getDocument();
          pdfStripper = new PDFTextStripper();
          pdDoc = new PDDocument(cosDoc);
          parsedText = pdfStripper.getText(pdDoc); 
      } catch (Exception e) {
          System.out.println("An exception occured in parsing the PDF Document.");
          e.printStackTrace();
          try {
                 if (cosDoc != null) cosDoc.close();
                 if (pdDoc != null) pdDoc.close();
             } catch (Exception e1) {
             e.printStackTrace();
          }
          return null;
      }      
      System.out.println("Done.");
      return parsedText;
  }
  // Write the parsed text from PDF to a file
  void writeTexttoFile(String pdfText, String fileName) {
      System.out.println("\nWriting PDF text to output text file " + fileName + "....");
      try {
          PrintWriter pw = new PrintWriter(fileName);
          pw.print(pdfText);
          pw.close();     
      } catch (Exception e) {
          System.out.println("An exception occured in writing the pdf text to file.");
          e.printStackTrace();
      }
      System.out.println("Done.");
  }
  public static void main(String args[]) {
      String fileList[] = {"SO115638.pdf","New_Document.txt"};
      if (fileList.length != 2) {
          System.out.println("Usage: java PDFTextParser &lt;InputPDFFilename&gt; &lt;OutputTextFile&gt;");
          System.exit(1);
      }
      PDFTextParser pdfTextParserObj = new PDFTextParser();
      String pdfToText = pdfTextParserObj.pdftoText(fileList[0]);
      if (pdfToText == null) {
          System.out.println("PDF to Text Conversion failed.");
      }
      else {
          System.out.println("\nThe text parsed from the PDF Document....\n" + pdfToText);
          pdfTextParserObj.writeTexttoFile(pdfToText, fileList[1]);
      }
  }  }
  </code></pre>