使用XWPFRun进行一次运行的大胆和平面文本,写入单词

时间:2017-10-03 12:10:32

标签: java text ms-word apache-poi xwpf

是否可以在一次运行中将粗体设置为true和false?

我现在有以下代码:

XWPFParagraph paragraph = nieuwDocument.createParagraph();

XWPFRun run = paragraph.createRun();
run.setBold(true);


for (XWPFParagraph paragraphs: paragraphList)
{   

    if(paragraphs.getStyle() !=null)
    {   
      if(paragraphs.getStyle().equals(kop) || paragraphs.getStyle().equals(tussenkop))
      { 
        run.addBreak();
        run.setText(paragraphs.getText());
        run.addCarriageReturn();
      }  
        if(paragraphs.getNumFmt() != null)
      { 
        //run.setBold(false)
        run.addTab();
        run.setText(paragraphs.getText());
        run.addCarriageReturn();
 }    
}

我尝试将run.setBold(false)添加到if(paragraphs.getNumFmt() != null)语句中,但之后所有文字都再次展开。

我希望上一个if语句中的文字与第一个if粗体相同。

更新(有2次不同的运行)

如果我创建两次运行,我的文本顺序就会出错。

XWPFRun run = paragraph.createRun();
run.setBold(true);

XWPFRun runTwo = paragraph.createRun();
runTwo.setBold(false);

for (XWPFParagraph paragraphs: paragraphList)
  { 

    if(paragraphs.getStyle() !=null)
    {   
     if(paragraphs.getStyle().equals(kop) || paragraphs.getStyle().equals(tussenkop))
         {  
           run.addBreak();
           run.setText(paragraphs.getText());
           run.addCarriageReturn();
          }  

          if(paragraphs.getNumFmt() != null)
          {     
            runTwo.addTab();
            runTwo.setText(paragraphs.getText());
            runTwo.addCarriageReturn();
          }   

Right order (with first code)

Wrong order (second code with two runs

希望这更清楚

1 个答案:

答案 0 :(得分:0)

我为您提供解决方案。它有点硬编码,您应该明白这一点!

public static void textStyledInbetween(XWPFDocument document, String text1, String style1, String text2, String style2, String text3, String style3) {
    XWPFParagraph paragraph = document.createParagraph();
 
    XWPFRun run1 = paragraph.createRun();
    XWPFRun run2 = paragraph.createRun();
    XWPFRun run3 = paragraph.createRun();
    paragraph.setAlignment(ParagraphAlignment.BOTH);
    run1.setFontSize(12);
    run1.setFontFamily("Arial");
    run2.setFontSize(12);
    run2.setFontFamily("Arial");
    run3.setFontSize(12);
    run3.setFontFamily("Arial");

    if (text1 != null) {
        if (style1 != null) {
            if (style1.equals("Bold")) {
                run1.setBold(true);
            } else if (style1.equals("Italian")) {
                run1.setItalic(true);
            }
        }
        run1.setText(text1);
        run1.setBold(false);
        run1.setItalic(false);
    }

    if (text2 != null) {
        if (style2 != null) {
            if (style2.equals("Bold")) {
                run2.setBold(true);
            } else if (style2.equals("Italian")) {
                run2.setItalic(true);
            }
        }
        run2.setText(text2);
    }

    if (text3 != null) {
        if (style3 != null) {
            if (style3.equals("Bold")) {
                run3.setBold(true);
            } else if (style3.equals("Italian")) {
                run3.setItalic(true);
            }
        }
        run3.setText(text3);
    }
}