我使用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);
修改
由于我们无法访问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);