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

Free Support Forum - aspose.com

Different margins for pages following the first page

Hey,
I am converting a HTML file to PDF. Almost everything works fine so far, but I am at a point, where I don’t know how to proceed. Is there an option to set different margins on pages following the first page? So far I am using the PageSetup methods like setTopMargin to set the margin for the entire document.

Hi there,

Thanks for your inquiry. The PageSetup class represents the page setup properties of a section. In your case, we suggest you following solution. Hope this helps you.

1) Please iterate through all nodes of document and get the last node of first page. You can get the page number of a node by using LayoutCollector.getStartPageIndex method.
2) Please move the cursor to the last node of first page and insert section break continuous using DocumentBuilder.insertBreak method.
3) After inserting section break, your document will have two sections.
4) Please use properties of PageSetup class according to your requirement.

If you face any issue, please share your HTML and output document here for our reference. We will then provide you more information about your query along with code.

Hey,
Thanks for your reply. I am a little bit lost on how I go about iterating through all the nodes in the document to get the last node of the first page. Could you please provide some sample code snippet for me to work with? My html file is fairly basic and includes ul/li, div and span tags, if that is relevant for this step.

Hi there,

Thanks for your inquiry. Please check the following code example. We have attached the input and output documents with this post for your kind reference. Hope this helps you.

Document doc = new Document(MyDir + "input.html");
DocumentBuilder builder = new DocumentBuilder(doc);
LayoutCollector collector = new LayoutCollector(doc);

int pageIndex = 1;

NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for(Paragraph para : paragraphs)
{
if (collector.getStartPageIndex(para) == 2)
{
//Move the cursor to the previous node of paragraph
//that is last node on first page of document
builder.moveTo(para.getPreviousSibling());
builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
PageSetup pageSetup = builder.getCurrentSection().getPageSetup();
pageSetup.setOrientation(Orientation.LANDSCAPE);
break;
}
}
doc.save(MyDir + "output.docx");

Hey, sorry for the late response.

I managed to get this to work, setting up different margins for pages following the first one. However, the changes do not display when converting to PDF. It works fine with .doc for example.

Hi there,

Thanks for your inquiry. Please call Document.UpdatePageLayout method before saving the document to PDF. Hope this helps you.