iTextSharp 表最后一个单元格固定高度不起作用

问题描述 投票:0回答:1

我正在使用 iTextSharp 生成 PDF 文档。我对嵌套表格中的最后一个单元格有一个小问题,它的高度是单元格空间的剩余部分,即使我为每个单元格设置了固定的高度。有没有办法覆盖这种行为,使其保持固定高度。我附上了正在发生的事情的图片。

var cats = DAL.Merchandise.GetListOfMerchandiseCategories();
var data = DAL.Merchandise.GetListOfMerchandise().AsQueryable();

var document = new Document(PageSize.A4, 25, 25, 25, 25);

var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
var mainTable = new PdfPTable(3);

    mainTable.SetWidths(new float[] { 200f, 200f, 200f });
    mainTable.TotalWidth = 600f;
    mainTable.LockedWidth = true;

    float[] widths = new float[] { 60f, 120f };

    var items = data.Where(x => x.Category == cat);
    var groupedItems = items.GroupBy(_ => _.PartDesc).Select(_ => new { _.Key, items = _ });

    foreach(var item in groupedItems) {
        var table = new PdfPTable(2);

        table.TotalWidth = widths.Sum();
        table.LockedWidth = true;
        table.SetWidths(widths);

        //Spacer
        table.AddCell(new PdfPCell(new Phrase("")) { Colspan = 2, FixedHeight = 30, Border = Rectangle.NO_BORDER });

        Image jpg;
        //try
        //{
        //      jpg = Image.GetInstance(string.Format(GlobalClass.ImageManagerURLForImages.ToString(),items.ImageName));
        //}
        //catch
        //{
            jpg = Image.GetInstance(System.Web.Hosting.HostingEnvironment.MapPath("~/_common/img/noimageavailable.png"));
        //}

        table.AddCell(new PdfPCell(jpg, true) { Colspan = 2, Border = Rectangle.NO_BORDER });

        //PartDesc
        table.AddCell(new PdfPCell(new Phrase(item.items.First().PartDesc, subTitleFont)) { Colspan = 2, FixedHeight = 20, Border = Rectangle.NO_BORDER });

        var alternate = false;
        foreach(var part in item.items) {
            var color = alternate ? BaseColor.GRAY : BaseColor.WHITE;
            table.AddCell(new PdfPCell(new Phrase(string.IsNullOrWhiteSpace(part.PartData) ? "" : part.PartData)) { Border = Rectangle.NO_BORDER, BackgroundColor = color, MinimumHeight = 15f, FixedHeight = 15f, PaddingBottom = 1, PaddingTop = 1, PaddingLeft = 2 });
            if (string.IsNullOrEmpty(part.Price))
                table.AddCell(new PdfPCell(new Phrase(part.PartNo)) { Border = Rectangle.NO_BORDER, BackgroundColor = color, MinimumHeight = 15f, FixedHeight = 15f, PaddingBottom = 1, PaddingTop = 1, PaddingLeft = 2 });
            else
                table.AddCell(new PdfPCell(new Phrase(string.Format("{0} £{1:0.00}pp", part.PartNo, part.Price))) { Border = Rectangle.NO_BORDER, MinimumHeight = 15f, BackgroundColor = color, FixedHeight = 15f, PaddingBottom = 1, PaddingTop = 1, PaddingLeft = 2 });

            alternate = !alternate;
        }

        table.SetExtendLastRow(false, false);

        mainTable.AddCell(new PdfPCell(table) { Border = Rectangle.NO_BORDER });
    }

    //To fill in the blank cells on a row, so that the row can get displayed
    var count = groupedItems.Count();
    while (count > 3)
        count -= 3;

    var remainder = 3 - count;

    for(int i = 0; i < remainder; i++) {
        mainTable.AddCell(new PdfPCell(new Phrase("")) { Border = Rectangle.NO_BORDER });
    }

    document.Add(mainTable);

生成的 PDF 中的片段

编辑

由于我们无法访问 DAL,因此上面的代码已在下面重新编写以使用模拟类

public class item {
    public String PartDesc;
    public String PartData;
    public String PartNo;
    public String Price;
    public item(String desc, String data, String no, String price) {
        this.PartDesc = desc;
        this.PartData = data;
        this.PartNo = no;
        this.Price = price;
    }
}

