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

Free Support Forum - aspose.com

How to insert page number while convert from excel to PDF

Hi Team,

Please provide sample code to insert page number in PDF While convert from excel to PDF.

1.Excel sheet contains 4 sheets.We need to convert excel to PDF with page number.But page numbers are not diplaying in PDF.

2. How to insert page number in PDF directly

Used code:

string filename = System.IO.Path.GetTempPath() + Session.SessionID + ".xls";


Workbook wb = new Workbook(filename);

Aspose.Cells.Worksheet worksheet = wb.Worksheets[15];

Aspose.Cells.Worksheet worksheet1 = wb.Worksheets[16];

Aspose.Cells.Worksheet worksheet2 = wb.Worksheets[17];

Aspose.Cells.Worksheet worksheet3 = wb.Worksheets[18];


Workbook workbook = new Workbook();

Aspose.Cells.Worksheet worksheet15 = workbook.Worksheets[0];

worksheet15.Name = "FIN";


Aspose.Cells.Worksheet worksheet16 = workbook.Worksheets[1];

worksheet16.Name = "STATEMENTS";


Aspose.Cells.Worksheet worksheet17 = workbook.Worksheets[2];

worksheet17.Name = " Analysis ";


Aspose.Cells.Worksheet worksheet18 = workbook.Worksheets[3];

worksheet18.Name = "Summary";






worksheet15.PageSetup.Orientation = PageOrientationType.Landscape;

worksheet15.PageSetup.FitToPagesTall = 100;

worksheet15.PageSetup.FitToPagesWide = 1;

worksheet15.PageSetup.PaperSize = PaperSizeType.PaperA4;

worksheet15.PageSetup.PrintQuality = 100;

worksheet15.PageSetup.Zoom = 75;

worksheet15.PageSetup.FirstPageNumber = 1;

worksheet16.PageSetup.Orientation = PageOrientationType.Landscape;

worksheet16.PageSetup.FitToPagesTall = 1;

worksheet16.PageSetup.FitToPagesWide = 100;

worksheet16.PageSetup.PaperSize = PaperSizeType.PaperA4;

worksheet16.PageSetup.PrintQuality = 100;

worksheet16.PageSetup.Zoom = 60;

worksheet17.PageSetup.Orientation = PageOrientationType.Landscape;

worksheet17.PageSetup.FitToPagesTall = 1;

worksheet17.PageSetup.FitToPagesWide = 100;

worksheet17.PageSetup.PaperSize = PaperSizeType.PaperA4;

worksheet17.PageSetup.PrintQuality = 100;

worksheet17.PageSetup.Zoom = 70;

worksheet18.PageSetup.Orientation = PageOrientationType.Landscape;

worksheet18.PageSetup.FitToPagesTall = 1;

worksheet18.PageSetup.FitToPagesWide = 100;

worksheet18.PageSetup.PaperSize = PaperSizeType.PaperA4;

worksheet18.PageSetup.PrintQuality = 100;

worksheet18.PageSetup.Zoom = 100;

workbook.Save(HttpContext.Current.Response, "Report.pdf", Aspose.Cells.ContentDisposition.Attachment, new XlsSaveOptions(Aspose.Cells.SaveFormat.Pdf));

Thanks and Regards



Well, by default, if your Excel sheets have page number as header/footer (that you may see in print preview of MS Excel), these page number would be retained in the output PDF file by Aspose.Cells.

If you need to insert page numbers on each page of a sheet, you need to use PageSetup options (by making use of Headers/Footers command scripts), so the pages numbers could be rendered as they are in the Excel sheets in the output PDF file, see the document for your reference:

Setting Headers and Footers

e.g. (examples of command scripts):

&P Represents current page number
&G Represents picture
&N Represents total number of pages