Free Support Forum - aspose.com

How to enable Page number

Hi,

I am using Aspose.PDF to generate a pdf report. I want to enable page numbering at the bottom right position. Can you please paste some code to do that.

Hi,

Loop through the Section. Get the Section and Add Header Footer and then add page numbers. Like :

foreach (Aspose.Pdf.Section sec in pdf.Sections)

{

//Get a section in the pdf object
Section sec1 = sec;


//Create a HeaderFooter object for the section
HeaderFooter hf = new HeaderFooter(sec1);


//Set the HeaderFooter object to odd and even footers
sec1.OddFooter = sec1.EvenFooter = hf;


//Add a text paragraph containing current page number of total number of pages
hf.Paragraphs.Add(new Text(hf,"page $p of $P"));

 break;

}

Note: In this case only one section should be there because the page numbering is according to the sections. If there are multiple sections then you need to restart the page numbering in all the sections by using the property isPageNumberRestarted.

Thanks.

Hi,

Thanks for prompt reply. I implemented the code and it is working fine. But for first page in footer I want some text insted of page number. And this text should come after a horizontal line. And from next page onwards page numbers should continue. For this I wrote code like this

int i=0;

foreach (Aspose.Pdf.Section sec in objPdf.Sections)
{

sec.PageInfo.Margin.Top = 40;
sec.PageInfo.Margin.Left = 40;
sec.PageInfo.Margin.Right = 40;
sec.PageInfo.Margin.Bottom = 40;
sec.PageInfo.PageWidth = PageSize.A3Width;
sec.PageInfo.PageHeight = PageSize.A3Height;
Aspose.Pdf.HeaderFooter objHF = new Aspose.Pdf.HeaderFooter(sec);
if (i == 0)
{
Aspose.Pdf.Table objFooterTbl = new Aspose.Pdf.Table();
objSec.Paragraphs.Add(objFooterTbl);
objFooterTbl.ColumnWidths = "100%";
objFooterTbl.Border = new BorderInfo((int)(BorderSide.Top), 2F, new Color("Grey"));
Text objHederTxt = new Text("Produced by xxxx");
Row row = objFooterTbl.Rows.Add();
Cell footerCell = row.Cells.Add();
footerCell.Paragraphs.Add(objHederTxt);
footerCell.Alignment = AlignmentType.Center;
objHF.Paragraphs.Add(objFooterTbl);
sec.OddFooter = objHF;
}
else
{
Text objHFT = new Text("Page $p of $P");
objHFT.TextInfo.Alignment = AlignmentType.Right;
objHF.Paragraphs.Add(objHFT);
sec.OddFooter = sec.EvenFooter = objHF;
}
i = 1;
}

With this code text is not displaying at the footer. Can you please guide me.

Hi,

Please consult http://www.aspose.com/Products/Aspose.Pdf/Api/SetPageHeaderandFooter.html . Look under "Advanced Header & Footer".

Thanks.

Hi Ahmeed,

Thanks for your reply. We are facing other problem. We have some articles which will be stored in the database in html format. We are fetching these articles' html string into a string and using BindHTML method for each article to generate a pdf report. Actually we want to separate each article with a horizontal line and should continue next article in the same page if space is available. Problem is whenever we use BindHTML, content is going to next page. Is there any way to accommodate all content contineously after calling BindHTML more than once.

Hi,

After you have added all the html articles you can loop through all the Sections and set the IsNewPage property to false.

Thanks.

Hi Adeel,
You had suggested “Loop through the Sections” for adding page numbers to PDF and it worked fine. But while testing, i found that the page number doesnot come in some pages. I found that the reason is because some sections span multiple pages (as we are binding multiple html contents). How to handle this scenario. Is there a way page number can be added to PDF object instead of associating with sections. There is no way we can make sure that a section spans only one page as the html content we bind might span multiple pages.

Any suggestions/sample code as to how to tackle this will be very helpful.

Regards,
Ramesh

Dear Ramesh,

I can’t understand your problem exactly. Please check if IsPageNumberForDocument is what you want. If it is not please provide an example and elaborate what you want.