C# iTextSharp页码,但不在第一页和最后一页上

C# iTextSharp页码,但不在第一页和最后一页上,c#,itextsharp,C#,Itextsharp,我试图显示我的PDF页面的页码,但我不想在第一页和最后一页显示,因为它们是封面 我使用以下代码: public class PDFPage : iTextSharp.text.pdf.PdfPageEventHelper { //I create a font object to use within my footer protected iTextSharp.text.Font footer { get { //

我试图显示我的PDF页面的页码,但我不想在第一页和最后一页显示,因为它们是封面

我使用以下代码:

public class PDFPage : iTextSharp.text.pdf.PdfPageEventHelper
{
    //I create a font object to use within my footer
    protected iTextSharp.text.Font footer
    {
        get
        {
            // create a basecolor to use for the footer font, if needed.
            iTextSharp.text.Color grey = new iTextSharp.text.Color(40, 40, 40);
            Font font = FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, grey);
            return font;
        }
    }

    //override the OnPageEnd event handler to add our footer
    public override void OnEndPage(PdfWriter writer, Document doc)
    {
        if (doc.PageNumber > 1)
        {
            //I use a PdfPtable with 2 columns to position my footer where I want it
            PdfPTable footerTbl = new PdfPTable(2);

            //set the width of the table to be the same as the document
            footerTbl.TotalWidth = doc.PageSize.Width;

            //Center the table on the page
            footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;

            //Create a paragraph that contains the footer text
            Paragraph para = new Paragraph(" ", footer);

            //add a carriage return
            para.Add(Environment.NewLine);
            para.Add(" ");

            //create a cell instance to hold the text
            PdfPCell cell = new PdfPCell(para);

            //set cell border to 0
            cell.Border = 0;

            //add some padding to bring away from the edge
            cell.PaddingLeft = 10;

            //add cell to table
            footerTbl.AddCell(cell);

            //create new instance of Paragraph for 2nd cell text
            para = new Paragraph(" " + doc.PageNumber, footer);

            //create new instance of cell to hold the text
            cell = new PdfPCell(para);

            //align the text to the right of the cell
            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            //set border to 0
            cell.Border = 0;

            // add some padding to take away from the edge of the page
            cell.PaddingRight = 10;

            //add the cell to the table
            footerTbl.AddCell(cell);

            //write the rows out to the PDF output stream.
            footerTbl.WriteSelectedRows(0, -1, 0, (doc.BottomMargin + 25), writer.DirectContent);
        }
    }

}
谢谢大家!

writer.PageNumber-1)是最后一页。所以也检查一下

公共类PageEventHelper:PdfPageEventHelper
{
PdfContentByte cb;
PdfTemplate模板;
私有整数TotalNumber=0;
//请记住最后一页
公共无效集合编号(int num)
{
TotalNumber=num;
}
公共覆盖无效OnPendDocument(PdfWriter编写器,文档)
{
cb=writer.DirectContent;
模板=cb.CreateTemplate(50,50);
}
公共覆盖无效OnEndPage(PdfWriter编写器、文档)
{
base.OnEndPage(编写者、文档);
cb=writer.DirectContent;
模板=cb.CreateTemplate(50,50);
BaseFont=BaseFont.CreateFont();
int pageN=writer.PageNumber;
字符串text=pageN.ToString();
float len=font.GetWidthPoint(文本,9);
iTextSharp.text.Rectangle pageSize=document.pageSize;
//cb.SetRGBColorFill(100100100);
;
cb.BeginText();
cb.SetFontAndSize(字体,9);
cb.SetTextMatrix(document.LeftMargin,pageSize.GetBottom(document.BottomMargin));
//cb.ShowText(文本);
如果(pageN>1&&TotalNumber==0)
{
cb.ShowTextAligned(Element.ALIGN_CENTER,(pageN-6).ToString(),300f,10f,0);
}
cb.EndText();
cb.AddTemplate(模板,document.LeftMargin+len,pageSize.GetBottom(document.BottomMargin));
}

}
您实际上还没有问任何问题。您的问题是您不知道如何使用此代码吗?还是这段代码不适合你?请给我们更多的细节。嗨,克里斯,代码工作起来很有魅力,但我的问题是我不知道如何隐藏我的PDF最后一页上的页码。哦,听起来很棒。我将尝试一下并报告结果。非常感谢。