我正在尝试制作一个在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);
}
}
除了此页脚之外,此代码没有任何问题。
答案 0 :(得分: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代码时请使用官方文档。
答案 1 :(得分: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();
}
您可以在此处查看整个代码。
答案 2 :(得分: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();
我必须改编自这段代码,才能在最后一页显示页脚,然后我才会在页面上显示页脚。所以我想回答这里发布的原始问题,可以在每个页面上显示页脚。