How to create a textbox in word openxml using c#

Viewed 13

I am trying to create a textbox using the openxml nuget for a word document. I can't seem to any resources regarding how to add a textbox to my page

Here is what I have tried

using (MemoryStream memory = new MemoryStream())
            {
                // Create Document
                using (WordprocessingDocument wordDocument =
                WordprocessingDocument.Create(memory, WordprocessingDocumentType.Document, true))
                {
                    // Add a main document part. 
                    MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

                    // Create the document structure and add some text.
                    mainPart.Document = new Document();
                    Body docBody = new Body();

                    TextBox testBox = new TextBox();
                    TextBoxContent textBoxContent = new TextBoxContent();
                    Paragraph paragraph = new Paragraph();
                    Run run = new Run();
                    Text text = new Text("test");
                    run.Append(text);
                    paragraph.Append(run);
                    textBoxContent.Append(paragraph);
                    testBox.Append(textBoxContent);

                    docBody.Append(testBox);

                    mainPart.Document.Append(docBody);
                    wordDocument.Save();
                }

                //Download File
                return new MemoryStream(memory.ToArray());
            }

The shown code gives me corrupted word error. How am i supposed to append the elements for it to work ?

0 Answers
Related