实际代码:

//Sample data since we don't have the DAL
var items = new item[] {
    new item("Alpha", "A Data", "A", "1"),
    new item("Alpha", "B Data", "B", "2"),
    new item("Bravo", "A Data", "A", "1"),
    new item("Bravo", "B Data", "B", "2"),
    new item("Charlie", "A Data", "A", "1"),
    new item("Charlie", "B Data", "B", "2"),
    new item("Charlie", "C Data", "C", "3"),
    new item("Charlie", "D Data", "D", "4"),
    new item("Charlie", "E Data", "E", "5")
};

//File to output to
var outputFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");

//Will hold the PDF as a byte array when we're done
Byte[] bytes;


using (var output = new MemoryStream()) {
    using (var document = new Document(PageSize.A4, 25, 25, 25, 25)) {
        using (var writer = PdfWriter.GetInstance(document, output)) {
            document.Open();

            var mainTable = new PdfPTable(3);


            mainTable.SetWidths(new float[] { 200f, 200f, 200f });
            mainTable.TotalWidth = 600f;
            mainTable.LockedWidth = true;

            float[] widths = new float[] { 60f, 120f };

            var groupedItems = items.GroupBy(_ => _.PartDesc).Select(_ => new { _.Key, items = _ });

            foreach (var item in groupedItems) {
                var table = new PdfPTable(2);

                table.TotalWidth = widths.Sum();
                table.LockedWidth = true;
                table.SetWidths(widths);

                //Spacer
                table.AddCell(new PdfPCell(new Phrase("")) { Colspan = 2, FixedHeight = 30, Border = iTextSharp.text.Rectangle.NO_BORDER });


                //PartDesc
                table.AddCell(new PdfPCell(new Phrase(item.items.First().PartDesc)) { Colspan = 2, FixedHeight = 20, Border = iTextSharp.text.Rectangle.NO_BORDER });

                var alternate = false;
                foreach (var part in item.items) {
                    var color = alternate ? BaseColor.GRAY : BaseColor.WHITE;
                    table.AddCell(new PdfPCell(new Phrase(string.IsNullOrWhiteSpace(part.PartData) ? "" : part.PartData)) { Border = iTextSharp.text.Rectangle.NO_BORDER, BackgroundColor = color, MinimumHeight = 15f, FixedHeight = 15f, PaddingBottom = 1, PaddingTop = 1, PaddingLeft = 2 });
                    if (string.IsNullOrEmpty(part.Price))
                        table.AddCell(new PdfPCell(new Phrase(part.PartNo)) { Border = iTextSharp.text.Rectangle.NO_BORDER, BackgroundColor = color, MinimumHeight = 15f, FixedHeight = 15f, PaddingBottom = 1, PaddingTop = 1, PaddingLeft = 2 });
                    else
                        table.AddCell(new PdfPCell(new Phrase(string.Format("{0} £{1:0.00}pp", part.PartNo, part.Price))) { Border = iTextSharp.text.Rectangle.NO_BORDER, MinimumHeight = 15f, BackgroundColor = color, FixedHeight = 15f, PaddingBottom = 1, PaddingTop = 1, PaddingLeft = 2 });

                    alternate = !alternate;
                }

                table.SetExtendLastRow(false, false);

                mainTable.AddCell(new PdfPCell(table) { Border = iTextSharp.text.Rectangle.NO_BORDER });
            }

            //To fill in the blank cells on a row, so that the row can get displayed
            var count = groupedItems.Count();
            while (count > 3)
                count -= 3;

            var remainder = 3 - count;

            for (int i = 0; i < remainder; i++) {
                mainTable.AddCell(new PdfPCell(new Phrase("")) { Border = iTextSharp.text.Rectangle.NO_BORDER });
            }

            document.Add(mainTable);

            document.Close();
        }
    }
    bytes = output.ToArray();
}


System.IO.File.WriteAllBytes(outputFile, bytes);
c# pdf itext
1个回答
0
投票

我能够通过以下设置修复它:

table.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER); table.setExtendLastRow(false, false);

对齐部分是最重要的部分,因为使用默认对齐方式,行扩展值在渲染时会被覆盖。

© www.soinside.com 2019 - 2024. All rights reserved.