Skip to content

Insert paragraphs between numbered / bulleted items #145

@zaryk

Description

@zaryk

I have a solution, but maybe you can consider providing methods, modifying current methods to do similar tasks, or even adding this to your examples for others. It took a while to come up with it, even though in the end, it was a pretty simple solution, but I figured there was a method for it already and first time using the library.

I was trying to add paragraphs between list items, so that the text would be numbered and image would not be numbered, but still be part of the item.

  • Tried Document.AddListItem.InsertParagraphAfterSelf, but since AddListItem returns a List, I wouldn't figure it would work and it didn't. It also gets "Object reference not set to an instance of an object" when doing so.

  • Tried List.Items[0].InsertParagraphAfterSelf, but errors out with something along the lines of "Parent not defined"

  • List.AddItem will create the paragraphs and images before the list, but not between the items.

  • List.Items.Add will add the paragraphs and images between the items, but it looks as if InsertParagraph and CreatePicture are creating the paragraphs and pictures as is, and not just creating objects to be used, so they are created both outside of the items before the list, and 1 set between the items.

  • Unless the project source is added to the project, constructors are only internal, and can't be used.

  • Wasn't sure if there was anything else that could be used, like maybe having a Document.CreateBreak or List.AddBreak.

Code:

         using (DocX docx = DocX.Create(path))
        {
            Novacode.List list = null;
            
            list = docx.AddList(listType: ListItemType.Numbered);
            foreach (var instruction in Instructions)
            {
                docx.AddListItem(list, instruction.Text, 0, ListItemType.Numbered);
                list.Items.Add(docx.InsertParagraph(string.Empty));

                Novacode.Image image = docx.AddImage(instruction.Path);
                Novacode.Picture picture = image.CreatePicture();
                picture.Name = System.IO.Path.GetFileName(instruction.Path);

                list.Items.Add(docx.InsertParagraph().AppendPicture(picture));
                list.Items.Add(docx.InsertParagraph());
            }
            list.Items.Add(docx.InsertParagraph());
            docx.InsertList(list);
           
            docx.Save();
        }

Here is an example document of using the above:
https://drive.google.com/file/d/0BxVsN77LSomYWGZqdkk5Y3dUaWc/view?usp=sharing

My solution was to retrieve the items' paragraph and append a break to it.:

          var list = docx.AddList(listType: ListItemType.Numbered);
             for(int i = 0; i < instructionCount; i++)
             {
                 docx.AddListItem(list, Instructions[i].Text, 0, ListItemType.Numbered);
                 var itemParagraph = list.Items[i].Append("\n");

                 string instructionPath = Instructions[i].Path;

                 Novacode.Image image = docx.AddImage(instructionPath);
                 Novacode.Picture picture = image.CreatePicture();
                 picture.Name = System.IO.Path.GetFileName(instructionPath);

                 itemParagraph.Append("\n").AppendPicture(picture).Append("\n");
             }

Here is an example of the solution:
https://drive.google.com/file/d/0BxVsN77LSomYYWZiYmNBdi13eEE/view?usp=sharing

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions