Giải pháp cho C# bạn có thể tìm thấy trong phần Chỉnh sửa của Câu hỏi. Đặc biệt Cảm ơn Bruno LowagieC# - itextSharp tổng phụ trên mỗi trang
Tôi đang cố tạo hóa đơn qua iTextSharp trong C#. Nó hoạt động rất tốt nhưng im gettin vấn đề trong khi tôi cố gắng in subtotal trên mỗi trang.
Đây là một phần của im mã sử dụng để tạo ra bảng cho các sản phẩm:
PdfPTable table = new PdfPTable(5);
table.WidthPercentage = 100;
float[] widths = new float[] { 10f, 30f, 120f, 30f, 30f };
table.SetWidths(widths);
table.SkipLastFooter = true;
table.SpacingAfter = 10;
//Cells to Write Header & Footer
AddCell(table, "Pos.", PdfPCell.ALIGN_RIGHT, BORDER_LTB, 0);
AddCell(table, "Menge", PdfPCell.ALIGN_RIGHT, BORDER_TB);
AddCell(table, "Text", PdfPCell.ALIGN_LEFT, BORDER_TB);
AddCell(table, "Einzelpreis ", PdfPCell.ALIGN_RIGHT, BORDER_TB);
AddCell(table, "Summe", PdfPCell.ALIGN_RIGHT, BORDER_RTB);
AddCell(table, "SUBTOTAL", PdfPCell.ALIGN_LEFT, BORDER_LTB, 7, 4);
AddCell(table, "", PdfPCell.ALIGN_LEFT, BORDER_RTB);
table.HeaderRows = 2;
table.FooterRows = 1;
//Cells with positions of invoice
for (int i = 0; i < beleg.Einzel.Count; i++)
{
AddCell(table, beleg.Einzel[i].be_lfd_nr.ToString(), PdfPCell.ALIGN_LEFT, BORDER_LTB);
AddCell(table, beleg.Einzel[i].be_art_menge.ToString("N", _gbVars.NFI3D), PdfPCell.ALIGN_RIGHT, BORDER_TB);
AddCell(table, (beleg.Einzel[i].be_art_bez.ToString() + "\n" + beleg.Einzel[i].be_pos_text.ToString()).Trim(), PdfPCell.ALIGN_LEFT, BORDER_TB);
AddCell(table, beleg.Einzel[i].be_summe_preis.ToString("N", _gbVars.NFI2D), PdfPCell.ALIGN_RIGHT, BORDER_TB);
AddCell(table, beleg.Einzel[i].be_summe_netto.ToString("N", _gbVars.NFI2D), PdfPCell.ALIGN_RIGHT, BORDER_RTB);
}
table.SplitLate = false;
document.Add(table);
Border_RTB và như vậy là hằng số cho tôi Border-Styles.
Mã này tạo ra một phù thủy bảng trông như thế này:
+------------------------------------------+
| Pos | Menge | Text | Einzelpreis | Summe |
+------------------------------------------+
| 1 | 2 | Text | 10.00 | 20.00 |
+------------------------------------------+
| 2 | 1 | Text | 10.00 | 10.00 |
+------------------------------------------+
| 3 | 4 | Text | 10.00 | 40.00 |
+------------------------------------------+
| 4 | 2 | Text | 10.00 | 20.00 |
+------------------------------------------+
|SUBTOTAL | |
+------------------------------------------+
Bảng có thể chảy đến trang tiếp theo một i whant để viết tổng số phụ của mỗi trang ở chân. Sự kiện "OnEndPage" không giúp tôi vì sự kiện này không "biết" ở đâu trong bảng trang bị hỏng.
Ai đó có thể cho tôi biết cách tôi nhận tổng số phụ trên mỗi trang? Có giải pháp làm thế nào tôi có thể tổng hợp một cái gì đó trên một trang in nó ra trên tập tin pdf?
Xin lỗi nếu mô tả của tôi không tốt. Có vấn đề với tiếng anh của tôi. : -/
EDIT:
Đây là giải pháp. Đặc biệt Cảm ơn Bruno. Anh ấy đã chỉ cho tôi cách. Trong Phiên bản của tôi, Subtotal KHÔNG Reset trên mọi Trang. Tôi đoán đây là một sự hiểu lầm trong mô tả của tôi về vấn đề này.
Các tác phẩm Clases mới:
public class Totals
{
public double subtotal = 0;
public double total = 0;
}
public class SubTotalEvent : IPdfPCellEvent
{
Double price;
Totals totals;
bool printTotals = false; //If we just whant to print out the Subtotal, this will be true.
bool CellWrittenOnce = false; //Bool to SUM price just once (if row flows to a second page)
public SubTotalEvent(Totals totals, double price) {
printTotals = false;
this.totals = totals;
this.price = price;
}
public SubTotalEvent(Totals totals) {
this.totals = totals;
printTotals = true;
}
public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
if (printTotals)
{
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(totals.subtotal.ToString()), position.GetLeft(0) + 2, position.GetBottom(0) + 2, 0);
return;
}
if (!CellWrittenOnce) {
totals.subtotal += price;
totals.total += price;
}
CellWrittenOnce = true;
}
}
Mã để tạo ra các bảng:
PdfPTable table = new PdfPTable(5);
table.WidthPercentage = 100;
float[] widths = new float[] { 10f, 30f, 120f, 30f, 30f };
table.SetWidths(widths);
table.SkipLastFooter = true;
table.SpacingAfter = 10;
AddCell(new PdfPCell(new Phrase("item")));
AddCell(new PdfPCell(new Phrase("amount")));
AddCell(new PdfPCell(new Phrase("text")));
AddCell(new PdfPCell(new Phrase("price")));
AddCell(new PdfPCell(new Phrase("sum")));
Totals totals = new Totals();
PdfPCell cel = new PdfPCell(new Phrase("Subtotal"));
cel.Colspan = 4;
table.AddCell(cel);
cel = new PdfPCell();
cel.CellEvent = new SubTotalEvent(totals);
table.AddCell(cel);
table.HeaderRows = 2;
table.FooterRows = 1;
for (int i = 0; i < beleg.Einzel.Count; i++)
{
AddCell(new PdfPCell(new Phrase(i.toString())));
AddCell(new PdfPCell(new Phrase("2")));
AddCell(new PdfPCell(new Phrase("ItemText")));
AddCell(new PdfPCell(new Phrase("10.00")));
cell = new PdfPCell(new Phrase("20.00"));
cell.CellEvent = new SubTotalEvent(totals, 20.00);
table.AddCell(cell);
}
table.SplitLate = false;
document.Add(table);
Bạn cần triển khai lớp 'PdfPageEventHelper'. Việc triển khai của bạn sẽ thực thi mỗi khi trang được tạo. Xem hướng dẫn này về cách triển khai nó http://www.mazsoft.com/blog/post/2008/04/30/Code-sample-for-using-iTextSharp-PDF-library – Darren
Tôi triển khai lớp 'PdfPageEventHelper'. Nhưng làm thế nào tôi có thể tìm ra trong 'void override công khai OnEndPage (PdfWriter writer, Document doc)' nơi tôi đang ở trong bảng? Bảng đã được viết vào thời điểm này. Sự kiện này được gọi tại 'document.Add (table); ' –
Một số điều không rõ ràng ngay với tôi từ mô tả sự cố của bạn, vì vậy tôi không chắc chắn liệu mình có câu trả lời hay không. Các giả định sau có đúng không: 1. tài liệu hóa đơn của bạn bao gồm một bảng lớn duy nhất có thể có thể mở rộng nhiều trang. 2. Hóa đơn của bạn không phải là biểu mẫu, bạn có quyền truy cập vào tất cả thông tin khi tạo tài liệu. 3. Bạn muốn in tổng phụ bao gồm tất cả các sản phẩm trên trang hiện tại + tổng số phụ cho đến trang hiện tại trong đầu trang/chân trang của trang đó. –