表格边框不会自动适合内容

时间:2015-06-18 20:40:18

标签: c# asp.net-mvc itextsharp

我的发票是通过iTextSharp(4.1.2)构建的。

我的问题在于我的发票的某个部分,其中我显示了条款和条件以及付款条件。下图显示了我的问题。

Output from the code provided below.

但是,当动态行不存在时(我的意思是没有选定的折扣,没有运费和没有税,只是小计和总计),外观与图像类似。我的下边框向下倾斜到底部,而不是在细胞内。

约束

  • 由于已经打印,升级现在还不是一个选项 需要iTextSharp 4.1.2。
  • 的功能
  • 由于时间限制,使用HTML到PDF还不是一种选择。

我希望你能帮助我。

编辑:修改了问题,以反映复制的输出和代码。



    protected void btnViewPDF_Click(object sender, EventArgs e)
    {
        // Create a document in memory with default settings.
        var document = new Document();
        string path = Server.MapPath("PDFs");

        // Commit the document to disk.
        PdfWriter.GetInstance(document, new FileStream(path + "/PaymentDetails.pdf", FileMode.Create));

        // Open th document.
        document.Open();
        
        #region Do your PDF document logic here...

        // Create a PdfPTable to contain two columns. This will be used for the layout.
        PdfPTable outerTable = new PdfPTable(2);
        outerTable.TotalWidth = 510f;
        outerTable.LockedWidth = true;
        outerTable.DefaultCell.Border = Rectangle.BOX;

        #region First Column

        // Add PdfPTable in the first column.
        PdfPTable outerTableFirstColumn = new PdfPTable(1);
        outerTableFirstColumn.TotalWidth = 450f;
        outerTableFirstColumn.DefaultCell.BorderColor = Color.BLACK;

        // Add cells to the first column.
        PdfPCell outerTableFirstColumnHeader = new PdfPCell()
            {
                Border = Rectangle.BOX,
                BorderColor = Color.BLACK,
                Phrase = new Phrase("Terms and Conditions", FontFactory.GetFont("Consolas", 8, (int) Font.BOLD, Color.BLACK)),
                Padding = 5f
            };

        outerTableFirstColumn.AddCell(outerTableFirstColumnHeader);

        String myTermsAndConditions = "Acceptance and Cancellation of Orders. An invoice accepted by the Customer may not be cancelled by the Customer and constitutes a binding agreement between the Customer and the Company. In the event of cancellation or other withdrawal of an order by Customer for any reason and without limiting any other remedy which the Company may have as a result of such cancellation or other withdrawal, reasonable cancellation charges, which shall include all expenses then incurred and commitments made by the Company, shall be paid by the Customer to the Company.";
        
        PdfPCell outerTableFirstColumnContent = new PdfPCell()
        {
            Border = Rectangle.BOX,
            BorderColor = Color.BLACK,
            Phrase = new Phrase(myTermsAndConditions, FontFactory.GetFont("Consolas", 8, (int)Font.NORMAL, Color.BLACK)),
            Padding = 5f
        };

        outerTableFirstColumn.AddCell(outerTableFirstColumnContent);

        #endregion

        #region Second Column

        // Add PdfPTable in the second column.
        PdfPTable outerTableSecondColumn = new PdfPTable(1);
        outerTableSecondColumn.TotalWidth = 70f;
        outerTableSecondColumn.DefaultCell.Border = Rectangle.BOX;
        
        PdfPTable outerTableSecondColumnContent = new PdfPTable(3);
        outerTableSecondColumnContent.SetWidths(new float[] { 3f, 0.5f, 1.5f });
        outerTableSecondColumnContent.DefaultCell.Border = Rectangle.BOX;

        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("Subtotal:"));
        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("$"));
        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("999.99"));

        #region Conditional adding of cells

        // Initialize details which can be controlled to produce results.
        var subTotal = 999.99M;

        var selectedDiscountType = DiscountType.NoDiscount;
        var flatDiscountAmount = 30M;
        var discountRate = 23.18M;
        var discountPercentAmount = 0.00M;

        var selectedTaxType = TaxType.NoTax;
        var flatTaxAmount = 20.00M;
        var taxRate = 12.25M;
        var taxPercentAmount = 0.00M;

        var includeShipping = false;
        var shippingCost = 20.50M;

        if (selectedDiscountType != 0)
        {
            switch (selectedDiscountType)
            {
                case DiscountType.FlatDiscount:
                    {
                        // Discount Amount
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("Flat Discount:"));
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("$"));
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell(decimal.Parse(flatDiscountAmount.ToString()).ToString("N")));
                    }
                    break;
                case DiscountType.PercentDiscount:
                    {
                        flatDiscountAmount = 0;

                        // Discount Rate
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("% Discount:"));
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell(" "));
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell(String.Format(discountRate.ToString(), "{0:0.0}")));

                        discountPercentAmount = subTotal * (discountRate / 100);

                        // Total Discount
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("Total Discount:"));
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("$"));
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell(decimal.Parse(discountPercentAmount.ToString()).ToString("N")));
                    }
                    break;
                default:
                    break;
            }
        }

        if (includeShipping)
        {
            // Shipping Cost
            outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("Shipping Cost:"));
            outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("$"));
            outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell(decimal.Parse(shippingCost.ToString()).ToString("N")));
        }

        if (selectedTaxType != 0)
        {
            switch (selectedTaxType)
            {
                case TaxType.FlatTax:
                    {
                        // Tax Amount
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("Flat Tax:"));
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("$"));
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell(decimal.Parse(flatTaxAmount.ToString()).ToString("N")));
                    }
                    break;
                case TaxType.PercentTax:
                    {
                        flatTaxAmount = 0;

                        // Tax Percent
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("% Tax:"));
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell(" "));
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell(String.Format(taxRate.ToString(), "{0:0.0}")));

                        taxPercentAmount = (subTotal - (flatDiscountAmount + discountPercentAmount) + (shippingCost)) * (taxRate / 100);

                        // Total Tax
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("Total Tax:"));
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("$"));
                        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell(decimal.Parse(taxPercentAmount.ToString()).ToString("N")));
                    }
                    break;
                default:
                    break;
            }
        }

        #endregion

        var grandTotalCost = (subTotal - (flatDiscountAmount + discountPercentAmount) + shippingCost) + (flatTaxAmount + taxPercentAmount);

        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("Grand Total:"));
        outerTableSecondColumnContent.AddCell(GeneratePaymentDetailCell("$"));
        outerTableSecondColumnContent.AddCell(GenerateGrandTotalCostCell(decimal.Parse(grandTotalCost.ToString()).ToString("N")));

        outerTableSecondColumn.AddCell(outerTableSecondColumnContent);

        #endregion

        // Add the columns to the table.
        outerTable.AddCell(outerTableFirstColumn);
        outerTable.AddCell(outerTableSecondColumn);

        // Add the table to the document.
        document.Add(outerTable);
        #endregion
        
        // Close the document.
        document.Close();
        
    }

    public PdfPCell GeneratePaymentDetailCell(String content) 
    { 
        PdfPCell cell = new PdfPCell()
        {
            Phrase = new Phrase(content, FontFactory.GetFont("Consolas", 8, Font.BOLD, Color.BLACK)),
            HorizontalAlignment = Element.ALIGN_RIGHT,
            VerticalAlignment = Element.ALIGN_TOP,
            Border = Rectangle.BOX
        };

        return cell;
    }

    public PdfPCell GenerateGrandTotalCostCell(String grandTotalCost)
    {            
        PdfPCell cell = new PdfPCell()
        {
            Phrase = new Phrase(grandTotalCost, FontFactory.GetFont("Consolas", 8, Font.BOLD, Color.BLACK)),
            HorizontalAlignment = Element.ALIGN_RIGHT,
            VerticalAlignment = Element.ALIGN_TOP,
            Border = 1,
            BorderWidthTop = 1,
            BorderWidthBottom = 2
        };

        return cell;
    }

    enum DiscountType
    {
        FlatDiscount = 100,
        PercentDiscount = 200,
        NoDiscount = 300
    }

    enum TaxType
    {
        FlatTax = 100,
        PercentTax = 200,
        NoTax = 300
    }




1 个答案:

答案 0 :(得分:0)

从我的复制中得到答案。通过在 outerTableSecondColumnContent 中添加一个空行(以AddCells()的形式),单元格缩小以适合内容。