我在使用 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;
}
我需要一种链接表格并强制它们显示在同一页面上的方法。如果它不适合当前页面的剩余空间,则自动创建一个新页面。
有谁知道如何实现这一目标?谢谢。