Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get font size of paragraph from docx document using apache POI?

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.

like image 290
Om Prakash Avatar asked Oct 25 '25 06:10

Om Prakash


1 Answers

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());
}
like image 80
Utkarsh Bhatt Avatar answered Oct 26 '25 19:10

Utkarsh Bhatt