Getting text style from docx using Apache poi

后端 未结 6 1378
清酒与你
清酒与你 2021-01-14 02:41

I\'m trying to get the style information from an MS docx file, I have no problem writing file content with added styles like bold, italic. font size etc, but reading the fil

6条回答
  •  余生分开走
    2021-01-14 03:26

    Okay, so based on the comments from Gagravarr, the solution is below, exactly as I wanted. So basically Gagravarr answered the question but I'm not sure how apart from saying it hear to give him credit.

    for (XWPFParagraph paragraph : docx.getParagraphs()) {
                    int pos = 0;
                    for (XWPFRun run : paragraph.getRuns()) {
                        System.out.println("Current run IsBold : " + run.isBold());
                        System.out.println("Current run IsItalic : " + run.isItalic());
                        for (char c : run.text().toCharArray()) {
    
                            System.out.print(c);
                            pos++;
                        }
                        System.out.println();
                    }
                }
    

    `

    Output below

    Current run IsBold : false Current run IsItalic : false "Hello, this is  Current run IsBold : true Current run IsItalic : false bold text Current run IsBold : false Current run IsItalic : false  and this is  Current run IsBold : false Current run IsItalic : true italic text Current run IsBold : false Current run IsItalic : false  a Current run IsBold : false Current run IsItalic : false n Current run IsBold : false Current run IsItalic : false d this is  Current run IsBold : true Current run IsItalic : true bold-italic text Current run IsBold : false Current run IsItalic : false "

提交回复
热议问题