C# - 每页上的 itextSharp 小计

标签 c# itext subtotal

您可以在问题的编辑部分找到 C# 的解决方案。 特别感谢 Bruno Lowagie

我正在尝试通过 C# 中的 iTextSharp 创建发票。它工作得很好,但是当我尝试在每一页上打印小计时我遇到了问题。

这是我用来为产品创建表格的代码的一部分:

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 等是我的边框样式的常量。

此代码生成如下所示的表格:

+------------------------------------------+
| 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                          |       |
+------------------------------------------+

表格可以转到下一页,我想在页脚处写下每一页的小计。 “OnEndPage”事件对我没有帮助,因为该事件“不知道”页面在表中的哪个位置损坏。

谁能告诉我如何得到每一页的小计?有什么解决方案可以让我在页面上总结一些内容并将其打印在 pdf 文件上吗?

对不起,如果我的描述不是那么好。我的英语有问题。 :-/

编辑:

这是解决方案。特别感谢布鲁诺。他给我指明了道路。 在我的版本中,小计不会在每一页上重置。我想这是我对问题描述的错误。

新类:

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

创建表的代码:

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

最佳答案

请看SubTotal示例。

首先我们创建一个类来跟踪小计和总计:

class Totals {
    double subtotal = 0;
    double total = 0;
}

然后我们实现我们将在第 5 列中使用的 PdfPCellEvent 接口(interface):

class SubTotalEvent implements PdfPCellEvent {

    Double price;
    Totals totals;

    public SubTotalEvent(Totals totals, double price) {
        this.totals = totals;
        this.price = price;
    }

    public SubTotalEvent(Totals totals) {
        this.totals = totals;
        price = null;
    }

    @Override
    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        if (price == null) {
            PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
                    new Phrase(String.valueOf(totals.subtotal)),
                    position.getLeft() + 2, position.getBottom() + 2, 0);
            totals.subtotal = 0;
            return;
        }
        totals.subtotal += price;
        totals.total += price;
    }

}

我们定义了两个构造函数:一个用于包含价格的普通单元格。一个用于包含小计的页脚单元格(在这种情况下我们不传递价格)。

这就是我们如何使用这个单元格事件:

public void createPdf(String dest) throws IOException, DocumentException {

    Totals totals = new Totals();

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(5);
    table.setWidths(new int[]{1, 1, 1, 3, 3});
    // header
    table.addCell("Pos");
    table.addCell("Menge");
    table.addCell("Text");
    table.addCell("Einzerpreis");
    table.addCell("Summe");
    // footer
    PdfPCell cell = new PdfPCell(new Phrase("Subtotal"));
    cell.setColspan(4);
    table.addCell(cell);
    cell = new PdfPCell();
    cell.setCellEvent(new SubTotalEvent(totals));
    table.addCell(cell);
    // definitions
    table.setHeaderRows(2);
    table.setFooterRows(1);
    // table body
    for(int r = 0; r < 50; ){
        table.addCell(String.valueOf(++r));
        table.addCell("1");
        table.addCell("text");
        table.addCell("10.0");
        cell = new PdfPCell(new Phrase("10.0"));
        cell.setCellEvent(new SubTotalEvent(totals, 10));
        table.addCell(cell);
    }
    document.add(table);
    // extra footer
    table = new PdfPTable(5);
    table.setWidths(new int[]{1, 1, 1, 3, 3});
    cell = new PdfPCell(new Phrase("Grand total"));
    cell.setColspan(4);
    table.addCell(cell);
    table.addCell(String.valueOf(totals.total));
    document.add(table);
    document.close();
}

结果是这样的:

enter image description here

在第一页上,我们有 46 行正文行,每行对应价格为 10 的商品。在页脚中,我们有 460 的小计。在第二页上,我们有 4 行正文行,每行对应价格为 10 的商品。在页脚中,我们有一个小计 40。我们添加了一个额外的表格来模拟总计的额外页脚:500。

关于C# - 每页上的 itextSharp 小计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39702435/

相关文章:

excel - 隐藏列时的公式小计 109 在 Excel 中不起作用

excel - 需要 Excel SUBTOTAL 以有条件地使用来自不同列的总和

r - 在 R 中计算小计

c# - 有什么办法可以让这个查询更快吗?

c# - 单例设计模式是获得数据库连接的首选

c# - iText 7 - 旋转文本未返回正确的角度

java - 为什么我没有得到最新的输出?

java - servlet 创建的 Pdf 文件未正确加载

c# - 将表达式类与 LINQ 查询一起使用

c# - 如何在 ASP.NET 中允许多种身份验证方法?