Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create page break using OpenXml

I use OpenXml to create Word document with simple text and some tables under this text. How can I force Paragraph with this text to show always on new page? Maybe this paragraph should be some Header but I'm not sure how to do this.

Thanks

like image 350
arek Avatar asked May 08 '10 19:05

arek


People also ask

What is DocumentFormat OpenXML?

The Open XML SDK provides tools for working with Office Word, Excel, and PowerPoint documents. It supports scenarios such as: - High-performance generation of word-processing documents, spreadsheets, and presentations. - Populating content in Word files from an XML data source.

What is a run in OpenXML?

The content of the paragraph is contained in one or more runs (<w:r>). Runs are non-block content; they define regions of text that do not necessarily begin on a new line. Like paragraphs, they are comprised of formatting/property definitions, followed by content.

Does OpenXML require office?

No. You only need to have a reference to the library that provides all the OpenXML functionality. That library is not dependant on Office. OpenXML will only handle the new file formats, not the old DOC and DOT.

How install DocumentFormat OpenXML?

Go to your Solution Explorer > right click on references and then click Manage NuGet Packages . Then search in tab "Online" for DocumentFormat. OpenXml and install it. After you can use DocumentFormat.


2 Answers

You can create a page break within a Run element using the <w:br> element. In raw OpenXML, it would look something like:

<w:p>   <w:r>     <w:br w:type="page" />   </w:r> </w:p> 

If you're using the OpenXml SDK, you can use

new Paragraph(   new Run(     new Break(){ Type = BreakValues.Page })); 

EDIT:

If you just want to specify that a paragraph is the last thing on a page, you can try the <w:lastRenderedPageBreak /> tag.

new Paragraph(    new Run(       new LastRenderedPageBreak(),       new Text("Last text on the page"))); 
like image 60
Adam Sheehan Avatar answered Sep 23 '22 09:09

Adam Sheehan


The PageBreakBefore property accomplishes this. It will insert a page break before your paragraph if Word didn't insert one automatically.

if (myParagraph.ParagraphProperties== null)  {      myParagraph.ParagraphProperties = new ParagraphProperties(); }  myParagraph.ParagraphProperties.PageBreakBefore = new PageBreakBefore(); 

I believe it looks something like this in Open XML:

  <w:p>     <w:pPr>       ...       <w:pageBreakBefore/>          ...     </w:pPr>     ...       </w:p> 
like image 44
Collin K Avatar answered Sep 25 '22 09:09

Collin K