Free Support Forum - aspose.com

How to insert a pagebreak without using the DocumentBuilder?

I started to build a word document using the Document object. I’m almost finnished
and everything seems to be working except inserting page breaks.
When I searched for how I could insert them I found out that the DocumentBuilder had a method for it.
I have not used the DocumentBuilder in any of my code so far and if I try to use it all my pagebreaks will be put at the top of the DocumentBuilder? Is this because I have used the Document object everywhere else?



Cheers,
Franke

Hello!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for your inquiry.

You can achieve the same effect by adding a run of text with ControlChar.PageBreak to the document:

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/aspose.words.controlchar.pagebreak.html

There is no difference whether you are using DocumentBuilder or doing the same without it while you’re doing everything correctly. DocumentBuilder is a helper class that simplifies operations on Document. There is no reason to start using it at this point where you are almost finished but need a page break.

Regards,

Thanks a lot for your fast reply!
I will give it a try. I have one more question. Why does the pagebreaks get first in the document when I’m trying to use the DocumentBuilder? Is it because I have used Document to create the rest of the document?

Hi again.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Without looking your code I can suspect that your DocumentBuilder is improperly positioned. It has an internal cursor that is set by MoveToXXX methods. Recommendations depend on where you need a page break. If you’d like to put it at the end of some paragraph just pass it to this simple method:

builder.MoveTo(paragraph);

builder.InsertBreak(ControlChar.PageBreak);

As I wrote there is no matter whether you construct a document with or without builder. Document doesn’t use any backlinks and cross-calls.

Regards,

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. This occurs because when you create DocumentBuilder the cursor is placed at the beginning of document. You can try using the following code:

//Here is your code that you are using fo document generation

//Then you create DocumentBuilder

DocumentBuilder builder = new DocumentBuilder(doc);

//Move cursor to the end of document

builder.MoveToDocumentEnd();

//And insert page break

builder.InsertBreak(BreakType.PageBreak);

I hope this could help you.

Best regards.

Thanks a lot!
Now I understand more how I should work with the DocumentBuilder if I need to do something more. Maybe change some things.
Right now I created a Run with the ControlChar.PageBreak constant and it worked out very well!