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

Insert numbering or heading

Jul 20, 2009 at 7:01 AM

Hello,

My name's Josh. I'm a new DocX user and I'm wondering if I can create a docx with numbering and / or set paragraph's Style.

For example I would like to be able to generate:

1. First part
    1.1  firts subpart
          1.1.1 first sub sub part

               paragraph description lalala
          1.1.2 second sub sub part
              etc...
    1.2 first

I saw on your documentation I can set formatting to a paragraph but I don't see anyway to add specific Style to a paragraph like Heading 1, Heading 2.

If I can specifie a style to a paragraph I will be able to set my Word Style to the Title, headings ...

--
Best regards,
Josh

 

Coordinator
Jul 20, 2009 at 12:43 PM
Hello Josh,
 
sorry it took me so long to reply. I was away all weekend exploring a section of Ireland. Please find the answer to your question below.
 
>> I'm a new DocX user and I'm wondering if I can create a docx with numbering and / or set paragraph's Style.
I think you are refering to a tabbed list? I have not added this feature to DocX yet but it is next on my list. If I could only find the time, I would love to add this feature.
 
>> I saw on your documentation I can set formatting to a paragraph but I don't see anyway to add specific Style to a paragraph like Heading 1, Heading 2.
I have not yet add any of the preset styles to DocX such as Heading1, Heading2 etc. As a work around, you could create a Formatting called Heading1 and apply that wherever you need it (See below for an example).
 
// Recreate the built in style Heading1
Formatting heading1= new Formatting();
heading1.Size = 14;
heading1.Bold = true;
heading1.FontFamily = new FontFamily("Cambria");
heading1.FontColor = System.Drawing.Color.FromArgb(54, 95, 145);
heading1.Color = new Color(54 95 145);
// Insert a new Paragraph at the end of this document with the 'style' heading1.
document.InsertParagraph("New text", false, heading1);

I hope this helps,
kind regards
Cathal
 
Nov 19, 2009 at 4:57 PM

Hi Cathal,

First of all, thank you very much for this excellent library!

I know that you are very busy, but I really (and urgently) need to be able to manage predefined styles (and assign them to paragraphs) and numbering into my .docx documents.

Can you give some hint about how to modify Formatting.cs class? I studied Open XML documentation, but the best I was able to do was to list the custom styles of a document as follows:

(...)

        static XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
        static XNamespace rels = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
        static XNamespace a = "http://schemas.openxmlformats.org/drawingml/2006/main";
        static XNamespace wp = "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing";

        static XName w_r = w + "r";
        static XName w_ins = w + "ins";
        static XName w_link = w + "hyperlink";
        static XName w_pPr = w + "pPr";
        static XName w_p = w + "p";
        static XName w_pStyle = w + "pStyle";
        static XName w_body = w + "body";
        static XName w_style = w + "style";
        static XName w_type = w + "type";
        static XName w_default = w + "default";
        static XName w_styleId = w + "styleId";
        static XName w_val = w + "val";
        static XName w_rPr = w + "rPr";
        static XName w_rStyle = w + "rStyle";
        static XName w_drawing = w + "drawing";
        static XName w_t = w + "t";
        static XName w_br = w + "br";
        static XName w_name = w + "name";
        static XName w_ilvl = w + "ilvl";
        static XName w_numId = w + "numId";
        static XName w_numPr = w + "numPr";
        static XName w_tr = w + "tr";
        static XName w_tc = w + "tc";
        static XName w_tbl = w + "tbl";
        static XName rels_id = rels + "id";
        static XName rels_embed = rels + "embed";
        static XName a_blip = a + "blip";
        static XName wp_extent = wp + "extent";

(...)        

        private void GetDocumentStyles(DocX document)
        {
            XDocument xstyle;

            //read DocX stream and transform it in WordprocessingDocument
            MemoryStream ms = new MemoryStream();
            document.SaveAs(ms);
            WordprocessingDocument wdoc = WordprocessingDocument.Open(ms, true);

            using (StreamReader sr = new StreamReader(wdoc.MainDocumentPart.StyleDefinitionsPart.GetStream()))
            {
                xstyle = XDocument.Load(sr);

                var styles = from style in xstyle
                                 .Root
                                 .Descendants(w_style)
                             let pPr = style
                                 .Elements(w_pPr)
                                 .FirstOrDefault()
                             let rPr = style
                                 .Elements(w_rPr)
                                 .FirstOrDefault()
                             select new
                             {
                                 pStyleName = style.Attribute(w_styleId).Value,
                                 pName = style.Element(w_name).Attribute(w_val).Value,
                                 pPStyle = pPr,
                                 pRStyle = rPr
                             };

                foreach (var style in styles)
                {
                //    Do something
                }
            }
        }

Unfortunately I need more attributes and methods for assign style to Paragraphs, and I don't know how to do it...

Thank you very much!

Best Regards

ZioZione

 

 

 

 

 

 

 

 

 

 

 

 

Jan 13, 2011 at 6:47 AM

Goodmorning Cathal,

Compliments first, and well deserved.

Could you give an estimate on your plans to incorporate named styles in the library. It would be a great addition to the functionallity already available.

We are setting up a project requiring document generation and are now looking at DocX and another (commercial) product. In our proof of concept we tried both and found the headings and numbered styles to be the delta that are the tipping point.

We could of course make the document apear to have headings, but flexibility in styling for our end users will probably have priority in making the choice.

What would you need from your followers to make this a priority?

Kind regards

Marcel

Coordinator
Jan 13, 2011 at 10:50 AM
Marcel,

can you elaborate on named styles? How do you invasion this working, something like the below?

Paragraph p = document.Paragraphs[0];
p.append("Title", NamedStyle.Heading3)
p.append(": DocX is an ", NamedStyle.Heading2)
p.append("opensource library.", NamedStyle.Heading1)

Kind regards,
Cathal
Jan 13, 2011 at 2:20 PM

Good afternoon Cathal,

The concept code you sent would be great.

Apart from setting the styling through code, this would allow a document to be opened and simply applying the ‘customer defined’ styling from the Word file.

So yes please, if this is possible.

Your other mail you mentioned the total donations. I think that it is worth more than the 100 euros yearly.

Are you at least interested in making this into a business? We could possibly help you in commercializing the ideas!

Kind regards

Marcel

From: coffeycathal [email removed]
Sent: donderdag 13 januari 2011 11:51
To: marcel.vandersluys@ziggo.nl
Subject: Re: Insert numbering or heading [DocX:62926]

From: coffeycathal

Marcel,

can you elaborate on named styles? How do you invasion this working, something like the below?

Paragraph p = document.Paragraphs[0];

p.append("Title", NamedStyle.Heading3)

p.append(": DocX is an ", NamedStyle.Heading2)

p.append("opensource library.", NamedStyle.Heading1)

Kind regards,

Cathal

Feb 11, 2011 at 9:02 PM

Hi Cathal,

Great library - thanks for sharing!
In your example given above to recreate a certain style, can you also show me how to add spacing before or after a paragraph? E.g. Heading 1 often uses 12pt before and 3pt after a paragraph. The paragraph.Spacing method seems to do something else.
Thanks,
Erik