1

我正在尝试制作一个将报价保存在 pdf 文件中的程序。它工作得很好,但我的页脚有问题。我按照这些说明进行操作。

不幸的是,页脚只显示在最后一页。我该如何解决这个问题?我正在寻找一个最简单的解决方案,因为我刚刚开始使用 C#。

这是我的代码:

public override void OnStartPage(PdfWriter writer, Document document)
  {
    base.OnStartPage(writer, document);

    PdfPTable tab = new PdfPTable(1);
    PdfPCell cell = new PdfPCell(new Phrase("Testing..."));
    cell.Border = 0;
    tab.TotalWidth = 300F;
    tab.AddCell(cell);
    tab.WriteSelectedRows(0, -1, 300, 30, writer.DirectContent);
  }
}

private void button1_Click(object sender, EventArgs e) // drukowanie oferty w PDF
{
  if (dataGridView1.Rows.Count > 0)
  {
    Document oferta = new Document(iTextSharp.text.PageSize.A4, 20, 20, 20, 20);
    SaveFileDialog saveFile = new SaveFileDialog();

      saveFile.FileName = textBox7.Text.Trim()+".pdf";
      saveFile.Title = "Zapisz ofertę (PDF)";
      saveFile.Filter = "Adobe Reader|*.pdf|Wszystkie pliki|*.*";

      if (saveFile.ShowDialog() != DialogResult.Cancel)
      {
        try
        {
          PdfWriter wri = PdfWriter.GetInstance(oferta, new FileStream(saveFile.FileName, FileMode.Create));

          oferta.AddAuthor(label1.Text);
          oferta.AddCreator("Creator");
          oferta.AddTitle(sNrOferty);

          oferta.Open();

          PdfContentByte cb = wri.DirectContent;

          iTextSharp.text.Font czcionkaTytul = new iTextSharp.text.Font(iTextSharp.text.FontFactory.GetFont("C:\\Windows\\Fonts\\Verdana.ttf", BaseFont.CP1257, 11.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK));
          iTextSharp.text.Font czcionkaTabela = new iTextSharp.text.Font(iTextSharp.text.FontFactory.GetFont("C:\\Windows\\Fonts\\Verdana.ttf", BaseFont.CP1257, 11.0F, iTextSharp.text.Font.NORMAL, BaseColor.BLACK));
          iTextSharp.text.Font czcionkaNaglowek = new iTextSharp.text.Font(iTextSharp.text.FontFactory.GetFont("C:\\Windows\\Fonts\\Verdana.ttf", BaseFont.CP1257, 11.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK));
          iTextSharp.text.Font czcionkaFirma = new iTextSharp.text.Font(iTextSharp.text.FontFactory.GetFont("C:\\Windows\\Fonts\\Verdana.ttf", BaseFont.CP1257, 11.0F, iTextSharp.text.Font.NORMAL, BaseColor.LIGHT_GRAY));

          // dodawanie logo - działa, nie ruszać :D
          iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Properties.Resources.Biomedica_Logo_Mailsignature_02, System.Drawing.Imaging.ImageFormat.Png);
          logo.SetAbsolutePosition(420, 750);
          logo.ScalePercent(30);
          cb.AddImage(logo);

          // dodawanie tekstów
          cb.BeginText();

          // some text lines are added here
          //...

          //dodawanie danych klienta
          // imie i nazwisko
          cb.SetColorFill(BaseColor.BLACK);
          cb.SetFontAndSize(BaseFont.CreateFont("C:\\Windows\\Fonts\\Verdanab.ttf", BaseFont.CP1257, BaseFont.NOT_EMBEDDED), 11.0F);
          cb.SetTextMatrix(300, 700);
          cb.ShowText(textBox1.Text.Trim());

          // pozycja
          cb.SetFontAndSize(BaseFont.CreateFont("C:\\Windows\\Fonts\\Verdana.ttf", BaseFont.CP1257, BaseFont.NOT_EMBEDDED), 11.0F);

          cb.SetTextMatrix(300, 680);
          cb.ShowText(textBox2.Text.Trim());
          //Organizacja
          cb.SetTextMatrix(300, 660);
          cb.ShowText(textBox3.Text.Trim());
          //adres
          cb.SetTextMatrix(300, 640);
          cb.ShowText(textBox4.Text.Trim());
          //miejscowosc
          cb.SetTextMatrix(300, 620);
          cb.ShowText(textBox5.Text.Trim());

          //nr oferty
          cb.SetFontAndSize(BaseFont.CreateFont("C:\\Windows\\Fonts\\Verdanab.ttf", BaseFont.CP1257, BaseFont.NOT_EMBEDDED), 11.0F);
          cb.ShowTextAligned(1, "Oferta cenowa nr: " + textBox7.Text.Trim(), 300, 580, 0);
          cb.EndText();

          PdfPTable tabela = new PdfPTable(dataGridView1.Columns.Count); //poniżej - definiowanie tabeli i przenoszenie danych z datagridview

          float[] szerokości = new float[] 
    {
      dataGridView1.Columns[0].Width,
      dataGridView1.Columns[1].Width,
      dataGridView1.Columns[2].Width,
      dataGridView1.Columns[3].Width,
      dataGridView1.Columns[4].Width,
      dataGridView1.Columns[5].Width,
      dataGridView1.Columns[6].Width
    };
          tabela.WidthPercentage = 95;
          tabela.SetWidths(szerokości);
          tabela.HorizontalAlignment = 1; // 0 - lewo, 1 - środek , 2 - prawo
          tabela.SpacingBefore = 170.0F;
          tabela.SpacingAfter = 12.0F;
          PdfPCell komorka = null;

          foreach (DataGridViewColumn c in dataGridView1.Columns)
          {
            komorka = new PdfPCell(new Phrase(new Chunk(c.HeaderText, czcionkaNaglowek)));
            komorka.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
            komorka.VerticalAlignment = PdfPCell.ALIGN_CENTER;
            tabela.AddCell(komorka);
          }

          if (dataGridView1.Rows.Count > 0)
          {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
              for (int j = 0; j < dataGridView1.Columns.Count; j++)
              {
                komorka = new PdfPCell(new Phrase(dataGridView1.Rows[i].Cells[j].Value.ToString(), czcionkaTabela));
                komorka.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
                komorka.VerticalAlignment = PdfPCell.ALIGN_CENTER;
                tabela.AddCell(komorka);
              }
            }
          }
          oferta.Add(new Paragraph("Biomedica Poland Sp. z o.o.", czcionkaFirma));
          oferta.Add(new Paragraph("ul. Raszyńska 13", czcionkaFirma));
          oferta.Add(new Paragraph("05-500 Piaseczno", czcionkaFirma));
          oferta.Add(new Paragraph("tel: +48 (22) 737 59 96", czcionkaFirma));
          oferta.Add(new Paragraph("fax: +48 (22) 737 59 94", czcionkaFirma));
          oferta.Add(new Paragraph("www.biomedica.pl", czcionkaFirma));

          oferta.Add(tabela); //dodawanie tabeli 

          oferta.Add(new Paragraph("Oferta ważna do: " + dateTimePicker2.Value.ToLongDateString(), czcionkaTabela));
          oferta.Add(new Paragraph("Proponowany termin dostawy: " + textBox6.Text.Trim() + " dni.", czcionkaTabela));
          oferta.Add(new Paragraph("Termin płatności: " + textBox8.Text.Trim() + " dni.", czcionkaTabela));

          oferta.Add(new Paragraph(30.0f, "Obowiązujące ceny mogą ulec zmianie w przypadku:", czcionkaNaglowek));
          oferta.Add(new Paragraph("   * wzrostu kursów walut powyżej o powyżej 5%", czcionkaNaglowek));
          oferta.Add(new Paragraph("   * zmian stawek cła", czcionkaTabela));
          oferta.Add(new Paragraph("   * lub podatków związanych z obrotem towarowym", czcionkaTabela));

          oferta.Add(new Paragraph(30.0f, "Ofertę wystawił/a:", czcionkaTabela));
          oferta.Add(new Paragraph(label1.Text, czcionkaTabela));
          oferta.Add(new Paragraph(label2.Text, czcionkaTabela));
          oferta.Add(new Paragraph("Tel: " + label3.Text, czcionkaTabela));
          oferta.Add(new Paragraph("e-mail: " + label4.Text, czcionkaTabela));

          wri.PageEvent = new PDFFooter();
          oferta.Close();

        }
        catch (DocumentException dex)
        {
          MessageBox.Show(dex.ToString());
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.ToString());
        }
      }
  }
  else
  {
    MessageBox.Show("Brak produktów w ofercie", "Pusta oferta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  }
}

除了此页脚之外,此代码没有问题。

4

3 回答 3

1

正如我在iText in Action一书中所写,您不应该在OnStartPage方法中添加页脚。使用OnEndPage您参考的文章中描述的方法。如果您想知道原因:请阅读本书。

此外,如书中所述,您需要在打开文档之前设置页面事件:

PdfWriter wri = PdfWriter.GetInstance(oferta, new FileStream(saveFile.FileName, FileMode.Create));
oferta.AddAuthor(label1.Text);
oferta.AddCreator("Creator");
oferta.AddTitle(sNrOferty);
wri.PageEvent = new PDFFooter();
oferta.Open();

每次创建新页面时都会触发页面事件。如果您仅在关闭文档之前添加页面事件,则不会在所有已完成的页面上触发该事件。换句话说:你提到的文章是错误的。

编写 iTextSharp 代码时请使用官方文档。

于 2013-04-08T11:47:35.263 回答
1

在 OnStartPage 中添加页脚不起作用。

这是一个例子:

public override void OnEndPage(PdfWriter writer, Document document)
        {
            base.OnEndPage(writer, document);

            int pageN = writer.PageNumber;
            String text = "Page " + pageN + " of ";
            float len = bf.GetWidthPoint(text, 8);

            Rectangle pageSize = document.PageSize;

            cb.SetRGBColorFill(100, 100, 100);

            cb.BeginText();
            cb.SetFontAndSize(bf, 8);
            cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30));
            cb.ShowText(text);
            cb.EndText();

            cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30));

            cb.BeginText();
            cb.SetFontAndSize(bf, 8);
            cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, 
                "Printed On " + PrintTime.ToString(), 
                pageSize.GetRight(40), 
                pageSize.GetBottom(30), 0);
            cb.EndText();
        }

你可以在这里查看整个代码。

创建页脚和页眉

于 2013-04-08T11:50:38.047 回答
1
document = new iTextSharp.text.Document();

HeaderFooter footer = new HeaderFooter(new Phrase("footer string here"), false);
footer.Alignment = iTextSharp.text.HeaderFooter.BOTTOM;
document.Footer = footer;

document.Open();

我不得不从这段代码中调整为只在最后一页上显示页脚,然后页脚才会出现在每一页上。所以我想回答这里发布的原始问题,可以在每一页上显示一个页脚。

于 2015-07-23T19:01:05.287 回答