How can I add break at 4th and 2nd page?

I want to insert break on second page and fourth page.

I want to replace below code with aspose code.

selection.GoTo(What: WdGoToItem.wdGoToPage, Which: WdGoToDirection.wdGoToNext, Type.Missing, 2);
selection.HomeKey(Unit: WdUnits.wdStory, Type.Missing);
selection.GoTo(What: WdGoToItem.wdGoToPage, Which: WdGoToDirection.wdGoToNext, Type.Missing, 4);
selection.HomeKey(Unit: WdUnits.wdStory, Type.Missing);

How can I achive this in aspose?

@Dhanashri0709 There is no method to move Aspose.Words’ DocumentBuilder cursor to the end of a particular page. However, you can detect page index where a particular node is placed using LayoutCollector class.

Could you please attach your input and expected output documents here for our reference? We will check the documents and provide you an alternative solution.

Thank you so much for your reply :slight_smile:

Check attached file input_docx and expected_output.
I am expecting blank page on second and fourth page.
Expected_docx.docx (15.4 KB)
Input_docx.docx (15.4 KB)

@Dhanashri0709 Thank you for additional information. It looks like your requirement is to start Book 2: and Book 2: chapters from odd page. Is it right? If so in your case you can use Section Break Odd Page instead of simple page break. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert page breaks before the "Book 2:" and "Book 3:" paragraphs.
List<Paragraph> targetParagraphs = doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>()
    .Where(p => p.ToString(SaveFormat.Text).Trim() == "Book 2:" || p.ToString(SaveFormat.Text).Trim() == "Book 3:").ToList();

foreach (Paragraph p in targetParagraphs)
    // Move document builder to the paragraph and insert a break.


I have tried with LayoutCollector but node can be anything and it is not confirmed what type it would be. Is there any other way to detect the page index or can we identify the last node of particular page (example: page no 3).

Document can be any word file. I have just given example. I am unaware of what content is going to come in the word file

@Dhanashri0709 The problem is that MS Word documents are flow document and do not have a concept of page. So nodes can span multiple pages. In your case, you can try using Document.ExtractPages method to split the document into parts and then concatenate the part back to gather using Document.AppendDocument method. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

// Get the first two pages.
Document doc1 = doc.ExtractPages(0, 2);

// Get pages from 3rd to 4th
Document doc2 = doc.ExtractPages(2, 2);

// Get the remaining pages.
Document doc3 = doc.ExtractPages(4, doc.PageCount - 4);

// Concatenate the documents back. Add page break at the beginning of the 2nd and 3rd part.
DocumentBuilder builder = new DocumentBuilder(doc2);
builder = new DocumentBuilder(doc3);

doc1.AppendDocument(doc2, ImportFormatMode.UseDestinationStyles);
doc1.AppendDocument(doc3, ImportFormatMode.UseDestinationStyles);


@alexey.noskov Thank you so much it works for me :slight_smile:

I have one more query. I have created my project in .net framework and trying to access System.Drawing.Printing.PrinterSettings but I am getting error for System.Drawing.Printing as I cannot see printing reference in System.Common dll

How can I access PrinterSettings while using below code?

Document doc = new Document(@"C:\Temp\in.docx");
doc.Print(); // Here I want to use second overload method of print

@Dhanashri0709 in .NET Framework you do not need to add reference to System.Drawing.Common. You can simply add reference to System.Drawing:

It works :slight_smile: Thank You so much.

1 Like