I'm generating a PDF I'm trying to put in a footer in my PDF pages. Part of it is stating Page numbers , like "Page 2 of 4". I'm trying to get the total number of pages in the file. To get the current page, it's just:
doc.PageNumber
So I've attepmted to use PdfReader to accomplish this.
private int GetNumOfPDFpages(string fileName)
{
PdfReader pdfReader = new PdfReader(fileName); //Page Header Signature not found
int numOfPages = pdfReader.NumberOfPages;
return numOfPages;
}
Here's a snippet from the code that calls this function:
private void ExportToPDF()
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "PDF Files|*.pdf";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string fileName = saveFileDialog.FileName;
int numOfPages = GetNumOfPDFpages(fileName); //Calls my first function
string directory = Path.GetDirectoryName(fileName);
string file = Path.GetFileName(fileName);
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(Path.Combine(directory, file), FileMode.Create));
wri.PageEvent = new ArdApp.Data.SignalPrototype.PDFattachmentHeader(signal, fileName,numOfPages);
doc.Open();
I found a test function here to verify.
private bool IsValidPdf(string filepath)
{
bool Ret = true;
PdfReader reader = null;
try
{
reader = new PdfReader(filepath);
}
catch
{
Ret = false;
}
return Ret;
}
And sure enough, the return value is false; I'm not converting from Word or using HTML. The file length = 39;
One solution I found was here for digitally signing pdf's , but I don't think that's what I'm looking for. Users just click "Export To PDF", names it, the pdf gets generated using data, and saves the file. What should I do or where should I look? Thanks.