Bug 53528 - Inserting consecutive character runs with different character properties not works properly
Summary: Inserting consecutive character runs with different character properties not ...
Status: NEW
Alias: None
Product: POI
Classification: Unclassified
Component: HWPF (show other bugs)
Version: 3.11-FINAL
Hardware: PC All
: P2 normal with 1 vote (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-07-10 10:34 UTC by Aleksey
Modified: 2015-03-15 20:36 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Aleksey 2012-07-10 10:34:07 UTC
When inserting two consecutive character runs with different properties (the first one is bold and the second is not) it results in them having the same properties (both are normal).

I've tried the following code with poi 3.8-20120326 and 3.9-beta1-20120628.

The expected results is "<b>[Bold]</b>[Normal]", but the actual result is "[Bold][Normal]".

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"));
    }
}
Comment 1 Alex 2015-03-15 14:19:57 UTC
Confirm the issue still exists in:

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-scratchpad</artifactId>
      <version>3.11</version>
    </dependency>

Have changed the example to see what each CharacterRun is:

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

public class WordWriter {
    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);
        System.out.println(cr1.text());

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

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

It shows:
[Bold]
[Bold][Normal]

It seems insertAfter returns the resulting string. The question is how to reference the string that was just inserted.

Would be pleased if anyone tells me how to insert two text fragments with different formatting in HWPF.