Join
Sign in
Search Options
Search Everything
Search Blog
Home
Blog
Resources
Forums
About Open XML
More ...
Home
»
Blog
»
OpenXML Developer
»
How to Insert a Page Break between Imported Content
How to Insert a Page Break between Imported Content
Blog
Samples, Demos, and Reference Articles
Get this RSS feed
Tags
.NET - C#
.NET (C#
.NET (C#, VB, J#, C++/CLI)
C/C++ - C++
C++/CLI)
DocumentBuilder
DrawingML
J#
Java
Linux/Unix/Solaris
Open Packaging Convention
OpenXML
OpenXML PowerTools
OpenXML SpreadsheetML
OpenXML WordprocessingML
Other
Pages
PowerTools
PresentationML
Scripting Languages
SpreadsheetML
VB
Windows (Win32, VB6, VFP, other)
WordProcessingML
WordprocessingML PowerTools
Blog - Monthly Archive List
Archives
May 2013
(3)
April 2013
(3)
March 2013
(2)
February 2013
(8)
January 2013
(11)
December 2012
(4)
November 2012
(10)
October 2012
(7)
September 2012
(1)
August 2012
(3)
July 2012
(2)
June 2012
(4)
May 2012
(3)
April 2012
(6)
March 2012
(6)
February 2012
(8)
January 2012
(4)
December 2011
(6)
November 2011
(9)
October 2011
(8)
September 2011
(5)
August 2011
(9)
July 2011
(5)
June 2011
(7)
May 2011
(6)
April 2011
(7)
March 2011
(3)
January 2011
(2)
October 2010
(2)
August 2010
(1)
May 2010
(1)
April 2010
(3)
March 2010
(6)
February 2010
(3)
December 2009
(4)
November 2009
(4)
September 2009
(7)
August 2009
(2)
July 2009
(3)
June 2009
(7)
May 2009
(6)
April 2009
(7)
March 2009
(8)
February 2009
(3)
September 2008
(1)
June 2008
(3)
May 2008
(2)
April 2008
(2)
March 2008
(2)
February 2008
(2)
January 2008
(5)
December 2007
(3)
November 2007
(3)
October 2007
(2)
September 2007
(2)
August 2007
(6)
July 2007
(3)
June 2007
(4)
May 2007
(4)
April 2007
(2)
March 2007
(6)
February 2007
(2)
January 2007
(5)
December 2006
(5)
November 2006
(12)
October 2006
(5)
September 2006
(3)
August 2006
(11)
July 2006
(5)
June 2006
(5)
May 2006
(6)
April 2006
(7)
March 2006
(17)
OpenXML Developer
RSS for posts
How to Insert a Page Break between Imported Content
Eric White
Mon, Apr 11 2011 10:24 AM
Comments
2
A question arose in the forums - when importing HTML content using altChunk, can you cause
KeepLines / KeepNext to be set for imported paragraphs
. This is not possible. You can control this when importing a WordprocessingML document using altChunk - you simply go into the imported document and set keepLines (section 17.3.1.14 in IS29500) / keepNext (section 17.3.1.15), and when Word imports that content, the resulting paragraphs have their keepLines / keepNext set per the imported content. However, there is no analogous markup for HTML, so there is no way to set those child elements of the paragraph properties element.
However, it is straightforward to insert a page break between imported chunks of content. The following example sets up a document with two imported chunks, with a page break inserted between them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
class Program
{
static void AppendAltChunk(WordprocessingDocument doc,
string altChunkId, string html)
{
MainDocumentPart mainPart = doc.MainDocumentPart;
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
"application/xhtml+xml", altChunkId);
using (Stream chunkStream = chunk.GetStream(FileMode.Create, FileAccess.Write))
using (StreamWriter stringStream = new StreamWriter(chunkStream))
stringStream.Write(html);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
OpenXmlElement last = doc.MainDocumentPart.Document
.Body
.Elements()
.LastOrDefault(e => e is Paragraph || e is AltChunk);
if (last == null)
doc.MainDocumentPart.Document.Body.InsertAt(altChunk, 0);
else
last.InsertAfterSelf(altChunk);
}
static void AppendPageBreak(WordprocessingDocument myDoc)
{
MainDocumentPart mainPart = myDoc.MainDocumentPart;
OpenXmlElement last = myDoc.MainDocumentPart.Document
.Body
.Elements()
.LastOrDefault(e => e is Paragraph || e is AltChunk);
last.InsertAfterSelf(new Paragraph(
new Run(
new Break() { Type = BreakValues.Page })));
}
static void Main(string[] args)
{
using (WordprocessingDocument doc =
WordprocessingDocument.Open("Test.docx", true))
{
// First, delete all paragraphs in the document, creating a blank document.
var paras = doc.MainDocumentPart
.Document
.Body
.Elements<Paragraph>()
.ToList();
foreach (var p in paras)
p.Remove();
string html1 =
@"<html>
<head/>
<body>
<h1>Html Heading</h1>
<p>On the Insert tab, the galleries include items that are designed to coordinate.</p>
<p>Most controls offer a choice of using the look from the current theme.</p>
</body>
</html>";
string html2 =
@"<html>
<head/>
<body>
<h1>New Heading</h1>
<p>This content is on its own page.</p>
<p>This is the second paragraph.</p>
</body>
</html>";
AppendAltChunk(doc,
"AltChunkId1",
html1);
AppendPageBreak(doc);
AppendAltChunk(doc,
"AltChunkId2",
html2);
}
}
}
2 Comments
Comments
hasan
Mon, Mar 18 2013 9:25 AM
is it sport turkish characters
hasan
Mon, Mar 18 2013 9:30 AM
please HELP
Page 1 of 1 (2 items)