在itextsharp c#.net中的页码

本文关键字:net itextsharp | 更新日期: 2023-09-27 18:12:01

我需要在论文的底部显示页码,如" page 1 of 2"。

private void metroButton1_Click(object sender, EventArgs e)
{
    SaveFileDialog savefiledialog1 = new SaveFileDialog();
    savefiledialog1.FileName = (metroComboBox1.Text) + " " + (DateTime.Now.ToLongDateString());
    savefiledialog1.Filter = "PDF Files|*.pdf";
    if (savefiledialog1.ShowDialog() == DialogResult.OK)
    {
        { 
          Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
          PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(savefiledialog1.FileName, FileMode.Create));
          doc.Open();
          iTextSharp.text.Image PNG = iTextSharp.text.Image.GetInstance("TexiconLogo.png");
          PNG.ScaleAbsolute(250, 125);
          PNG.SetAbsolutePosition(175, 660);
          PNG.SpacingAfter = 70f;
          doc.Add(PNG);
          Paragraph para2 = new Paragraph("Printed By:", FontFactory.GetFont("Segoe UI", 9f, iTextSharp.text.Font.ITALIC, BaseColor.BLACK));
          para2.SpacingBefore = 70f;
          para2.SpacingAfter = .50f;
          para2.IndentationLeft = 15f;
          doc.Add(para2);
          Paragraph para4 = new Paragraph(globalVar.name + ", " + globalVar.position);
          para4.SpacingAfter = .50f;
          para4.IndentationLeft = 15f;
          para4.Font.Size = 9;
          doc.Add(para4);
          Paragraph para5 = new Paragraph(DateTime.Now.ToLongDateString());
          para5.SpacingAfter = 10f;
          para5.IndentationLeft = 15f;
          para5.Font.Size = 9;
          doc.Add(para5);
          Paragraph para3 = new Paragraph(metroComboBox1.Text);
          para3.SpacingAfter = 3f;
          para3.IndentationLeft = 200f;
          para3.Font.Size = 13;
          doc.Add(para3);
          PdfPTable table = new PdfPTable(metroGrid1.Columns.Count);
          for (int j = 0; j < metroGrid1.Columns.Count; j++)
          {
              PdfPCell cell = new PdfPCell(new Phrase(metroGrid1.Columns[j].HeaderText, FontFactory.GetFont("Times New Roman", 8f, iTextSharp.text.Font.BOLD, BaseColor.WHITE)));
              cell.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#9DA2A3"));
              cell.HorizontalAlignment = 1;
              table.AddCell(cell);
          }
          table.HeaderRows = 1;
          for (int i = 0; i < metroGrid1.Rows.Count; i++)
          {
              for (int k = 0; k < metroGrid1.Columns.Count; k++)
              {
                  PdfPCell cell2 = new PdfPCell(new Phrase(metroGrid1[k, i].Value.ToString(), FontFactory.GetFont("Times New Roman", 7f, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)));
                  if (i % 2 != 0)
                  {
                      cell2.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#C4C4C4"));
                  }
                  cell2.HorizontalAlignment = 1;
                  table.AddCell(cell2);
              }
          }
          doc.Add(table);
          doc.Close();
          System.Diagnostics.Process.Start(savefiledialog1.FileName);
        }
    }
}

在itextsharp c#.net中的页码

试试我做的这个函数:

    private int last_printed_page;
    public void printPageNumber(int page_number = -1)
    {
        if (page_number == -1)
            page_number = writer.CurrentPageNumber;
        if (last_printed_page != numero)
        {
            PdfContentByte cb = writer.DirectContent;
            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            cb.BeginText();
            cb.SetFontAndSize(bf, 10);
            cb.ShowTextAligned((int)Doc.RightMargin, "" + numero, Doc.PageSize.Width - Doc.RightMargin - 5, 20, 0);
            cb.EndText();
            last_printed_page = page_number;
        }
    }