Aspose.Words for Java - Inserting Lists

Is it possible to insert new items into an existing list within a document? I have a document with a few lists in it and I want to be able to add new items to one of the lists. Also, it seems that whenever a add a new list do a document it always has an extra empty item at the end of the list… Thanks.

Jeff

Hi

Thanks for your request. Please see the following link to learn how to create list in the document:
https://docs.aspose.com/words/java/working-with-lists/

You can use the same technique to add new item into existing list. Just move documentbuilder cursor to the end of list item and inset ParagraphBreak.

Also, you can attach your document and tell me where you would like to insert new list item and I will create sample code for you.

Best regards.

Thanks for you response Alexey! Attached is the document in which I would like to append to an existing list. The list is on page 4 of the document. Thanks for you help!

Jeff

Hi

Thank you for additional information. I see the there is a merge field where you would like to add list items. I would like to suggest you using mail merge with regions in this case.

Please see the following code and sample document attached to this post.

// Open the document.
Document doc = new Document("C:\\Temp\\in.doc");
//Get data
Connection conn = createConnection();
ResultSet rs = getDocumentsData(conn);
//Execure mail merge
doc.getMailMerge().executeWithRegions("list", rs);
// Save the modified document.
doc.save("C:\\Temp\\out.doc");
Another way to achieve what you need is using DocumentBuilder. 
// Open the document and create DocumentBuilder
Document doc = new Document("C:\\Temp\\in1.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
//Move document builder cursor to the merge field
builder.moveToMergeField("item");
//Insert few items
builder.writeln("item 2");
builder.writeln("item 3");
builder.writeln("item 4");
//Resert numbering
builder.getListFormat().removeNumbers();
// Save the modified document.
doc.save("C:\\Temp\\out1.doc");

Hope this helps.

Best regards.

I will try it! Thanks for your help!

Jeff

How do I know if I am creating a new List vs appending to an existing List? I need to know because if it is a new List I might need to change the Numbering style. Thanks.

Jeff

As a follow up to my previous post, I want to be more specific. In the attached document from the earlier post there are two merge fields (TitleRequirements and TitleExceptions). I wish to insert a new numbered list for the TitleRequirements merge field and I wish to continue the existing List for TitleExceptions. I can continue the TitleExcpetions list just fine, but for some reason, I cannot get numbering to appear in the TitleRequirements List. Actually, it doesn’t appear to insert a list at all, just the values I have passed to insert. I am using the “moveToMergeField” method you specified in an earlier post. Thanks.

Jeff

Hi

Thanks for your inquiry. Could you please show me your code? In additional, please simplify your document and attach it here, also please attach your output document and expected output. I will check your code and documents and provide you more information.

Best regards,

I cannot simplify my document, as this is a production template I will have to handle. I have attached it here. What I am seeing now is that whenever I try to number the new List both lists end up having strange characters instead of actual numbers or letters. The code is in a test case, but this is how I am doing it:

private class MergeFieldInsertList implements MergeFieldEventHandler {

    /**
     * @param o
     * @param e
     * @throws Exception
     */
    public void mergeField(Object o, MergeFieldEventArgs e) throws Exception {
        if (e.getDocumentFieldName().equals("TitleExceptions")) {
            DocumentBuilder builder = new DocumentBuilder(e.getDocument());
            ListCollection lc = e.getDocument().getLists();
            builder.moveToMergeField(e.getDocumentFieldName());
            ListFormat lf = builder.getListFormat();
            // lf.applyNumberDefault();
            builder.writeln("this is 1 item");
            builder.writeln("this is 2 item");
            builder.writeln("this is 3 item");
            builder.write("this is 4 item");

            // lf.removeNumbers();

            // e.setText("");
        } else if (e.getDocumentFieldName().equals("TitleRequirements")) {
            DocumentBuilder builder = new DocumentBuilder(e.getDocument());
            ListCollection lc = e.getDocument().getLists();
            builder.moveToMergeField(e.getDocumentFieldName());
            ListFormat lf = builder.getListFormat();
            lf.setList(lc.add(ListTemplate.NUMBER\_DEFAULT));
            builder.writeln("this is 1 item");
            builder.writeln("this is 2 item");
            builder.writeln("this is 3 item");
            builder.write("this is 4 item");

        }
    }
}

Attached is the output doc I get with using the code from my previous
post. You will notice on page three that the list has strange
characters instead of numbers. The existing list looks fine. Also, how
do I know if for a particular mergefield I am in an existing List or if
there is no list present? If there is no list present I will need to
insert a new List. Thanks.

Jeff

Hi

Thank you for additional information. I managed to reproduce the problem with lists. I will notify you as soon as it is fixed.

Regarding checking whether the paragraph is a list item, you can use the following code to do that:

if(builder.getCurrentParagraph().isListItem()){.....}

Best regards.

This is a major bug for us, as we have a deadline to put this into production. Is there anything I can do on my end to fix this? Thanks!

Jeff

Hi Jeff,

Thanks for your request. As a workaround, you can try using predefined lists in your document instead of creating list in the code. As you do at “TitleExceptions” merge field.

We will try to resolve the problem with lists before next hotfix. I will notify you as soon as the problem is resolved.

Best regards.

I have another question regarding lists. I want to be able to add a single list item which will be essentially a formatted paragraph. By formatting I mean it will have newline and tab characters. Whenever I try to insert a String containing this formatted text, I get multiple list items, one for each newline character in the formatted text. Is there a way I can insert a Paragraph or something similar for a list item? Thanks.

Jeff

Hi Jeff,

Thanks for your inquiry. You can use line break (\v) instead of paragraph break.
https://reference.aspose.com/words/java/com.aspose.words/controlchar#LINE_BREAK

Hope this helps.

Best regards.

Thanks for the quick reply. So do I need to search and replace newline characters in my String and replace with \v character? Is this pretty normal when using the java component to replace all newline(\n) with this line break char? Do you have any sample code? Cheers!

Jeff

After thinking about it. I think I misunderstood your last response. I think you were intending on me calling builder.insertBreak(BreakType.LINE_BREAK)…

What I am trying to do is insert a String like the following:

String item1 = "This is the first line\n\t\t\t This is the second line\n";
String item2 = "This is third line";

as a single List item:

A. This is the first line

This is the second line

B. This is the third line

Basically, I am getting a block of text from a database that has formatting characters already in it, and I need to insert it as a single formatted list item. Any ideas?

Jeff

Hi Jeff,

Thanks for your request. Yes, you should just replace paragraph break with line break in your string. Please see the following code example:

String item1 = "This is the first line\n\t\t\t This is the second line\n";
String item2 = "This is third line";
// Replace \n in the strings with \v
item1 = item1.replace('\n', ControlChar.LINE_BREAK_CHAR);
item2 = item2.replace('\n', ControlChar.LINE_BREAK_CHAR);
// Create Docuemnt and DocumentBuilder
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getListFormat().applyBulletDefault();
// Insert items
builder.writeln(item1);
builder.writeln(item2);
builder.getListFormat().removeNumbers();
// Save output document
doc.save("C:\\Temp\\out.doc");

Hope this helps.

Best regards.

Hey Alexey,

Unfortunately using this code the formatting gets really wacky. Please
look at page 4 of the attached merged.doc. The template is also
attached that I am using. Thanks.

Jeff

Hi

Thank you for additional information. This occurs because your paragraphs are justified. Please try using this code:

// Create Docuemnt and DocumentBuilder
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getListFormat().applyBulletDefault();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
// Insert items
builder.writeln(item1);
builder.writeln(item2);

Best regards.