How to add a section to the next page using openxml?

I want to add a section break at the end of the document and add text.

My code is as follows:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; namespace WordDocManipulation { class Program { static void Main(string[] args) { string path = @"C:\sample.docx"; string strtxt = "Hello This is done by programmatically"; OpenAndAddTextToWordDocument(path,strtxt); } public static void OpenAndAddTextToWordDocument(string filepath, string txt) { /* I want to the below text to be added in the new section */ // Open a WordprocessingDocument for editing using the filepath. WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true); // Assign a reference to the existing document body. Body body = wordprocessingDocument.MainDocumentPart.Document.Body; // Add new text. Paragraph para = body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); run.AppendChild(new Text(txt)); // Close the handle explicitly. wordprocessingDocument.Close(); } } } 

What should I do?

+6
source share
3 answers

You need to add a section break to the section properties. Then you need to add section properties to paragraph properties. Then add paragraph properties to the paragraph.

  Paragraph paragraph232 = new Paragraph(); ParagraphProperties paragraphProperties220 = new ParagraphProperties(); SectionProperties sectionProperties1 = new SectionProperties(); SectionType sectionType1 = new SectionType(){ Val = SectionMarkValues.NextPage }; sectionProperties1.Append(sectionType1); paragraphProperties220.Append(sectionProperties1); paragraph232.Append(paragraphProperties220); 

As a result of Open XML:

  <w:p> <w:pPr> <w:sectPr> <w:type w:val="nextPage" /> </w:sectPr> </w:pPr> </w:p> 

If you create a Word document that looks the way you want the result to appear, open it in the Open XML Productivity Tool , you can display the code and see which C # code will generate the various Open XML elements that you are trying to achieve.

+7
source

First you need to create a break paragraph

 Paragraph PageBreakParagraph = new Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new DocumentFormat.OpenXml.Wordprocessing.Break() { Type = BreakValues.Page })); 

Then you need to add a paragraph

 wordprocessingDocument.MainDocumentPart.Document.Body.Append(PageBreakParagraph) 

You can also specify where to insert it if you do not want to add it to the end using the InsertAfter and InsertBefore methods

 wordprocessingDocument.MainDocumentPart.Document.Body.InsertAfter(PageBreakParagraph, ReferenceElement); wordprocessingDocument.MainDocumentPart.Document.Body.InsertBefore(PageBreakParagraph, ReferenceElement); 

Edit:

This adds a page break rather than a section break.

+1
source

this one will add a section break at the end of the page

 private static void AddSectionBreakToTheDocument(string fileName) { using (WordprocessingDocument mydoc = WordprocessingDocument.Open(fileName, true)) { MainDocumentPart myMainPart = mydoc.MainDocumentPart; Paragraph paragraphSectionBreak = new Paragraph(); ParagraphProperties paragraphSectionBreakProperties = new ParagraphProperties(); SectionProperties SectionBreakProperties = new SectionProperties(); SectionType SectionBreakType = new SectionType() { Val = SectionMarkValues.NextPage }; SectionBreakProperties.Append(SectionBreakType); paragraphSectionBreakProperties.Append(SectionBreakProperties); paragraphSectionBreak.Append(paragraphSectionBreakProperties); myMainPart.Document.Body.InsertAfter(paragraphSectionBreak, myMainPart.Document.Body.LastChild); myMainPart.Document.Save(); } } 
0
source

Source: https://habr.com/ru/post/958286/


All Articles