1

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.

Community
  • 1
  • 1
Josh M
  • 173
  • 5
  • 17
  • I'm confused. You're showing a Save File dialog and then passing the result of that file into a function asking how many pages are in that file. Are you saving over an existing file each time with no option for creating a new file? If so, then you'll need to post one of the PDFs here for use to inspect. The error message is pretty clear, iText doesn't think that what you are giving it is a valid PDF. – Chris Haas Sep 25 '15 at 13:17
  • Yes, I'm saving a file. I'm passing it into a short function to use PdfReader to get the number of pages in that file. I'm using the resulting number to pass into my class function "OnEndPage()", which then is displayed in my footer since that's where my footer information is. I know it's named "PDFattachmentHeader" but I plan on renaming it since it includes both the header and fotoer. Anyway, the user saves the file, and it can be overwritten if the file name's the same. – Josh M Sep 25 '15 at 13:26
  • The PDF information (the body, footer, and header) shows up pretty well except for some alignment issues. So data does show. – Josh M Sep 25 '15 at 13:30
  • 1
    **Advice 1:** Please throw away your code. Study the documentation. Try anew. **Reason:** Your code is all wrong and your question is very confusing. If you don't like advice 1, consider: **Advice 2:** Throw away your question and rephrase it, explaining in plain words (as simple as possible) what you're trying to achieve. – Bruno Lowagie Sep 25 '15 at 14:50
  • You claim: *Anyway, the user saves the file, and it can be overwritten if the file name's the same.* That is wrong. See my answer to the question [How to update a PDF without creating a new PDF?](http://stackoverflow.com/questions/16081831/how-to-update-a-pdf-without-creating-a-new-pdf) to find out why you are wrong. – Bruno Lowagie Sep 25 '15 at 14:56
  • You claim: *The file length is 39.* It is impossible to create a PDF file that contains only 39 bytes, hence the file you are examining is not a PDF file. – Bruno Lowagie Sep 25 '15 at 14:58
  • You claim that you are using the `OnEndPage()` method to add page numbers to an existing PDF. That is wrong: `OnEndPage()` is a page event method. Page events only make sense when creating a new PDF document from scratch. You should never use it when dealing with existing PDFs. Adding page numbers to existing PDFs is done with `PdfStamper`. – Bruno Lowagie Sep 25 '15 at 15:00
  • I could go on adding comments explaining why your question should be deleted and rephrased, but I hope you already get the idea after reading the last three comments. – Bruno Lowagie Sep 25 '15 at 15:01
  • @BrunoLowagie When I debugged on the "int numOfPages = GetNumOfPDFpages(fileName);" and did "fileName.Length in the Immediate Window, it gave me 39. I don't know why. And I am building a PDF from line 1 to the last line with each export, so I'm not sure if that's considered "updating". In any case, I just found out that I can't use iTextSharp anyway. Thanks for the tips though. – Josh M Sep 25 '15 at 16:11
  • So when you say that the *file length is 39*, do you mean that the number of pages is 39? Doesn't `fileName.Length` give you the number of characters of the file name? I fear that what you're saying isn't really making sense. – Bruno Lowagie Sep 25 '15 at 16:48
  • @BrunoLowagie I just checked that, and yes, file length is the number of characters in the filepath. The only reason I brought up length was because you mentioned about checking if length is 0 when using PdfReader in a related post. I thought it was referring to content, but I didn't actually verify if file.length was related to characters or bits/bytes. I didn't read too carefully. I don't actually use that in my project. – Josh M Sep 25 '15 at 17:03
  • Well there you have it. Checking the length of a file name doesn't tell you anything about the file size, nor about the fact whether or not a file starts with the bytes `%PDF-` which is what `PdfReader` expects (the error you mention is thrown when this is not the case). – Bruno Lowagie Sep 25 '15 at 17:06
  • Since the OP isn't going down this path anymore I vote to close – Chris Haas Sep 25 '15 at 18:14

0 Answers0