简体   繁体   中英

How to get the total amount of pages of a PDF file - iTextSharp, c#

I need help. I've spent last 4 hours trying to get the total amount of pages of my PDF file. I want to have something like "Page X/Y" in my footer. Can someone please tell me what to do with this code?

public class pdfPage : PdfPageEventHelper
{              
    public override void OnEndPage(PdfWriter writer, Document doc)
    {

        iTextSharp.text.Rectangle page = doc.PageSize;
        //PdfPTable EndTable = new PdfPTable(2);
        PdfPTable EndTable = new PdfPTable(2);
        EndTable.DefaultCell.Padding = 2f;
        EndTable.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
        iTextSharp.text.Font smallfont2 = FontFactory.GetFont(FontFactory.HELVETICA, "CP1250", 10);
        PdfPCell stopka1 = new PdfPCell(new Phrase("Left column - not important", smallfont2));
        stopka1.BorderWidthLeft = 0;
        stopka1.BorderWidthBottom = 0;
        stopka1.BorderWidthRight = 0;
        stopka1.HorizontalAlignment = Element.ALIGN_LEFT;
        stopka1.VerticalAlignment = Element.ALIGN_MIDDLE;
        PdfPCell stopka2 = new PdfPCell(new Phrase("Page " + doc.PageNumber + "/", smallfont2));
        stopka2.BorderWidthLeft = 0;
        stopka2.BorderWidthBottom = 0;
        stopka2.BorderWidthRight = 0;
        stopka2.HorizontalAlignment = Element.ALIGN_RIGHT;
        stopka2.VerticalAlignment = Element.ALIGN_MIDDLE;
        EndTable.AddCell(stopka1);
        EndTable.AddCell(stopka2);
        EndTable.TotalWidth = page.Width - doc.LeftMargin - doc.RightMargin;
        EndTable.WriteSelectedRows(0, -1, doc.LeftMargin, EndTable.TotalHeight + doc.BottomMargin - 45, writer.DirectContent);

    }
   } 

EDIT

Ok, I sorted it out. I simply closed the PDF file I was working on then copied it as a temporary file. Then in the method "OnEndPage" I counted pages in this temporary document. Later on, I opened a new document copied everything from this temporary, created an object of the pdfPage class and connected it to writer2.PageEvent. Now it works :)

Please read the documentation . If you don't know where to start looking, check the keywords , more specifically: Page X of Y .

There are two examples here:

  • MovieCountries1 creates the PDF in one go, and adds the total number of page in the onDocumentClose() method, because, as you've already discovered, there is no way you or any type of software knows what the total number of pages will be before the document is closed.
  • TwoPasses creates the PDF in two passes, which is a much more robust solution than what you seem to have chosen as solution.

I'm sure you're tech-savvy enough to understand Java and to translate it to C#, but in case there are other people on SO who aren't, they can find the C# version of these examples here and here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM