Tuesday, November 25, 2014

Get PDF fonts information line by line using PDFBox API

Sample code snippet on extracting font information line by line using PDFBox API in JAVA.

 public String[] getFontLineByLineFromPdf(String fileName)throws IOException  
   {  
     PDDocument doc= PDDocument.load(fileName);  
     PDFTextStripper stripper = new PDFTextStripper() {  
       String prevBaseFont = "";  
       protected void writeString(String text, List<TextPosition> textPositions) throws IOException  
       {  
         StringBuilder builder = new StringBuilder();  
         for (TextPosition position : textPositions)  
         {  
           String baseFont = position.getFont().getBaseFont();  
           if (baseFont != null && !baseFont.equals(prevBaseFont))  
           {  
             builder.append('[').append(baseFont).append(']');  
             prevBaseFont = baseFont;  
           }  
           builder.append(position.getCharacter());  
         }  
         writeString(builder.toString());  
       }  
     };  
     String content=stripper.getText(doc);  
     doc.close();  
     String pdfLinesWithFont[]= content.split("\\r?\\n");  
     return pdfLinesWithFont;  
   }  

java

3 comments :

  1. you can try this free online pdf to text converter to convert pdf to text online.

    ReplyDelete
  2. If you are pursuing a career in the information technology field, chances are you will need to obtain some kind of IT certification in order to qualify for a job. There are many IT certification options out there, so sometimes it can be difficult to choose which one is best for you. Comptia A+ Training London

    ReplyDelete

 

© 2011 GIS and Remote Sensing Tools, Tips and more .. ToS | Privacy Policy | Sitemap

About Me