wordpress hit counter
How to properly Insert (append) numbered paragraphs into document body. - WordprocessingML - Formats - OpenXML Developer

How to properly Insert (append) numbered paragraphs into document body.

Formats

Discussions about working with different Open XML Formats

How to properly Insert (append) numbered paragraphs into document body.

  • rated by 0 users
  • This post has 4 Replies |
  • 0 Followers
  • I have a docx, and somewhere in the center of that document there's one numbered paragraph, here's how it looks in Word:

        some text & stuff

        I. First one

        some text & stuff

    I want to add many more numbered paragraphs, that follow one another, so that the final document should look like this:

        some text & stuff

        I.    First one
        II.   Second one   
        III:  Third one  
       
        some text & stuff

    So i found out that in my document.xml, the first numbered paragraph element (I. First one) ends at positon 9. so i'm inserting new paragraphs there, using method InsertAt(paragraph, index).
    I know there has to be a much better way to do this, but i just can't find a way to do it.
    I would like to somehow find out the position of the first numbered paragraph element in document.xml and from there just append the rest of them.

    Thanks a lot in advance!

    Code so far:

            public void CreateNextNumberedPar(string path, string paragraph_text)
            {

                using (WordprocessingDocument doc = WordprocessingDocument.Open(path, true))
                {
                    Paragraph par = new Paragraph(new ParagraphProperties(
                            new NumberingProperties(
                                new NumberingLevelReference() { Val = 0 },
                                new NumberingId() { Val = 7 }),
                            new Justification() { Val = JustificationValues.Both },
                            new ParagraphMarkRunProperties(
                                new FontSize() { Val = (UInt32Value)20U },
                                new Languages() { Val = "pl-PL" })),
                        new Run(
                            new RunProperties(
                                new FontSize() { Val = (UInt32Value)20U },
                                new Languages() { Val = "pl-PL" }),
                            new Text(paragraph_text)));                        
           
                doc.MainDocumentPart.Document.Body.InsertAt(par, 9);

                    doc.MainDocumentPart.Document.Save();

                }
    }

  • Hi there,

    Sorry for late response. Have you figured out the solution?

    I wrote some simple code here:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml;

    namespace AppendNumberedParagraphs
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (WordprocessingDocument document = WordprocessingDocument.Open("test.docx", true))
                {
                    Paragraph p = document.MainDocumentPart.Document.Body.Elements<Paragraph>().Last();

                    // Create a new list paragraph,
                    Paragraph newParagraph = new Paragraph(
                        new ParagraphProperties(
                            new ParagraphStyleId() { Val = "ListParagraph" },
                            new NumberingProperties(
                                new NumberingLevelReference() { Val = 0 },
                                new NumberingId() { Val = 1 })),
                        new Run(
                            new Text("append text")));               

                    // Insert the new pargraph at the end of the document.
                    document.MainDocumentPart.Document.Body.InsertAfter<Paragraph>(newParagraph, p);

                    // Save changes.
                    document.MainDocumentPart.Document.Save();
                }
            }
        }
    }

    I'm not very clear why you want to get the index. In my code, you just need to get the last paragraph, then append the new paragraph after it.

    Hope it helps.

  • Thank you for your reply Goodol.

    I wasn't clear enough about what i want to achieve. the problem is, that i have Word document structured like this:
    --
    some Paragraphs..
        1. Numbered Paragraph
        2. Numbered Paragraph
    some more Paragraphs..
    --


    Your code appends the new paragraph after the last Paragraph in the document, but it does not append it after the last Numbered Paragraph. Your code does the following:

    --
    some Paragraphs..
        1. Numbered Paragraph
        2. Numbered Paragraph
    some more Paragraphs..
        3. Numbered Paragraph    
    --


    Here's the case i want to achieve :
    --
    some Paragraphs..
        1. Numbered Paragraph
        2. Numbered Paragraph
        3. Numbered Paragraph
    some more Paragraphs..
    --

    I'm wondering, maybe i should first get the last Numbered Paragraph element by its ParagraphStyleName Which is "ListParagraph" and then insert a new Paragraph after that element.

    But what if there are a couple more elements in the document with the same value ("ListParagraph") and with different numbering styles, in a single document, for example:

        1. Numbered Paragraph
        2. Numbered Paragraph
        3. Numbered Paragraph

        Some Paragraphs..
           
        I.   Numbered Paragraph
        II.  Numbered Paragraph
        III. Numbered Paragraph


    The difference between these two kinds of Numbered Paragraph Lists is only in numID. Lets say i want to append new Numbered Paragraph only to the Numbered Paragpraph List Which is numbered with Arabic numerals. Does anyone know how to to that ?

    I'm getting really frustrated about the whole thing because i know that there is a simple soulution out there.

    Thanx again for your time Goodol!

  • Thanks for the feedback. That is very useful.



    taux pret auto - taux pret auto differe selon la rise en compte ... calculent automatiquement le taux pour un prĂȘt automobile donne.
  • I think you can read the attributes of element "w:numPr" to distinguish them.

Page 1 of 1 (5 items)