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

Free Support Forum - aspose.com

How to set Page size to custom height and width


I want to set different page size in my word document. I want set some predefined height let say 10 cm and width 18 cm for first page and then default page size for other pages. Can you please guide to the correct code. I tried multiple things , but nothing works.



Thanks for your inquiry. Please note there is no page concept in Microsoft Word documents; pages are created by Microsoft Word on the fly and unfortunately there is no direct method you can use to achieve what you need. The PageSetup class represents the page setup properties of a section. PageSetup object contains all the page setup attributes of a section (left margin, bottom margin, paper size, and so on) as properties. You can set different page size to different sections.

You may split your Document in separate pages using DocumentPageSplitter class from PageSplitter project and create a document with different sections, each page as a separate section. Later you can set page size of any desired section as following.

You can find PageSplitter project inAspose.Words for .NET examples repository at GitHub.

Document doc = new Document("Test.doc");
// Create and attach collector to the document before page layout is built.
LayoutCollector layoutCollector = new LayoutCollector(doc);
// This will build layout model and collect necessary information.
// Split nodes in the document into separate pages.
DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);
// Create a new document to add each page as a separate section.
Document document = splitter.GetDocumentOfPage(1);
for (int i = 2; i <= doc.PageCount; i++)
    Aspose.Words.Document dstDoc = splitter.GetDocumentOfPage(i);
    document.AppendDocument(dstDoc, ImportFormatMode.UseDestinationStyles);
//Set PageSetup settings of first section
PageSetup firstpageSetup = document.FirstSection.PageSetup;
firstpageSetup.PageWidth = ConvertUtil.InchToPoint(6);
firstpageSetup.PageHeight = ConvertUtil.InchToPoint(8);