We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose Word- Loading template- Applying orientation and adding content- causing a page break

Hi Team,


I spent much time in solving this issue, but unable to find solution, so i need small help from your end.

I am using Word template and then for inserting document i am changing page orientation to Landscape.

It is causing page break before inserting text to it.

You can see below sample code and attached word template for reference.


using (MemoryStream documentStream = new MemoryStream(document))
{
ProcessingDocument = new Document(documentStream, new LoadOptions() { LoadFormat = LoadFormat.Docx });
}
ProcessingDocumentBuilder = new DocumentBuilder(ProcessingDocument);

PageSetup pageSetUp = ProcessingDocumentBuilder.CurrentSection.PageSetup;
pageSetUp.Orientation = Orientation.Landscape;
// Insert text here (You can check with simple write also)
ProcessingDocumentBuilder.Writeln(“text”);
Please point me out where I went wrong in code.

Thanks in advance,
Regards,
Nakul

Hello

Thanks for your request. It is expected behavior. If you try doing the same in MS Word you will get exactly the same result.

You can try using the code below, to remove the last page break in a document.

Document document = new Document("C:\\Temp\\InitialPage6.docx");

DocumentBuilder builder = new DocumentBuilder(document);

builder.CurrentSection.PageSetup.Orientation = Orientation.Landscape;

builder.Writeln("text");

NodeCollection runs = document.LastSection.GetChildNodes(NodeType.Run, true);

// Find the runs that contain the last page break characters in this section and remove the character.

for (int i = runs.Count - 1; i >= 0; i--)

{

Run run = (Run)runs[i];

if (run.Text.IndexOf(ControlChar.PageBreakChar) >= 0)

{

run.Text = run.Text.Remove(run.Text.IndexOf(ControlChar.PageBreakChar), 1);

break;

}

}

document.Save("C:\\Temp\\out.docx");

Best regards,