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

Free Support Forum - aspose.com

Page Margins dynamic

We have a header that appears in PDF but not sure how many lines the header will have.

So we need to set the page margin in such a way that the header text does not overlap.

is this possible ?

Hi there,


Thanks for your inquiry. Please check following code snippet to add Header/Footer using BeforePageGenerate delegate, while creating a new PDF document. It will adjust page contents dynamically. If the issue persist then please share your sample code and input/output document, then we will look into it and will guide you accordingly.

Document pdf = new Document();

Page page = pdf.getPages().add();

page.OnBeforePageGenerate.add(new BeforePageGenerate() {

public void invoke(Page page) {

onPageGenerate(page);

}});

TextFragment text = new TextFragment("Aspose.Pdf for Java");

for (int i = 0; i < 400; i++)

{

page.getParagraphs().add(text);

}

pdf.save(myDir+"Headerfooter_DOM.pdf");

-------

public static void onPageGenerate(Page page)

{

if (page.getNumber() == 1)

{

page.setFooter(new com.aspose.pdf.HeaderFooter());

page.getFooter().setMargin (new MarginInfo());

page.getFooter().getMargin().setLeft (50);

page.getFooter().getMargin().setRight (50);

page.getFooter().getMargin().setTop (30);

page.getFooter().getMargin().setBottom (10);

TextFragment footerText = new TextFragment();

TextSegment footerSegment = new TextSegment("First page");

footerSegment.getTextState().setFontSize (12);

footerSegment.getTextState().setHorizontalAlignment (HorizontalAlignment.Center);

footerText.getSegments().add(footerSegment);

footerText.getTextState().setHorizontalAlignment (HorizontalAlignment.Center);

page.getFooter().getParagraphs().add(footerText);

}

else

{

page.setFooter(new com.aspose.pdf.HeaderFooter());

page.getFooter().setMargin (new MarginInfo());

page.getFooter().getMargin().setLeft (50);

page.getFooter().getMargin().setRight (50);

page.getFooter().getMargin().setTop (30);

page.getFooter().getMargin().setBottom (10);

TextFragment footerText = new TextFragment();

TextSegment footerSegment = new TextSegment("Subsequent page $p / $P");

footerSegment.getTextState().setFontSize (12);

footerSegment.getTextState().setHorizontalAlignment (HorizontalAlignment.Center);

footerText.getSegments().add(footerSegment);

footerText.getTextState().setHorizontalAlignment (HorizontalAlignment.Center);

page.getFooter().getParagraphs().add(footerText);

}

}


Please feel free to contact us for any further assistance.

Best Regards,