This project has moved. For the latest updates, please go here.

Adding support for lists

Developer
Jul 24, 2012 at 3:31 PM

Hello, 

I am planning to work on a feature to allow DocX to support lists (bullet, number) - single level lists for the time being.

To support reading list information, I have added an isListItem and ListType properties to the Paragraph and read that information from the <numPr> node in paragraphs and the <num> and corresponding <abstractNum> nodes in styles.xml which I get from document.numbering. This allows a paragraph to be delineated with the proper list type information if the paragraph is a list item.

To support creating lists in DocX, can I get some input on what would be the most appropriate way to extend the API? Although the focus right now is capability to support single-level lists, any caveats related to multi-level lists would also be helpful.

 

Thanks, 

Developer
Jul 24, 2012 at 3:59 PM

DocX community will gladly accept your addition. Good Luck! Propose what you had in mind (code wise) and maybe someone will tell you how he sees it. Otherwise there's no starting point ;)

Developer
Jul 24, 2012 at 4:26 PM

 

Thanks MadBoy. 

For creating lists I was thinking something along the following lines:

List = document.List.Add(ListType.Bulleted)
where list type would be Bulleted or Numbered

Behind the scenes, should List be an object derived from paragraph? This also implies that we have a list collection. But since lists are essentially paragraphs with <numPr> nodes that link to <abstractNum> nodes in styles.xml, how do we create/link this information when creating a list? A secondary issue would be adding text/content to the created list. 

Developer
Jul 24, 2012 at 5:02 PM
Edited Jul 24, 2012 at 5:25 PM

Shouldn't it be more like a copy of creating table approach?

 

Paragraph p = document.InsertParagraph();

List myList = p.InsertListAfterSelft(5); // .InsertListAfterSelf

myList.Type = ListType.Bulleted;

myList.Row[0].Paragraph[0].Text = something

or

myList.Row[0].Paragraph[0].InsertText("Nazwa", false, formattingTabelaContent);

 

or something similar.. surely all options could be done that way.. Of course List might need new name to coexist with Lists.

Developer
Jul 24, 2012 at 6:29 PM

MadBoy, replicating the table approach looks more feasible.  Thanks - 

Developer
Jul 24, 2012 at 8:03 PM

One issue here with the List object inheriting from Container is that there is no "list" xElement node, rather a list is actually a collection of paragraph nodes with <numPr> nodes containing list styling information.

Developer
Jul 26, 2012 at 2:27 PM

 

In the file HelperFunctions.cs there is a method AddDefaltStylesXml(..) method which uses the Novacode.Resources.default_styles.xml.gz xml resource to get the default MS Word styles.xml. Is there a default resource for numbering.xml that I can use?  I need to create a default numbering.xml when a list is added to a DocX document.

Thanks, 

Coordinator
Jul 26, 2012 at 2:31 PM
Hi faizanj,

In the file HelperFunctions.cs there is a method AddDefaltStylesXml(..) method which uses the Novacode.Resources.default_styles.xml.gz xml resource to get the default MS Word styles.xml. Is there a default resource for numbering.xml that I can use? I need to create a default numbering.xml when a list is added to a DocX document.


There currently is no default resource for numbering.xml.
You should copy the AddDefaltStylesXml() implementation and create your own similar function AddDefaltNumeringXml().
This was the best approach I could find to solve this problem.

Regards,
Cathal

Developer
Jul 26, 2012 at 2:41 PM

 

Thanks Cathal - I was doing exactly what you mentioned but I was wondering if there exists a default implementation of numbering.xml for MS Word. Did you create your own default styles.xml resource or is it an "official" MS Word styles.xml from openxml or some other place?

Coordinator
Jul 26, 2012 at 2:48 PM

It was a long time ago... but I believe if you generate an empty document with word this styles.xml file is automatically created. I simply extracted and zipped this file.

On 26 Jul 2012 14:41, "faizanj" <notifications@codeplex.com> wrote:

From: faizanj

Thanks Cathal - I was doing exactly what you mentioned but I was wondering if there exists a default implementation of numbering.xml for MS Word. Did you create your own default styles.xml resource or is it an "official" MS Word styles.xml from openxml or some other place?

Read the full discussion online.

To add a post to this discussion, reply to this email (DocX@discussions.codeplex.com)

To start a new discussion for this project, email DocX@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Developer
Jul 26, 2012 at 2:55 PM

 

