I'm working with ASP MVC and i use iTextSharp to generate PDF's in my application. But now i have a problem: I printing lists and when exist more than one page, i want to show the page number (ex.: Page 1 to 4
). I found some examples, but i think it is more complexes than i need to do (like exameple).
EDIT: I found this example 2. I can count number of pages, but i cant print the number in pages.
What i did:
public ActionResult downloadListaISCC(DateTime? DataFimFiltro)
{
//Code to generate list to PDF
//My document
Document doc1 = new Document();
doc1.SetPageSize(iTextSharp.text.PageSize.A4);
doc1.SetMargins(0f, 0f, 0f, 0f);
doc1.NewPage();
MemoryStream pdfStream = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(doc1, pdfStream);
//Code to create table
doc1.Add(table); //table list in document
//Follow the example 2 (link)
pdfWriter.CloseStream = true;
doc1.Close();
//E fui seguindo o exemplo do segundo link
string file = "D:/gerarPDFOleotorres/"+ nomeDoc +"";
// add page numbers
Document copyDoc = new Document();
PdfCopy copyPdf = new PdfCopy(copyDoc, new FileStream(file, FileMode.Create));
copyPdf.SetPageSize(PageSize.A4.Rotate());
copyDoc.Open();
// read the initial pdf document
PdfReader reader = new PdfReader(pdfStream.ToArray());
int totalPages = reader.NumberOfPages;
PdfImportedPage copiedPage = null;
iTextSharp.text.pdf.PdfCopy.PageStamp stamper = null;
for (int i = 0; i < totalPages; i++)
{
// get the page and create a stamper for that page
copiedPage = copyPdf.GetImportedPage(reader, (i + 1));
stamper = copyPdf.CreatePageStamp(copiedPage);
// add a page number to the page
ColumnText.ShowTextAligned(stamper.GetUnderContent(), Element.ALIGN_CENTER, new Phrase((i + 1) + "/" + totalPages, fontetexto), 820f, 15, 0);
stamper.AlterContents();
// add the altered page to the new document
copyPdf.AddPage(copiedPage);
}
copyDoc.Close();
reader.Close();
// flush and clear the document from memory
pdfStream.Flush();
pdfStream.Close();
}