2014-02-11 2 views
2

PDFBox를 사용하여 PDF 파일의 각 줄의 글꼴을 가져 오는 방법이 있습니까? 이것을 시도했지만 그 페이지에 사용 된 모든 글꼴을 나열합니다. 해당 글꼴에 표시되는 줄 또는 텍스트를 표시하지 않습니다.PDFBox를 사용하여 각 줄의 글꼴 가져 오기

List<PDPage> pages = doc.getDocumentCatalog().getAllPages(); 
for(PDPage page:pages) 
{ 
Map<String,PDFont> pageFonts=page.getResources().getFonts(); 
for(String key : pageFonts.keySet()) 
    { 
    System.out.println(key+" - "+pageFonts.get(key)); 
    System.out.println(pageFonts.get(key).getBaseFont()); 
    } 
} 

모든 의견을 환영합니다. 감사!

답변

11

PDFBox를 사용하여 PDF에서 텍스트 (일반 또는 스타일 정보 포함)를 추출 할 때마다 일반적으로 PDFTextStripper 클래스 나 그 친척 중 하나를 사용하여 시도해야합니다. 이 클래스는 이미 PDF 내용 파싱에 관련된 모든 과중한 작업을 수행합니다.

는이 같은 일반 PDFTextStripper 클래스를 사용

PDDocument document = ...; 
PDFTextStripper stripper = new PDFTextStripper(); 
// set stripper start and end page or bookmark attributes unless you want all the text 
String text = stripper.getText(document); 

이, 예를 들어, 단순히 일반 텍스트를 반환 일부 R40 양식에서 :

Claim for repayment of tax deducted 
from savings and investments 
How to fill in this form 
Please fill in this form with details of your income for the 
above tax year. The enclosed Notes will help you (but there is 
not a note for every box on the form). If you need more help 
with anything on this form, please phone us on the number 
shown above. 
If you are not a UK resident, do not use this form – please 
contact us. 
Please do not send us any personal records, or tax 
certificates or vouchers with your form. We will contact 
you if we need these. 
Please allow four weeks before contacting us about your 
repayment. We will pay you as quickly as possible. 
Use black ink and capital letters 
Cross out any mistakes and write the 
correct information below 
... 

당신은, 다른 한편으로는, 단순한 텍스트보다 그 방법 writeString(String, List<TextPosition>) 및 프로세스 자세한 정보를 덮어 쓸 수 있습니다. 어디 글꼴 변경 사용 된 글꼴의 이름에 대한 정보를 추가하려면, 당신은이를 사용할 수 있습니다

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()); 
    } 
}; 

을 같은 형태로 들어 원하지 않는 경우

[DHSLTQ+IRModena-Bold]Claim for repayment of tax deducted 
from savings and investments 
How to fill in this form 
[OIALXD+IRModena-Regular]Please fill in this form with details of your income for the 
above tax year. The enclosed Notes will help you (but there is 
not a note for every box on the form). If you need more help 
with anything on this form, please phone us on the number 
shown above. 
If you are not a UK resident, do not use this form – please 
contact us. 
[DHSLTQ+IRModena-Bold]Please do not send us any personal records, or tax 
certificates or vouchers with your form. We will contact 
you if we need these. 
[OIALXD+IRModena-Regular]Please allow four weeks before contacting us about your 
repayment. We will pay you as quickly as possible. 
Use black ink and capital letters 
Cross out any mistakes and write the 
correct information below 
... 

를 얻을 수 텍스트와 병합 할 폰트 정보는 단순히 메소드에서 별도의 구조를 작성하여 덮어 씁니다.

TextPosition은 그것이 나타내는 텍스트 조각에 대해 더 많은 정보를 제공합니다. 그것을 검사하십시오!