Header footer page identification

Hi,


I am using com.aspose.pdf classes.

I would like to know is there is way through which one can identify the first page of PDF or the last page of PDF.

Cause on the first page I have to add 2 headers and on the last page I have to insert 2 footers.

The legacy Aspose had such functionality I guess. Is it still present in com.aspose.pdf classes ?

Hi Saurabh,


Thanks for your inquriy. In com.aspose.pdf you do not have some specific property to identify first or last page of the PDF document but can iterate through the pages as following and can add required header/footer accordingly. However if there is some difference in your requirement and my understanding then please share some more details.

for (int pageCount = 1;
pageCount <= doc.getPages().size(); pageCount++)<o:p></o:p>

{

if (pageCount==1)

{

//header/footer for first page

}

else if (pageCount==doc.getPages().size())

{

//header/footer for last page

}

else

{

//common header/footer

}

}


Best Regards,

Hi,


I tried your approach, But I am missing or doing something wrong.
Can you please look at the following code and correct me.

public static void main(String[] args) {
long start = System.currentTimeMillis();
Document doc = new Document();
Page page = doc.getPages().add();
page.getPageInfo().setHeight( PageSize.getA4().getHeight() );
page.getPageInfo().setWidth( PageSize.getA4().getWidth() );
MarginInfo info = new MarginInfo( 15, 15, 15, 15 );
page.getPageInfo().setMargin( info );

page.getParagraphs().add( getTable() ); // regular table with 200 rows
doc.processParagraphs();
for (int pageCount = 1; pageCount <= doc.getPages().size(); pageCount++)
{
Page page1;
if (pageCount==1)
{
page1 = doc.getPages().get_Item( pageCount );
info = new MarginInfo( 15, 15, 15, 30 );
page1.getPageInfo().setMargin( info );
}
else if (pageCount==doc.getPages().size())
{
page1 = doc.getPages().get_Item( pageCount );
info = new MarginInfo( 15, 30, 15, 15 );
page1.getPageInfo().setMargin( info );
}
else
{
//common header/footer
}
}
doc.save( path );
long end = System.currentTimeMillis();
System.out.println("Generating took "+(end - start));
System.out.println(“Generation complete”);
}

I am increasing the margin because on the first page there are two headers and on the last page there are two footers.

In one of the questions I posted before it was stated to me that the header is rendered in top margin of the page and the footer in the bottom margin.

Once margin is set then I can insert the elements in the header footer.

Hi Saurabh,


Thanks for sharing your sample code. I have tested the scenario and noticed that header/footer contents are overlapped the Page contents. It seems we need to use OnBeforePageGenerate event of Page for the purpose, but currently we are unable to identify the last page of PDF without saving it. So we have logged a ticket PDFNEWJAVA-35656 to identify last page of the PDF document. We will notify you as soon as it is resolved.

We are sorry for the inconvenience caused.

Best Regards,

Hi,


In your previous reply you stated

It seems we need to use OnBeforePageGenerate event of Page for the purpose,

This event is it available ?

If yes can you please send me a link or a example of how to use this event ?


Hi Saurabh,


Thanks for your inquriy. Yes, the OnBeforePageGenerate event is supported and please check following code snippet for the purpose. However as stated above ,I am unable to find option to identify the last page of PDF, while generating a new PDF. We will notify you as soon as above logged issues is resolved.

Document doc = new Document();<o:p></o:p>

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

page.getPageInfo().setHeight( com.aspose.pdf.PageSize.getA4().getHeight() );

page.getPageInfo().setWidth( com.aspose.pdf.PageSize.getA4().getWidth() );

//MarginInfo info = new MarginInfo( 15, 15, 15, 15 );

//page.getPageInfo().setMargin( info );

page.OnBeforePageGenerate.add(new BeforePageGenerate() {

public void invoke(Page page) {

onPageGenerate(page);

}});

com.aspose.pdf.HeaderFooter header1=new com.aspose.pdf.HeaderFooter();

header1.getParagraphs().add(new HtmlFragment("header page first line"));

header1.getParagraphs().add(new HtmlFragment("header page second line"));

com.aspose.pdf.HeaderFooter footer = new com.aspose.pdf.HeaderFooter();

footer.getParagraphs().add(new HtmlFragment("footer last page "));

com.aspose.pdf.HeaderFooter header2=new com.aspose.pdf.HeaderFooter();

header2.getParagraphs().add(new HtmlFragment("header page"));

// Initializes a new instance of the Table

com.aspose.pdf.Table table = new com.aspose.pdf.Table();

table.setColumnWidths( "20% 20% 20% 20% 20%");

// header row 1

Row row = table.getRows().add();

Cell cell=getCell(-1,-1);

for( int i = 0; i < 200; i++ ) {

row = table.getRows().add();

for( int j = 0; j < 5; j++ ){

cell = getCell( i, j );

row.getCells().add(cell);

}

}

// Add table object to first page of input document

page.getParagraphs().add(table);

doc.save(myDir+"testheaderfooter.pdf");

----

public static void onPageGenerate(Page page)

{

if (page.getNumber() == 1)

{

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

MarginInfo info = new MarginInfo( 15, 30, 15, 15 );

page.getPageInfo().setMargin( info );

TextFragment footerText = new TextFragment();

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

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

footerText.getSegments().add(footerSegment);

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

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

}

else

{

MarginInfo info = new MarginInfo( 15, 16, 15, 15 );

page.getPageInfo().setMargin( info );

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

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);

}

}


Best Regards,