Frolovskij
Frolovskij

Reputation: 31

Formatting text using Apache POI 3.8 (HWPF)

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?

Upvotes: 3

Views: 1792

Answers (1)

Alexey Vashchenkov
Alexey Vashchenkov

Reputation: 263

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

Upvotes: -3

Related Questions