3

Is it possible to set bold to true and false in one run?

I have the following code on the moment :

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

I have tried to add run.setBold(false) into if(paragraphs.getNumFmt() != null) statement but then all the text is flat again.

I would like the text from the last if statement flat and from the first if bold.

Update (with 2 different runs)

If I create two runs my text order is going wrong.

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

Hope this make it clearer

YowE3K
  • 23,852
  • 7
  • 26
  • 40
Patrick
  • 331
  • 3
  • 18
  • "Is it possible to set bold to true and false in one run?": No. – Axel Richter Oct 03 '17 at 12:57
  • @Alex Richter, Do u have any solution to that? I can make two `XWPFRun` but then the text is going out of the right order. – Patrick Oct 03 '17 at 12:58
  • Why "the text is going out of the right order" using multiple runs? All other people are using multiple runs for styling text without any problems. Please show a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) which shows your problem in reproducible manner. – Axel Richter Oct 03 '17 at 13:04
  • @AxelRichter Alright I have tried to, see my update – Patrick Oct 03 '17 at 13:12
  • @AxelRichter If u have some free time I would appreciate it if u can have a look at my updated post. – Patrick Oct 04 '17 at 06:44
  • You haven't read the linked help page of how to create a Minimal, Complete, and Verifiable example. Using the code you provides, helpers cannot create their own test cases in an easy way. And I will not bothering myself with creating test cases based on suspicions which then possibly are wrong. This is wasted time. – Axel Richter Oct 04 '17 at 07:53

1 Answers1

0

I have a solution for you. Its a little bit hard coded you should get the point!

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