wordpress hit counter
Re: XML paragraph code to an existing word docx file using open xml sdk 2.0 - .Net - Development Tools - OpenXML Developer

Re: XML paragraph code to an existing word docx file using open xml sdk 2.0

Development Tools

Discussions about working with Open XML using a wide range of development tools

XML paragraph code to an existing word docx file using open xml sdk 2.0

  • rated by 0 users
  • This post has 1 Reply |
  • 1 Follower
  • How to add a XML paragraph code to an existing word docx file using open xml sdk 2.0 in .net

    <w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:pPr>
            <w:jc w:val="left" />
        </w:pPr>
        <w:r>
            <w:rPr>
                <w:color w:val="auto" />
                <w:b />
                <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial" />
                <w:sz w:val="28" />
            </w:rPr>
            <w:t>Hello World</w:t>
        </w:r>

    </w:p>


    can it be done using AddCustomXmlPart or AddNewPart<CustomXmlPart>()

    Regards

  • This code should do what you're wanting:

    /// <summary>
    /// Add a paragraph with the given <param name="text"/> to the <param name="docName"/>
    /// </summary>
    /// <param name="docName">Name of the document.</param>
    /// <param name="text">The text to insert into the document.</param>
    public static void AddParagraphWordprocessingDocumentWithText(string docName, string text)
    {
        // Open the Wordprocessing document.
        using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(docName, true))
        {
            Text docText = new Text(text);

            Run run = new Run(docText);

            Paragraph paragraph = new Paragraph(run);

            wordprocessingDocument.MainDocumentPart.Document.Body.Append(paragraph);

            // Save changes to the main document part.
            wordprocessingDocument.MainDocumentPart.Document.Save();
        }
    }
Page 1 of 1 (2 items)