Cathal, yes, Word does generate a default styles.xml with a blank document. But it only generates a numbering.xml when there is atleast one list in a document. Thus it looks like there is no default numbering.xml representation.

Coordinator
Jul 26, 2012 at 3:16 PM
>> Thus it looks like there is no default numbering.xml representation.

Hmm... one way to solve this would be the following. There are only 3 default list options offered by Word?
Inline image 2
If you create a document which contains one of each list type, you could consider the resulting numbering.xml to be the default one.

A couple of experiments that would be worth doing

1) Insert two of every list type into a document and see if the numbering.xml file is identical to the case where a document contains only one of each list type.

2) Each of the 3 list types allows you to choose a different numbering format (see the tool-tip box in the screenshot).
How does this effect the numbering.xml file if at all.

I am not sure what tool you are using to compare the internal structure of documents but I use BeyondCompare.


Developer
Aug 15, 2012 at 3:10 PM

We have added support for sections and lists in DocX - please see below:

Sections:

We have aded basic support is for adding section breaks (continuous). Add paragraphs and then follow it up by a section break.  The GetSections() method returns a list of Sections in a document.

Example 1: Insert two paragraphs, insert a section, and then insert another paragraph which is contained by a default section (every word document has one default section)

 

document.InsertParagraph("First Para");
document.InsertParagraph("Second Para");
document.InsertSection();
document.InsertParagraph("This is default para");

List <sections> sections = document.GetSections();
List<Paragraph> paraInSection = sections[0].SectionParagraphs;

Lists:

Paragraphs now also have a IsListItem and ListItemType properties.

The DocX.cs class has an AddList method to add/create a list and an InsertList method to add a list to the document:

  public List AddList(string listText, int level = 0, ListItemType listType = ListItemType.Bulleted, int? startNumber = null, bool trackChanges = false)

and an AddListItem method to add list items to an already existing list:

  public List AddListItem(List list, string listText, int level = 0, ListItemType listType = ListItemType.Numbered, int? startNumber = null, bool trackChanges = false)

 

Example 1: Create a numbered list with a list item, add another list item, and then insert list into document. If the list item being added is not of the same list type, a new list is created with that list item. 

var list = document.AddList("List Text", 0, ListItemType.Numbered);
document.AddListItem(list, "List Text2");
document.InsertList(list);

To get all lists in a document, just call document.Lists;

 

 

 

 

Developer
Aug 20, 2012 at 1:40 PM

Can you provide source code so we can update the code? You can either mail me, give it here or do it any other way ;-)

Developer
Aug 20, 2012 at 2:41 PM
MadBoy wrote:

Can you provide source code so we can update the code? You can either mail me, give it here or do it any other way ;-)

We have been working off a Github respository - you can get the source code from https://github.com/Word-DocX/DocX

Nov 22, 2012 at 6:05 PM
Edited Nov 22, 2012 at 6:51 PM

Could there be an easy way to replace text in a document with a list? i.e. document.ReplaceText("sometext",somelist)

            List<int> EmploymentSummaryLocation = document.FindAll("<EmploymentSummary>");

            var list = document.AddList();
            document.AddListItem(list, "List Text2");
           
            EmploymentSummaryLocation.Reverse();
            foreach (int index in EmploymentSummaryLocation)
            {
                document.InsertList(index, list);
            }

This results in "specified value was out of range" ;(

Feb 26, 2013 at 4:53 PM
Edited Feb 26, 2013 at 4:54 PM
Any update on this? Is List support in the latest release??

Currently when reading a word table cell that contains a numbered list docX throws an error.

Eagerly awaiting a reply :)
Feb 26, 2013 at 5:16 PM
Not the codebase here on codeplex, but the github branch has a version of this which appears to work.
Can't vouch for it's robustness without taking a bit of a closer look, but it should get you started.
Apr 28, 2013 at 9:26 AM
How would I format (font, font size etc) contents of a list with the Github version?
May 25, 2013 at 9:21 PM
Just in case someone is interested,

I have made a PR in https://github.com/Word-DocX/DocX/pull/5 where I find the paragraphs in each section and allow the dev remove/insert them. I have removed the foreign SectionParagraphs property and made use of the inherited Paragraphs one.

The code is modification on top of the unofficial branch, but it should be fairly easy to integrate in this repo too as all the required code has been modified and can be previewed in the File Changes tab.

Feel free to comment on the pr.

Thanks for the lib!