C# iTextSharp表格:未显示的行
我在使用iTexSharp版本5.5.7.0的表时遇到问题 我想建立一个按类别和子类别分组的产品目录 我需要显示类别名称、子类别名称以及其中的产品。 当子类别更改时,我需要重复类别名称:C# iTextSharp表格:未显示的行,c#,.net,itextsharp,pdfptable,C#,.net,Itextsharp,Pdfptable,我在使用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
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...
缺少product 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剩余部分,则自动创建一个新页面
有人知道如何做到这一点吗?
谢谢