Splitting tables
Examples that show what happens when a table doesn't fit the page:
splitrowatendofpage
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.tables;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;
import java.io.File;
public class SplitRowAtEndOfPage {
public static final String DEST = "./target/sandbox/tables/split_row_at_end_of_page.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new SplitRowAtEndOfPage().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
Table table = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();
// Notice that the width is bigger than available area (612 - 36 - 36 = 540, where 36 is the value of the left (and the right) margin
table.setWidth(550);
for (int i = 0; i < 6; i++) {
Cell cell = new Cell()
.add(new Paragraph((i == 5) ? "Three\nLines\nHere" : Integer.toString(i)));
table.addCell(cell);
}
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, new PageSize(612, 237));
doc.add(table);
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Tables
{
public class SplitRowAtEndOfPage
{
public static readonly string DEST = "results/sandbox/tables/split_row_at_end_of_page.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new SplitRowAtEndOfPage().ManipulatePdf(DEST);
}
private void ManipulatePdf(String dest)
{
Table table = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
// Notice that the width is bigger than available area (612 - 36 - 36 = 540, where 36 is the value of the left (and the right) margin
table.SetWidth(550);
for (int i = 0; i < 6; i++)
{
Cell cell = new Cell()
.Add(new Paragraph((i == 5) ? "Three\nLines\nHere" : i.ToString()));
table.AddCell(cell);
}
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, new PageSize(612, 237));
doc.Add(table);
doc.Close();
}
}
}
splitrowatspecificrow
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.tables;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.element.Table.RowRange;
import com.itextpdf.layout.layout.LayoutContext;
import com.itextpdf.layout.layout.LayoutResult;
import com.itextpdf.layout.properties.AreaBreakType;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.renderer.AreaBreakRenderer;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.IRenderer;
import com.itextpdf.layout.renderer.TableRenderer;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class SplitRowAtSpecificRow {
public static final String DEST = "./target/sandbox/tables/split_row_at_specific_row.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new SplitRowAtSpecificRow().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
Table table = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();
table.setWidth(550);
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, new PageSize(612, 242));
for (int i = 0; i < 40; i++) {
Cell cell;
if (i == 20) {
cell = new Cell().add(new Paragraph("Multiple\n\n\n\nLines"));
} else {
cell = new Cell().add(new Paragraph(Integer.toString(i)));
}
table.addCell(cell);
}
table.setNextRenderer(new SplitTableAtSpecificRowRenderer(table, new ArrayList<>(
Arrays.asList(
// first break occurs on row 4
4,
// break at row 7 gets ignored because if we break at 9 it still fits the page
7, 9,
// last break occurs at 25 the other rows get rendered as normal
25,
// break point 36 does not break because the remaining rows still fit the page
36,
// break point 50 gets ignored because there are not enough rows
50
))
));
doc.add(table);
doc.close();
}
static class SplitTableAtSpecificRowRenderer extends TableRenderer {
private final ArrayList<Integer> breakPoints;
private int amountOfRowsThatAreGoingToBeRendered = 0;
public SplitTableAtSpecificRowRenderer(Table modelElement, ArrayList<Integer> breakPoints) {
super(modelElement);
this.breakPoints = breakPoints;
}
@Override
public IRenderer getNextRenderer() {
return new SplitTableAtSpecificRowRenderer((Table) modelElement, this.breakPoints);
}
@Override
public LayoutResult layout(LayoutContext layoutContext) {
LayoutResult result = null;
while (result == null) {
result = attemptLayout(layoutContext, this.breakPoints);
}
this.breakPoints.replaceAll(integer -> integer - this.amountOfRowsThatAreGoingToBeRendered);
return result;
}
private LayoutResult attemptLayout(LayoutContext layoutContext, List<Integer> breakPoints) {
LayoutResult layoutResult = super.layout(layoutContext);
if (layoutResult.getStatus() == LayoutResult.FULL || breakPoints.isEmpty()) {
this.amountOfRowsThatAreGoingToBeRendered = getAmountOfRows(layoutResult);
return layoutResult;
}
int breakPointToFix = calculateBreakPoint(layoutContext);
if (breakPointToFix >= 0) {
forceAreaBreak(breakPointToFix);
this.amountOfRowsThatAreGoingToBeRendered = breakPointToFix - 1;
return null;
}
return layoutResult;
}
private int calculateBreakPoint(LayoutContext layoutContext) {
LayoutResult layoutResultWithoutSplits = attemptLayout(layoutContext, Collections.emptyList());
if (layoutResultWithoutSplits == null) {
return Integer.MIN_VALUE;
}
int amountOfRowsThatFitWithoutSplit = getAmountOfRows(layoutResultWithoutSplits);
int breakPointToFix = Integer.MIN_VALUE;
for (Integer breakPoint : new ArrayList<>(breakPoints)) {
if (breakPoint <= amountOfRowsThatFitWithoutSplit) {
breakPoints.remove(breakPoint);
if (breakPoint < amountOfRowsThatFitWithoutSplit && breakPoint > breakPointToFix) {
breakPointToFix = breakPoint;
}
}
}
return breakPointToFix;
}
private void forceAreaBreak(int rowIndex) {
rowIndex++;
if (rowIndex > rows.size()) {
return;
}
for (CellRenderer cellRenderer : rows.get(rowIndex)) {
if (cellRenderer != null) {
cellRenderer.getChildRenderers()
.add(0, new AreaBreakRenderer(new AreaBreak(AreaBreakType.NEXT_PAGE)));
break;
}
}
}
private static int getAmountOfRows(LayoutResult layoutResult) {
if (layoutResult.getSplitRenderer() == null) {
return 0;
}
return ((SplitTableAtSpecificRowRenderer) layoutResult.getSplitRenderer()).rows.size();
}
}
}
C#
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using iText.IO.Util;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Layout;
using iText.Layout.Properties;
using iText.Layout.Renderer;
namespace iText.Samples.Sandbox.Tables
{
public class SplitRowAtSpecificRow
{
public static readonly string DEST = "results/sandbox/tables/split_row_at_specific_row.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new SplitRowAtSpecificRow().ManipulatePdf(DEST);
}
private void ManipulatePdf(String dest)
{
Table table = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
table.SetWidth(550);
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, new PageSize(612, 242));
for (int i = 0; i < 40; i++)
{
Cell cell;
if (i == 20)
{
cell = new Cell().Add(new Paragraph("Multiple\n\n\n\nLines"));
}
else
{
cell = new Cell().Add(new Paragraph(i.ToString()));
}
table.AddCell(cell);
}
table.SetNextRenderer(new SplitTableAtSpecificRowRenderer(table, new List<int>()
{
// first break occurs on row 4
4,
// break at row 7 gets ignored because if we break at 9 it still fits the page
7, 9,
// last break occurs at 25 the other rows get rendered as normal
25,
// break point 36 does not break because the remaining rows still fit the page
36,
// break point 50 gets ignored because there are not enough rows
50
}
));
doc.Add(table);
doc.Close();
}
class SplitTableAtSpecificRowRenderer : TableRenderer
{
private readonly List<int> breakPoints;
private int amountOfRowsThatAreGoingToBeRendered = 0;
public SplitTableAtSpecificRowRenderer(Table modelElement, List<int> breakPoints) : base(modelElement)
{
this.breakPoints = breakPoints;
}
public override IRenderer GetNextRenderer()
{
return new SplitTableAtSpecificRowRenderer((Table)modelElement, this.breakPoints);
}
public override LayoutResult Layout(LayoutContext layoutContext)
{
LayoutResult result = null;
while (result == null)
{
result = AttemptLayout(layoutContext, this.breakPoints);
}
for (var index = 0; index < this.breakPoints.Count; index++)
{
this.breakPoints[index] -= amountOfRowsThatAreGoingToBeRendered;
}
return result;
}
private LayoutResult AttemptLayout(LayoutContext layoutContext, List<int> breakPoints)
{
LayoutResult layoutResult = base.Layout(layoutContext);
if (layoutResult.GetStatus() == LayoutResult.FULL || breakPoints.Count == 0)
{
this.amountOfRowsThatAreGoingToBeRendered = GetAmountOfRows(layoutResult);
return layoutResult;
}
int breakPointToFix = CalculateBreakPoint(layoutContext);
if (breakPointToFix >= 0)
{
ForceAreaBreak(breakPointToFix);
this.amountOfRowsThatAreGoingToBeRendered = breakPointToFix - 1;
return null;
}
return layoutResult;
}
private int CalculateBreakPoint(LayoutContext layoutContext)
{
LayoutResult layoutResultWithoutSplits = AttemptLayout(layoutContext, new List<int>());
if (layoutResultWithoutSplits == null)
{
return int.MinValue;
}
int amountOfRowsThatFitWithoutSplit = GetAmountOfRows(layoutResultWithoutSplits);
int breakPointToFix = int.MinValue;
foreach (int breakPoint in new List<int>(breakPoints))
{
if (breakPoint <= amountOfRowsThatFitWithoutSplit)
{
breakPoints.Remove(breakPoint);
if (breakPoint < amountOfRowsThatFitWithoutSplit && breakPoint > breakPointToFix)
{
breakPointToFix = breakPoint;
}
}
}
return breakPointToFix;
}
private void ForceAreaBreak(int rowIndex)
{
rowIndex++;
if (rowIndex > rows.Count)
{
return;
}
foreach (CellRenderer cellRenderer in rows[rowIndex])
{
if (cellRenderer != null)
{
cellRenderer.GetChildRenderers()
.Insert(0, new AreaBreakRenderer(new AreaBreak(AreaBreakType.NEXT_PAGE)));
break;
}
}
}
private static int GetAmountOfRows(LayoutResult layoutResult)
{
if (layoutResult.GetSplitRenderer() == null)
{
return 0;
}
return ((SplitTableAtSpecificRowRenderer)layoutResult.GetSplitRenderer()).rows.Count;
}
}
}
}
splitting
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.tables;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;
import java.io.File;
public class Splitting {
public static final String DEST = "./target/sandbox/tables/splitting.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new Splitting().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Paragraph p = new Paragraph("Test");
Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
for (int i = 1; i < 6; i++) {
table.addCell("key " + i);
table.addCell("value " + i);
}
for (int i = 0; i < 27; i++) {
doc.add(p);
}
doc.add(table);
for (int i = 0; i < 24; i++) {
doc.add(p);
}
table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
for (int i = 1; i < 6; i++) {
table.addCell("key " + i);
table.addCell("value " + i);
}
Table nesting = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();
Cell cell = new Cell().add(table);
cell.setBorder(Border.NO_BORDER);
// iText will make its best to process this cell on a single area
cell.setKeepTogether(true);
nesting.addCell(cell);
doc.add(nesting);
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Tables
{
public class Splitting
{
public static readonly string DEST = "results/sandbox/tables/splitting.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new Splitting().ManipulatePdf(DEST);
}
private void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Paragraph p = new Paragraph("Test");
Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
for (int i = 1; i < 6; i++)
{
table.AddCell("key " + i);
table.AddCell("value " + i);
}
for (int i = 0; i < 27; i++)
{
doc.Add(p);
}
doc.Add(table);
for (int i = 0; i < 24; i++)
{
doc.Add(p);
}
table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
for (int i = 1; i < 6; i++)
{
table.AddCell("key " + i);
table.AddCell("value " + i);
}
Table nesting = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
Cell cell = new Cell().Add(table);
cell.SetBorder(Border.NO_BORDER);
// iText will make its best to process this cell on a single area
cell.SetKeepTogether(true);
nesting.AddCell(cell);
doc.Add(nesting);
doc.Close();
}
}
}
splitting2
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.tables;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;
import java.io.File;
public class Splitting2 {
public static final String DEST = "./target/sandbox/tables/splitting2.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new Splitting2().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Paragraph p = new Paragraph("Test");
Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
for (int i = 1; i < 6; i++) {
table.addCell("key " + i);
table.addCell("value " + i);
}
for (int i = 0; i < 27; i++) {
doc.add(p);
}
doc.add(table);
for (int i = 0; i < 24; i++) {
doc.add(p);
}
table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
for (int i = 1; i < 6; i++) {
table.addCell("key " + i);
table.addCell("value " + i);
}
table.setKeepTogether(true);
doc.add(table);
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Tables
{
public class Splitting2
{
public static readonly string DEST = "results/sandbox/tables/splitting2.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new Splitting2().ManipulatePdf(DEST);
}
private void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Paragraph p = new Paragraph("Test");
Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
for (int i = 1; i < 6; i++)
{
table.AddCell("key " + i);
table.AddCell("value " + i);
}
for (int i = 0; i < 27; i++)
{
doc.Add(p);
}
doc.Add(table);
for (int i = 0; i < 24; i++)
{
doc.Add(p);
}
table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
for (int i = 1; i < 6; i++)
{
table.AddCell("key " + i);
table.AddCell("value " + i);
}
table.SetKeepTogether(true);
doc.Add(table);
doc.Close();
}
}
}
tablesplittest
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.tables;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.LineSeparator;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.properties.VerticalAlignment;
import java.io.File;
public class TableSplitTest {
public static final String DEST = "./target/sandbox/tables/tables_split_test.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new TableSplitTest().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, new PageSize(595, 842));
doc.setMargins(55, 15, 35, 15);
ILineDrawer line = new SolidLine(2);
line.setColor(ColorConstants.LIGHT_GRAY);
LineSeparator tableEndSeparator = new LineSeparator(line);
tableEndSeparator.setMarginTop(10);
String[] header = new String[]{"Header1", "Header2", "Header3",
"Header4", "Header5"};
String[] content = new String[]{"column 1", "column 2",
"some Text in column 3", "Test data ", "column 5"};
Table table = new Table(UnitValue.createPercentArray(new float[] {3, 2, 4, 3, 2})).useAllAvailableWidth();
for (String columnHeader : header) {
Paragraph headerParagraph = new Paragraph(columnHeader)
.setFont(PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD))
.setFontSize(10);
Cell headerCell = new Cell()
.add(headerParagraph)
.setTextAlignment(TextAlignment.CENTER)
.setVerticalAlignment(VerticalAlignment.MIDDLE)
.setBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1))
.setPadding(8);
table.addHeaderCell(headerCell);
}
for (int i = 0; i < 15; i++) {
int j = 0;
for (String text : content) {
Paragraph paragraph = new Paragraph((i == 13 && j == 3) ? "Test data \n Test data \n Test data" : text)
.setFont(PdfFontFactory.createFont(StandardFonts.HELVETICA))
.setFontSize(10);
Cell cell = new Cell()
.add(paragraph)
.setBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1))
.setPaddingLeft(5)
.setPaddingTop(5)
.setPaddingRight(5)
.setPaddingBottom(5);
table.addCell(cell);
j++;
}
}
doc.add(table);
doc.add(tableEndSeparator);
for (int k = 0; k < 5; k++) {
Paragraph info = new Paragraph("Some title")
.setFont(PdfFontFactory.createFont(StandardFonts.HELVETICA))
.setFontSize(10)
.setMarginTop(12);
doc.add(info);
table = new Table(UnitValue.createPercentArray(new float[] {3, 2, 4, 3, 2})).useAllAvailableWidth();
table.setMarginTop(15);
for (String columnHeader : header) {
Paragraph paragraph = new Paragraph(columnHeader)
.setFont(PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD))
.setFontSize(10);
Cell headerCell = new Cell()
.add(paragraph)
.setTextAlignment(TextAlignment.CENTER)
.setVerticalAlignment(VerticalAlignment.MIDDLE)
.setBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1))
.setPaddingLeft(8)
.setPaddingTop(8)
.setPaddingRight(8)
.setPaddingBottom(8);
table.addHeaderCell(headerCell);
}
for (String text : content) {
Paragraph paragraph = new Paragraph(text)
.setFont(PdfFontFactory.createFont(StandardFonts.HELVETICA))
.setFontSize(10);
Cell cell = new Cell()
.add(paragraph)
.setBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1))
.setPaddingLeft(5)
.setPaddingTop(5)
.setPaddingRight(5)
.setPaddingBottom(5);
table.addCell(cell);
}
doc.add(table);
doc.add(tableEndSeparator);
}
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace iText.Samples.Sandbox.Tables
{
public class TableSplitTest
{
public static readonly string DEST = "results/sandbox/tables/tables_split_test.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new TableSplitTest().ManipulatePdf(DEST);
}
private void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, new PageSize(595, 842));
doc.SetMargins(55, 15, 35, 15);
ILineDrawer line = new SolidLine(2);
line.SetColor(ColorConstants.LIGHT_GRAY);
LineSeparator tableEndSeparator = new LineSeparator(line);
tableEndSeparator.SetMarginTop(10);
String[] header = {"Header1", "Header2", "Header3", "Header4", "Header5"};
String[] content = {"column 1", "column 2", "some Text in column 3", "Test data ", "column 5"};
Table table = new Table(
UnitValue.CreatePercentArray(new float[] {3, 2, 4, 3, 2})).UseAllAvailableWidth();
foreach (String columnHeader in header)
{
Paragraph headerParagraph = new Paragraph(columnHeader)
.SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD))
.SetFontSize(10);
Cell headerCell = new Cell()
.Add(headerParagraph)
.SetTextAlignment(TextAlignment.CENTER)
.SetVerticalAlignment(VerticalAlignment.MIDDLE)
.SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1))
.SetPadding(8);
table.AddHeaderCell(headerCell);
}
for (int i = 0; i < 15; i++)
{
int j = 0;
foreach (String text in content)
{
Paragraph paragraph = new Paragraph((i == 13 && j == 3) ? "Test data \n Test data \n Test data" : text)
.SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA))
.SetFontSize(10);
Cell cell = new Cell()
.Add(paragraph)
.SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1))
.SetPaddingLeft(5)
.SetPaddingTop(5)
.SetPaddingRight(5)
.SetPaddingBottom(5);
table.AddCell(cell);
j++;
}
}
doc.Add(table);
doc.Add(tableEndSeparator);
for (int k = 0; k < 5; k++)
{
Paragraph info = new Paragraph("Some title")
.SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA))
.SetFontSize(10)
.SetMarginTop(12);
doc.Add(info);
table = new Table(
UnitValue.CreatePercentArray(new float[] {3, 2, 4, 3, 2})).UseAllAvailableWidth();
table.SetMarginTop(15);
foreach (String columnHeader in header)
{
Paragraph paragraph = new Paragraph(columnHeader)
.SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD))
.SetFontSize(10);
Cell headerCell = new Cell()
.Add(paragraph)
.SetTextAlignment(TextAlignment.CENTER)
.SetVerticalAlignment(VerticalAlignment.MIDDLE)
.SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1))
.SetPaddingLeft(8)
.SetPaddingTop(8)
.SetPaddingRight(8)
.SetPaddingBottom(8);
table.AddHeaderCell(headerCell);
}
foreach (String text in content)
{
Paragraph paragraph = new Paragraph(text)
.SetFont(PdfFontFactory.CreateFont(StandardFonts.HELVETICA))
.SetFontSize(10);
Cell cell = new Cell()
.Add(paragraph)
.SetBorder(new SolidBorder(ColorConstants.LIGHT_GRAY, 1))
.SetPaddingLeft(5)
.SetPaddingTop(5)
.SetPaddingRight(5)
.SetPaddingBottom(5);
table.AddCell(cell);
}
doc.Add(table);
doc.Add(tableEndSeparator);
}
doc.Close();
}
}
}