2013-04-08 121 views
1

我想制作一个程序,在PDF文件中保存报价。它工作的很好,但是我的页脚有问题。我跟着这些instructions为什么页脚仅显示在最后一页上?

不幸的是,页脚仅显示在最后一页上。我该如何解决这个问题?我正在寻找一个最简单的解决方案,因为我刚刚开始使用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); 
    } 
} 

没有问题,此代码除本页脚。

回答

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代码时请使用官方文档。

+0

啊!非常感谢你。问题发生在open()之后的wri.PageEvent中。 OnStartPage方法是测试问题的垃圾,其实它是OnEndPage中的相同代码。无论如何 - 谢谢你:D – 2013-04-08 11:53:49

+1

不客气。奇怪的是,答案是downvoted ... – 2013-04-08 11:55:54

+0

我不知道是否它的这个downvote我的错。我压了起来,但我认为我无法上涨或下降,因为声望= 13 ... – 2013-04-08 12:17:01

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(); 
     } 

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

CREATING FOOTER AND HEADER

+0

答案不完整。该事件仅适用于最后一页的主要原因是该事件设置在错误的时刻。它应该在打开文档之前设置,而不是在关闭之前设置。 – 2013-04-08 11:55:10

+0

这是正确的..谢谢@BrunoLowagie会记住这一点。谢谢 – pordi 2013-04-08 11:57:49

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(); 

我不得不从这个代码适应仅在最后一页上显示页脚,之前我做了页脚是显示在每个页面上的。所以我想回答这里发布的原始问题,可以在每个页面上显示页脚。