Free Support Forum - aspose.com

Adding item in list

Hi,

I want to add a item in list .

How can i do this maintaining the list? (Numbers should change automatically)

Thanks.

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry.

1. The simplest way is moving DocumentBuilder cursor to the end of list item (paragraph) and insert paragraph break. For example, see the following code:

// Open source document.

Document doc = new Document("C:\\Temp\\in.doc");

// Create DocumentBuilder.

DocumentBuilder builder = new DocumentBuilder(doc);

// We know that the last paragraph in the document is list item.

// So move corsor to the end of this paragraph.

// You can insert bookmark at the end of the list,

// and then move cursor to the bookmark.

builder.moveTo(doc.getLastSection().getBody().getLastParagraph());

// Insert paragraph break.

builder.writeln();

// Insert text of the newly inserted list item.

builder.write("This is new list item");

// Save output document.

doc.save("C:\\Temp\\out.doc");

2. If you need to build list from scratch, you can easily achieve this using DocumentBuilder. Follow the link to learn more:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.listformat.applybulletdefault.html

3. Another possible situation is when you have style associated with list. In this case, you should just inset paragraph and specify the appropriate style of paragraph. In the code example, I create such style programmatically, but you might have such style already created in your document.

// Open source document.

Document doc = new Document("C:\\Temp\\in.doc");

// Create DocumentBuilder.

DocumentBuilder builder = new DocumentBuilder(doc);

// Add paragraph style asociated with list.

Style myStyle = doc.getStyles().add(StyleType.PARAGRAPH, "my style");

List myList = doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT);

myStyle.getListFormat().setList(myList);

// Insert few list items.

builder.getCurrentParagraph().getParagraphFormat().setStyle(myStyle);

builder.writeln("item 1");

builder.writeln("item 2");

builder.writeln("item 3");

builder.write("item 4");

// Save output document.

doc.save("C:\\Temp\\out.doc");

4. You can just specify List, which paragraph belongs to, like shown in the following code example:

// Open source document.

Document doc = new Document("C:\\Temp\\in.doc");

// We know that the first paragraph is list item.

// Get List of this paragraph.

List lst = doc.getFirstSection().getBody().getFirstParagraph().getListFormat().getList();

// Add one more paragraph at the end of the document and set List.

Paragraph par = new Paragraph(doc);

par.appendChild(new Run(doc, "This is new list item."));

par.getListFormat().setList(lst);

// Insert paragraph into the document.

doc.getFirstSection().getBody().appendChild(par);

// Save output document.

doc.save("C:\\Temp\\out.doc");

Hope this information might be useful for you. Please let me know in case of any issues, I will be glad to help you.

Best regards.