使用带有iTexSharp版本5.5.7.0的表格时遇到问题。
我想建立一个按类别和子类别分组的产品目录。
我需要显示类别名称,子护理名称及其中的产品。 当子类别改变时,我需要重复类别名称:
Category 1
Sub category 1
Product 1 table
Product 2 table...
Category 1
Sub category 2
Product 1 table ...
Category 2
Sub category 1
Product 1 table
Product 2 table...
Category 2
Sub category 2
Product 1 table
Product 2 table
Product 3 table...
问题是我不希望在页面底部显示类别和子类别名称,并在下一页显示该子类别的第一个产品。
所以我创建了一个tableContainer,其中我添加了tableCategory,tableSubCategory和第一个tableProduct。
我将此tabContainer添加到文档中,然后我添加了一个包含其余产品的表。 我尝试使用KeepTogether,SplitRows和SplitLate但没有成功。 有时似乎有效,但有些行没有在页面上显示如下:
End of Page :
Category
Sub category
New Page :
Product 2 table...
Product 3 table...
缺少产品1表
我的文件是A4尺寸,有边距:
pdfDocument = pdfDocument = new Document(iTextSharp.text.PageSize.A4, 20, 20, 40, 40);
pdfWriter = PdfWriter.GetInstance(pdfDocument, new FileStream(path, fileMode));
我在文档中显示数据的方式:
PdfPTable tableContainer = CreateTable(new float[] { 100f }, false, 0f, 0f, pdfDocument.PageSize.Width);
tableContainer.SplitRows = false;
tableContainer.KeepTogether = true;
foreach (string category in categoriesList)
{
//Cell category
PdfPCell cellCategory = CreateCell(category, Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, "Oswald", Rectangle.NO_BORDER, 8, Font.BOLD, BaseColor.WHITE, lightBlueColor, 1, 30);
foreach (string subCategory in subCategoriesList)
{
//Cell sub category
PdfPCell cellSubCategory = CreateCell(category, Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, "Oswald", Rectangle.NO_BORDER, 8, Font.BOLD, BaseColor.WHITE, lightBlueColor, 1, 30);
tableContainer.AddCell(cellCategory);
tableContainer.AddCell(cellSubCategory);
//table with list of products
PdfPTable productsTable = CreateTable(new float[] { 100f }, false, 0f, 10f, pdfDocument.PageSize.Width - (pdfDocument.LeftMargin + pdfDocument.RightMargin) - 40);
foreach (Product p in productsFilteredList)
{
//Create product Table
PdfPTable productTable = CreateProductTable(pdfDocument, p);
//If first product : category , sub category and first product must be displayed together on the same page
if(firstProduct)
tableContainer .AddCell(productTable);
else
productsTable.AddCell(productTable);
}
pdfDocument.Add(tableContainer);
pdfDocument.Add(productsTable);
}
}
private PdfPTable CreateProductTable(Document pdfDocument, product p)
{
PdfPTable productTable = CreateTable(new float[] { 25f, 20f, 35f, 10f, 10f }, false, 0f, 0f, pdfDocument.PageSize.Width - (pdfDocument.LeftMargin + pdfDocument.RightMargin) - 40);
PdfPCell cellImage = CreateCellImage(tempFolderPhotosPath + p.Code + ".jpeg", 50f, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER, 1, Rectangle.UNDEFINED, 60f);
cellImage.PaddingLeft = 30f;
productTable.AddCell(cellImage);
productTable.AddCell(CreateCell(p.Code, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER, "Oswald", Rectangle.NO_BORDER, 7, Font.BOLD, BaseColor.BLACK, backGroundColor, 1, productTable.DefaultCell.Height));
productTable.AddCell(CreateCell(p.Description, Element.ALIGN_MIDDLE, Element.ALIGN_LEFT, "Oswald", Rectangle.NO_BORDER, 8, Font.NORMAL, BaseColor.BLACK, backGroundColor, 1, productTable.DefaultCell.Height));
productTable.AddCell(CreateCell(p.Reference, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER, "Oswald", Rectangle.NO_BORDER, 8, Font.BOLD, BaseColor.BLACK, backGroundColor, 1, productTable.DefaultCell.Height));
productTable.AddCell(CreateCell(p.Price , Element.ALIGN_MIDDLE, Element.ALIGN_CENTER, "Oswald", Rectangle.NO_BORDER, 8, Font.NORMAL, BaseColor.BLACK, backGroundColor, 1, productTable.DefaultCell.Height));
return productTable;
}
private PdfPCell CreateCell(string content, int verticalAlignement, int horizontalAlignement, string fontName, int border, int fontSize, int fontStyle, BaseColor fontColor, BaseColor backgroundColor, int rowSpan, float minHeight)
{
PdfPCell cell = new PdfPCell(new Phrase(new Chunk(content, FontFactory.GetFont(fontName, fontSize, fontStyle, fontColor))));
cell.BackgroundColor = backgroundColor;
cell.HorizontalAlignment = horizontalAlignement;
cell.VerticalAlignment = verticalAlignement;
cell.Border = border;
cell.Rowspan = rowSpan;
cell.MinimumHeight = minHeight;
cell.Border = border;
return cell;
}
private PdfPCell CreateCellImage(string pathImage, float widthPercentage, int verticalAlignement, int horizontalAlignement, int rowSpan, int border, float minHeight)
{
PdfPCell cell = new PdfPCell();
cell.HorizontalAlignment = horizontalAlignement;
cell.VerticalAlignment = verticalAlignement;
cell.Border = border;
cell.Rowspan = rowSpan;
cell.Border = Rectangle.NO_BORDER;
Image image = Image.GetInstance(pathImage);
image.ScaleToFit(widthPercentage, minHeight);
cell.MinimumHeight = minHeight + 10;
cell.AddElement(image);
return cell;
}
我需要一种方法来链接表并强制它们显示在同一页面上。如果它不适合当前页面剩余的sapce,则自动创建一个新页面。
有谁知道如何实现这一目标? 感谢。