指数超出范围.必须非负且小于集合的大小.使用DynamicPDF

本文关键字:集合 小于 DynamicPDF 使用 范围 指数 | 更新日期: 2023-09-27 18:11:25

每当我运行这段代码时,我都会得到这个错误-我一直盯着它几个小时,试图破译正在发生的事情,但它是动态PDF内部的东西,所以我无法进入代码来调试它。如有任何帮助,不胜感激。

    public void GeneratePDF(Guid id)
    {
        ceTe.DynamicPDF.Document doc = new ceTe.DynamicPDF.Document();
        doc.Author = new IssueTracker.ActiveDirectory.Searcher().FindBySAMAccountName(User.Identity.Name.Remove(0, 5)).DisplayName;
        doc.Creator = "GPO Pricing Application";
        doc.Title = "GPO Pricing Recommendation";
        Page page = new Page(ceTe.DynamicPDF.PageSize.Letter, ceTe.DynamicPDF.PageOrientation.Landscape);
        doc.Pages.Add(page);
        Worksheet worksheet = repo.GetByID(id);
        AddElement(page, "Report Name: ", worksheet.Name, 0, 0 );
        AddElement(page, "Customer Name: ", worksheet.CustomerName, 0, 20);
        AddElement(page, "Allied Group: ", worksheet.AlliedGroup, 0, 40);
        AddElement(page, "City, State: ", worksheet.City + ", " + worksheet.State, 0, 60);
        AddElement(page, "Gross Annual Sales: ", string.Format("{0:C}", worksheet.AnnualSales), 0, 80);
        var Y = 100;
        Table2 table = new Table2(0, Y, 850, 600);
        table.Border.Width = 0.5f;
        table.CellDefault.Border.Width = 0.5f;
        table.Columns.Add(45); // Contract Id
        table.Columns.Add(125); // Product Line
        table.Columns.Add(75); // SKU
        table.Columns.Add(150); // Description
        table.Columns.Add(30);  // Qty
        table.Columns.Add(50); // Current Price (ea)
        table.Columns.Add(50); // Current Spend
        table.Columns.Add(50); // New Price (ea)
        table.Columns.Add(50); // New Spend
        table.Columns.Add(50); // Variance
        Row2 row = table.Rows.Add(10, Font.TimesBold, 8);
        row.CellDefault.VAlign = VAlign.Top;
        row.CellDefault.Align = TextAlign.Center;
        row.Cells.Add("Contract Id");
        row.Cells.Add("Product Line");
        row.Cells.Add("SKU");
        row.Cells.Add("Description");
        row.Cells.Add("Qty");
        row.Cells.Add("Current Price (ea)");
        row.Cells.Add("Current Spend");
        row.Cells.Add("New Price (ea)");
        row.Cells.Add("New Spend");
        row.Cells.Add("Variance");
        var products = productRepository.Get(id);
        foreach(var product in products)
        {
            Y += 20;
            if (Y > 580)
            {
                page.Elements.Add(table);
                page = new Page(ceTe.DynamicPDF.PageSize.Letter, ceTe.DynamicPDF.PageOrientation.Landscape);
                doc.Pages.Add(page);
                table = new Table2(0, Y, 850, 800);
                Y = 0;
                row = table.Rows.Add(10, Font.TimesBold, 8);
                row.CellDefault.VAlign = VAlign.Top;
                row.CellDefault.Align = TextAlign.Center;
                row.Cells.Add("Contract Id"); //*********** This is where I get the error
                row.Cells.Add("Product Line");
                row.Cells.Add("SKU");
                row.Cells.Add("Description");
                row.Cells.Add("Qty");
                row.Cells.Add("Current Price (ea)");
                row.Cells.Add("Current Spend");
                row.Cells.Add("New Price (ea)");
                row.Cells.Add("New Spend");
                row.Cells.Add("Variance");
            }
            row = table.Rows.Add(10, Font.TimesRoman, 8);
            row.CellDefault.Align = TextAlign.Center;
            Cell2 contractId = row.Cells.Add(product.record.ContractId);
            Cell2 productLine = row.Cells.Add(product.record.ProductLine);
            Cell2 sku = row.Cells.Add(product.record.LongItemNbr);
            Cell2 description = row.Cells.Add(product.record.Description);
            Cell2 qty = row.Cells.Add(product.Qty.ToString());
            Cell2 currentPrice = row.Cells.Add(string.Format("{0:C}", product.record.DirectPriceEaches));
            Cell2 currentSpend = row.Cells.Add(string.Format("{0:C}", product.Spend));
        }
        doc.DrawToWeb("GPO Pricing Recommendation.pdf");
    }
    private void AddElement(Page page, string label, string data, int x, int y)
    {
        Label Label = new Label(label, x, y, 140, 14, Font.TimesRoman, 14, TextAlign.Right);
        Label Data = new Label(data, x + 145, y, 250, 14, Font.TimesRoman, 14, TextAlign.Left);
        page.Elements.Add(Label);
        page.Elements.Add(Data);
    }

指数超出范围.必须非负且小于集合的大小.使用DynamicPDF

我在代码的if (Y> 580)部分缺少了table.columns.add

相关文章: