wordpress hit counter
Welcome to OpenXML Developer Sign in | Join | Help

Insert Page Break using C# (WordprocessingDocument)

Last post 02-07-2010, 10:38 PM by laflour. 7 replies.
Sort Posts: Previous Next
  •  03-31-2009, 10:21 AM 4406

    Insert Page Break using C# (WordprocessingDocument)

    Hi to all,

    I just started using WordprocessingDocument. I was wondering how you would insert a Section Break, Page Break into a Wordprocessing Document?

    Any help would be appreciated.

    Thanks
  •  03-31-2009, 11:15 AM 4407 in reply to 4406

    Re: Insert Page Break using C# (WordprocessingDocument)

    I am using the following code to combine documents. I want to be able to add a Section Break at the end of the final document.

    public static void MergeSourceDocument(string sourceFile, string destinationFile, string AltChunkID)
            {
                using (WordprocessingDocument myDoc =
                    WordprocessingDocument.Open(sourceFile, true))
                {
                   
                    string altChunkId = AltChunkID;
                    MainDocumentPart mainPart = myDoc.MainDocumentPart;
                    AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
                        AlternativeFormatImportPartType.WordprocessingML, altChunkId);
                    using (FileStream fileStream = File.Open(destinationFile, FileMode.Open))
                        chunk.FeedData(fileStream);
                    AltChunk altChunk = new AltChunk();
                    altChunk.Id = altChunkId;
                    mainPart.Document
                        .Body
                        .InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
                    mainPart.Document.Save();
                                              
                }
            }

    Thanks to all
  •  04-01-2009, 12:53 AM 4419 in reply to 4407

    Re: Insert Page Break using C# (WordprocessingDocument)

    Hi,

    DocumentReflector is a good learning tool to solve open issues. Insert a section break in your document via UI, then use DocumentReflector to see the C# code of that element.

  •  04-01-2009, 10:51 AM 4428 in reply to 4419

    Re: Insert Page Break using C# (WordprocessingDocument)

    gooddol,

    This is a wonderful tool and worked perfect.

    Thanks for the reply!


  •  04-17-2009, 11:20 AM 4506 in reply to 4407

    Re: Insert Page Break using C# (WordprocessingDocument)

    Did you ever resolve this?
  •  04-20-2009, 12:10 AM 4513 in reply to 4506

    Re: Insert Page Break using C# (WordprocessingDocument)

    Hi Geoff,

    I think the suggestion was to use the Document Reflector to analyze a document generated inside MS Word.

    We're working on an article that looks at how to use the document reflector, but, until then this blog post has some walk throughs.
    http://blogs.msdn.com/alspeirs/archive/2008/12/09/generating-documents-with-c-open-xml-and-the-document-reflector.aspx
  •  04-20-2009, 11:00 AM 4517 in reply to 4513

    Re: Insert Page Break using C# (WordprocessingDocument)

    Thanks.Using the document reflector I have the following code:

    Dim para As Paragraph = New Paragraph(New Run(New Break()))
    mainPart.Document.Body.InsertAfter(para, mainPart.Document.Body.LastChild())

    Which seems to work ok.
  •  02-07-2010, 10:38 PM 8121 in reply to 4517

    Re: Insert Page Break using C# (WordprocessingDocument)

    Seems that it's quite different in OpenXML SDK v.2
    Now you need to use the following one

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

    Where "theTable" is the element where you want to add the page break after.
View as RSS news feed in XML