3

I am trying to insert the following text in the document using Apache POI 3.8:

[Bold][Normal],

but the output document has this:

[Bold][Normal]

The code:

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        final HWPFDocument doc = new HWPFDocument(new FileInputStream("empty.dot"));

        final Range range = doc.getRange();
        final CharacterRun cr1 = range.insertAfter("[Bold]");
        cr1.setBold(true);

        final CharacterRun cr2 = cr1.insertAfter("[Normal]");
        cr2.setBold(false);

        doc.write(new FileOutputStream("output.doc"));
    }
}

What is the correct way of doing this?

rmtheis
  • 5,992
  • 12
  • 61
  • 78
Frolovskij
  • 31
  • 3
  • I think you might have issues trying it on the overall range. Can you try getting just one paragraph, and appending the runs to that, and see if that behaves better? – Gagravarr Jul 06 '12 at 11:19
  • `final Range range = doc.getRange().getParagraph(0);` - doesn't help, the text is still [Bold][Normal]. I've tried the similar approach with XWPF and it works as intended, but I still need HWPF. – Frolovskij Jul 06 '12 at 11:39

1 Answers1

-3

I do it like this. Using POI 3.11

paragraph = doc.createParagraph();
paragraph.setStyle(DOG_HEAD_STYLE);
XWPFRun tmpRun = paragraph.createRun();
tmpRun.setText("non bold text ");

tmpRun = paragraph.createRun();
tmpRun.setBold(true);
tmpRun.setText("bold text");
tmpRun = paragraph.createRun();
tmpRun.setBold(false);
tmpRun.setText(" non bold text again");
Alexey Vashchenkov
  • 233
  • 1
  • 4
  • 12