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

Free Support Forum - aspose.com

Specify page margins when printing

We are using Aspose.Words ver 11.11

How do I specify different page margins that are different for the first page than the rest of the document?

Using the document explorer demo provided from aspose I am loading an html document that will have more than one page when rendered.

In the preview.cs file, I have modified Execute method by attaching to the QueryPageSettings event.

In the event handler looks like:

private static int page = 1;

static void awPrintDoc_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
{
PageSettings settings = (PageSettings)e.PageSettings.Clone();

if (page == 1)
{
settings.Margins = new Margins(100, 100, 500, 100);
}
else
{
settings.Margins = new Margins(100, 100, 100, 100);
}

e.PageSettings = settings;
++page;


} private static int page = 1;

static void awPrintDoc_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
{
PageSettings settings = (PageSettings)e.PageSettings.Clone();

if (page == 1)
{
settings.Margins = new Margins(100, 100, 500, 100);
}
else
{
settings.Margins = new Margins(100, 100, 100, 100);
}

e.PageSettings = settings;
++page;


}

when the preview renders, the margins are ignored.

What is the correct way to specify the margins for the first page that are different from the rest of the document?

Thanks

Hi Tony,


Thanks for your inquiry and sorry for the delayed response.

There is no “Page” concept in Microsoft Word documents. Pages are created by Microsoft Word on the fly and unfortunately there is no built-in method in Aspose.Words that you can use to specify margins for a single Page. May be you can implement the following work flow to achieve this:

  1. Extract the content of first page into an empty Document. (As suggested here, you need to use ‘PageNumberFinder’ class to be able to extract Page’s contents.)
  2. Extract the remaining content into another intermediate Document object.
  3. Join these two Documents into one.
  4. Set the PageSetup properties of the first Section
  5. Set the PageSetup properties of the second Section
  6. Render/Print the final document containing the two Sections with different margins

I hope, this helps.

Best regards,

Where do I find the ‘PageNumberFinder’ class?

If I follow the suggested method, what would happen if the changing the margins of the first section causes that section to render into more than one page?

Would I then have the first 2 pages with the section 1 margins and the rest of the document with the section 2 margins?

Hi Tony,


Thanks for your inquiry.

I have attached the PageFinder class here with this post for your reference. Secondly, you are correct; all pages of the first section will have the same margin settings which are defined via Section. May be you should do something like reduce the text font size, increase the Section height or may be reduce vertical spacing between Paragraphs using ParagraphFormat.SpaceAfter and ParagraphFormat.SpaceBefore properties to be able to fit the content inside the first page. I hope, this helps.

Best regards,

Hi,

I have found the PageNumberFinder class and examined it.

In a previous post you suggest that we could use it to extract pages and provided a link to another post. That post had no information on how to use the PageNumberFinder class to extract pages or to split a document into pages.

I want to be able to:

1) set the section margins to the first page margins

2) set the headers and footers

3) split the document into 2 documents. Document 1 contains first page, document 2 contains remainder of document

4) append document 2 to document 1 (now I should have 2 sections in the resulting document with the first section containing the first page, and the second section containing the remainder).

5) set the margins on the section section.

Done - I should have 1 document setup the way we need it.

Hi Tony,


Thanks for your inquiry. The following code imports content of each page in source document into a separate Section in destination document.

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

// Set up the document which pages will be copied to. Remove the empty section.

Document dstDoc = new Document();

dstDoc.RemoveAllChildren();

PageNumberFinder finder = new PageNumberFinder(doc);

// Split nodes which are found across pages.

finder.SplitNodesAcrossPages(true);

// Copy all content including headers and footers from the specified pages into the destination document.

ArrayList pageSections = finder.RetrieveAllNodesOnPages(1, doc.PageCount, NodeType.Section);

foreach (Section section in pageSections)

dstDoc.AppendChild(dstDoc.ImportNode(section, true));

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


In your case, to be able to keep different margin settings for the first page to those specified for the rest of the pages in your document, you simply need to change margin settings for the first Section in the destination document as follows:

PageSetup firstPs = dstDoc.FirstSection.PageSetup;

firstPs.LeftMargin = 2 * 72;

firstPs.RightMargin = 2 * 72;

PageSetup remainingPs = dstDoc.LastSection.PageSetup;

firstPs.LeftMargin = 3 * 72;

firstPs.RightMargin = 3 * 72;

for (int i = 1; i < dstDoc.Sections.Count; i++)

{

dstDoc.Sections[i].PageSetup.LeftMargin = remainingPs.LeftMargin;

dstDoc.Sections[i].PageSetup.RightMargin = remainingPs.RightMargin;

}


Regarding creating Headers/Footers, I would suggest you please read the following article:
http://www.aspose.com/docs/display/wordsnet/How+to++Create+Headers+Footers+using+DocumentBuilder

I hope, this helps.

Best regards,