3
votes

I have a MS-Word document in docx format. I want to get font size of each paragraph or line. I tried looking similar approach used here for docx file on apache POI documentation page but could not found one.

I tried this approach but it is giving -1 as font size and font-name as null.

File file = new File(fileName);
FileInputStream fis = new FileInputStream(file.getAbsolutePath());

XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();

System.out.println("Total no of paragraph in Docx : "+paragraphs.size());
for (XWPFParagraph para : paragraphs) {
    XWPFStyle style = document.getStyles().getStyle(para.getStyleID());
    System.out.println(para.getText());
    int pos = 0;
    for (XWPFRun run : para.getRuns()) {
      System.out.println("Current run IsBold : " + run.isBold());
      System.out.println("Current run IsItalic : " + run.isItalic());
      System.out.println("Current Font Size : " + run.getFontSize());
      System.out.println("Current Font Name : " + run.getFontName());
    }
  }
  fis.close();

Update: I found this, but could not manage to get font size.

Thanks in advance.

1
Most likely that means the run doesn't have explicit font details set, and is falling back on the paragraph's style. What happens if you try getting the font stuff off the paragraph?Gagravarr
Any idea you would like to suggest me to get the font-details? any library or any other approach?Om Prakash

1 Answers

2
votes

For the font size, you can code as below :

int fontSize = run.getFontSize();
if (fontSize == -1){
   System.out.println(document.getStyles().getDefaultRunStyle().getFontSize())
}else{
    System.out.println(run.getFontSize());
}