The headers are automatically assigned their correct numbers which is great.
I added a bulleted list to one of my sections. As in the examples, I end the bullted list by calling
builder.getListFormat().removeNumbers();
The headers I add after this bulleted list are not being numbered
correctly. How can I end the list & return to the default
numbering that was working before the list?
removeNumbers() sets both list level and list id numbers for the current paragraph to zero – so you zeroed your heading numbers too. I don’t know your context – there are too many combinations that can solve your task. For instance, you can simply remember these numbers for your headers and restore them after bullets:
//writing numbered headers...
//remember list level and id numbers
int listLevelNumber = builder.getListFormat().getListLevelNumber();
int listId = builder.getListFormat().getListId();
//writing some bulleted text...
//restore list numbers
builder.getListFormat().setListId(listId);
builder.getListFormat().setListLevelNumber(listLevelNumber);
Another option, you can include a bulleted style into the same Outline Numbered List where already placed your “Heading 2” and recall bullets by the style name or by setting current paragraph’s list level.
Best Regards,
I get a compile error that the getListId() method doesn’t exist.
It looks like getListFormat returns a ListFormat object and the
getListId method is defined for the List object. I don’t see how
to get the list object?
The above compiles but throws a null pointer exception on the getList() line when I run it.
It also looks like there is no setListId() method.
Here’s my code:
DocumentBuilder builder = new DocumentBuilder(doc);
// get the list level number and list id so we can restore them after the bulleted list
int listLevelNumber = builder.getListFormat().getListLevelNumber();
// throws null pointer exception:
int listId = builder.getListFormat().getList().getListId();
builder.getListFormat().applyBulletDefault();
for (String bulletedText : bulletedTextList)
{
builder.writeln(bulletedText);
}
// end the bullets.
builder.getListFormat().removeNumbers();
// restore the numbers
builder.getListFormat().setListLevelNumber(listLevelNumber);
// (start a new section)
builder.getParagraphFormat().setStyleName("Heading 2");
builder.writeln("Another heading");
“Another heading” prints out with a bullet instead of a level 2 heading.
Sorry I have not made it clear. You should use getList()/setLest() instead of getListId()/setListId(). There is a sample with a default numbering formatting:
@Test
public void TestMixed1() throws Exception
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Heading paragraph without bullets and numbers.");
//apply heading style and default numbering
builder.getParagraphFormat().setStyleName("Heading 2");
builder.getListFormat().applyNumberDefault();
builder.writeln("Heading2 1");
builder.writeln("Heading2 2");
builder.writeln("Heading2 3");
//get the list level number and list id so we can restore them after the bulleted list
List headingList = builder.getListFormat().getList();
int headingListLevel = builder.getListFormat().getListLevelNumber();
//apply normal style and bulleting
builder.getParagraphFormat().setStyleName("Normal");
builder.getListFormat().applyBulletDefault();
builder.writeln("Bulleted text 1");
builder.writeln("Bulleted text 2");
//actually you don't need in removeNumbers() here because setList() and
//setListLevelNumber() sets these numbers again righ below.
// builder.getListFormat().removeNumbers();
//restore heading style, list and list level
builder.getParagraphFormat().setStyleName("Heading 2");
builder.getListFormat().setList(headingList);
builder.getListFormat().setListLevelNumber(headingListLevel);
builder.writeln("Heading2 4");
builder.writeln("Heading2 5");
//end numeration
builder.getListFormat().removeNumbers();
//text without numeration
builder.getParagraphFormat().setStyleName("Normal");
builder.writeln("Footing paragraph without bullets and numbers.");
doc.save("X:\\tmp\\MixedList1.doc");
}
You also can use build-in word templates for numbered, bulleted or outlined lists:
@Test
public void TestMixed2() throws Exception
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Heading paragraph without bullets and numbers.");
//get outline numbering template
List outlineHeadingList = doc.getLists().add(ListTemplate.OUTLINE_HEADINGS_LEGAL);
//apply heading style, list and list level
builder.getParagraphFormat().setStyleName("Heading 1");
builder.getListFormat().setList(outlineHeadingList);
builder.getListFormat().setListLevelNumber(0);
builder.writeln("Heading1 1");
builder.getParagraphFormat().setStyleName("Heading 2");
builder.getListFormat().setListLevelNumber(1);
builder.writeln("Heading2 1");
builder.writeln("Heading2 2");
//apply normal style and bulleting
builder.getListFormat().applyBulletDefault();
builder.getParagraphFormat().setStyleName("Normal");
builder.writeln("Bulleted text 1");
builder.writeln("Bulleted text 2");
//actually you don't need in removeNumbers() here because setList() and
// setListLevelNumber() sets these numbers again.
// builder.getListFormat().removeNumbers();
//continue outlined list
builder.getParagraphFormat().setStyleName("Heading 2");
builder.getListFormat().setList(outlineHeadingList);
builder.getListFormat().setListLevelNumber(1);
builder.writeln("Heading2 3");
builder.writeln("Heading2 4");
//end numeration
builder.getListFormat().removeNumbers();
//text without numeration
builder.getParagraphFormat().setStyleName("Normal");
builder.writeln("Footing paragraph without bullets and numbers.");
doc.save("X:\\tmp\\MixedList2.doc");
}