iText 7 - 设置单元格宽度
iText 7 - set width of cells
我正在使用 iText 7 生成内部带有 table 的 PDF。
我想定义单元格的大小。
例如我有这个:
我想减少带有数字(07、08 ...)的列并增加其他列。
以下是我创建 table 的方法:
int nbColumns = 7 + planning.size() *4;
Table table = new Table(nbColumns);
table.setWidthPercent(100);
table.addCell(createCell(""));
table.addCell(createCell("Col1"));
table.addCell(createCell("Col2"));
DateFormat hourFormat = new SimpleDateFormat("HH", Locale.FRENCH);
for(Date hourDate : planning){
table.addCell(new Cell(1,4).setTextAlignment(TextAlignment.CENTER).add(hourFormat.format(hourDate)).setFont(regular).setFontSize(10));
}
table.addCell(createCell("Long"));
table.addCell(createCell("A"));
table.addCell(createCell("B"));
table.addCell(createCell(""));
我尝试使用 Cell 的方法 setWidth
,但它没有任何作用。
我尝试在创建时定义列宽 table :
float[] colWidths = {1,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,2,2,2};
Table table = new Table(colWidths);
但它会在 header 个单元格中造成多行混乱。
你能帮我吗?
如果 float[] colWidths = {1,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,2,2,2};
是一个数组,其中包含您想要的每一列的相对宽度,请将它们转换为 UnitValue
百分比数组,并使用它构造您的 table:
Table table = new Table(UnitValue.createPercentArray(colWidths));
如果您希望 table 的绝对宽度高于为列传递的宽度,请使用 Table#setFixedLayout()
。
运行 and/or 对以下代码进行实验以查看每种不同类型的声明的效果可能会有所帮助:
public void createPdf(String dest) throws IOException, FileNotFoundException{
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
float tableWidth = 100f;
//Using defaults
doc.add(new Paragraph("default/auto-layout"));
doc.add(new Paragraph("Using a float[]"));
float[] colWidths = {1,2,3,4};
Table table = new Table(colWidths);
table.setWidthPercent(tableWidth);
fillTable(table);
doc.add(table);
doc.add(new Paragraph("Using a UnitValue[] point array"));
colWidths=new float[]{20f,40f,80f,160f};
table = new Table(UnitValue.createPointArray(colWidths));
table.setWidth(tableWidth);
fillTable(table);
doc.add(table);
doc.add(new Paragraph("Using a UnitValue[] percent array"));
colWidths=new float[]{1,2,3,4};
table = new Table(UnitValue.createPercentArray(colWidths));
table.setWidthPercent(tableWidth);
fillTable(table);
doc.add(table);
doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
//Using fixedLayout
doc.add(new Paragraph("Using fixedLayout"));
doc.add(new Paragraph("Using a float[]"));
colWidths =new float[]{1,2,3,4};
table = new Table(colWidths);
table.setFixedLayout();
table.setWidthPercent(tableWidth);
fillTable(table);
doc.add(table);
doc.add(new Paragraph("Using a UnitValue[] point array"));
colWidths=new float[]{20f,40f,80f,160f};
table = new Table(UnitValue.createPointArray(colWidths));
table.setWidthPercent(tableWidth);
table.setFixedLayout();
fillTable(table);
doc.add(table);
doc.add(new Paragraph("Using a UnitValue[] percent array"));
colWidths=new float[]{1,2,3,4};
table = new Table(UnitValue.createPercentArray(colWidths));
table.setWidthPercent(tableWidth);
table.setFixedLayout();
fillTable(table);
doc.add(table);
doc.close();
}
public void fillTable(Table table){
table.addCell("Water");
table.addCell("Fire");
table.addCell("Heaven");
table.addCell("As I'm grounded here on");
table.addCell("It's waving oceans are calling");
table.addCell("is burning me deep inside");
table.addCell("can wait for me");
table.addCell("Earth");
}
我没有足够的声誉来发表评论,所以我添加了 C# 版本作为附加答案。还针对更高版本的 IText7 进行了更新,因为不推荐使用 SetWidthPercentage。
public static void CreatePdf(String dest)
{
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
float tableWidth = 100f;
//Using defaults
doc.Add(new Paragraph("default/auto-layout"));
doc.Add(new Paragraph("Using a float[]"));
float[] colWidths = { 1, 2, 3, 4 };
Table table = new Table(colWidths);
table.SetWidth(UnitValue.CreatePercentValue(tableWidth));
fillTable(table);
doc.Add(table);
doc.Add(new Paragraph("Using a UnitValue[] point array"));
colWidths = new float[] { 20f, 40f, 80f, 160f };
table = new Table(UnitValue.CreatePointArray(colWidths));
table.SetWidth(300); //20 + 40 + 80 + 160
fillTable(table);
doc.Add(table);
doc.Add(new Paragraph("Using a UnitValue[] percent array"));
colWidths = new float[] { 1, 2, 3, 4 };
table = new Table(UnitValue.CreatePercentArray(colWidths));
table.SetWidth(UnitValue.CreatePercentValue(tableWidth));
fillTable(table);
doc.Add(table);
doc.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
//Using fixedLayout
doc.Add(new Paragraph("Using fixedLayout"));
doc.Add(new Paragraph("Using a float[]"));
colWidths = new float[] { 1, 2, 3, 4 };
table = new Table(colWidths);
table.SetFixedLayout();
table.SetWidth(UnitValue.CreatePercentValue(tableWidth));
fillTable(table);
doc.Add(table);
doc.Add(new Paragraph("Using a UnitValue[] point array"));
colWidths = new float[] { 20f, 40f, 80f, 160f };
table = new Table(UnitValue.CreatePointArray(colWidths));
table.SetWidth(UnitValue.CreatePercentValue(tableWidth));
table.SetFixedLayout();
fillTable(table);
doc.Add(table);
doc.Add(new Paragraph("Using a UnitValue[] percent array"));
colWidths = new float[] { 1, 2, 3, 4 };
table = new Table(UnitValue.CreatePercentArray(colWidths));
table.SetWidth(UnitValue.CreatePercentValue(tableWidth));
table.SetFixedLayout();
fillTable(table);
doc.Add(table);
doc.Close();
}
public static void fillTable(Table table)
{
table.AddCell("Water");
table.AddCell("Fire");
table.AddCell("Heaven");
table.AddCell("As I'm grounded here on");
table.AddCell("It's waving oceans are calling");
table.AddCell("is burning me deep inside");
table.AddCell("can wait for me");
table.AddCell("Earth");
}
我正在使用 iText 7 生成内部带有 table 的 PDF。
我想定义单元格的大小。
例如我有这个:
我想减少带有数字(07、08 ...)的列并增加其他列。
以下是我创建 table 的方法:
int nbColumns = 7 + planning.size() *4;
Table table = new Table(nbColumns);
table.setWidthPercent(100);
table.addCell(createCell(""));
table.addCell(createCell("Col1"));
table.addCell(createCell("Col2"));
DateFormat hourFormat = new SimpleDateFormat("HH", Locale.FRENCH);
for(Date hourDate : planning){
table.addCell(new Cell(1,4).setTextAlignment(TextAlignment.CENTER).add(hourFormat.format(hourDate)).setFont(regular).setFontSize(10));
}
table.addCell(createCell("Long"));
table.addCell(createCell("A"));
table.addCell(createCell("B"));
table.addCell(createCell(""));
我尝试使用 Cell 的方法 setWidth
,但它没有任何作用。
我尝试在创建时定义列宽 table :
float[] colWidths = {1,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,2,2,2};
Table table = new Table(colWidths);
但它会在 header 个单元格中造成多行混乱。
你能帮我吗?
如果 float[] colWidths = {1,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,2,2,2};
是一个数组,其中包含您想要的每一列的相对宽度,请将它们转换为 UnitValue
百分比数组,并使用它构造您的 table:
Table table = new Table(UnitValue.createPercentArray(colWidths));
如果您希望 table 的绝对宽度高于为列传递的宽度,请使用 Table#setFixedLayout()
。
运行 and/or 对以下代码进行实验以查看每种不同类型的声明的效果可能会有所帮助:
public void createPdf(String dest) throws IOException, FileNotFoundException{
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
float tableWidth = 100f;
//Using defaults
doc.add(new Paragraph("default/auto-layout"));
doc.add(new Paragraph("Using a float[]"));
float[] colWidths = {1,2,3,4};
Table table = new Table(colWidths);
table.setWidthPercent(tableWidth);
fillTable(table);
doc.add(table);
doc.add(new Paragraph("Using a UnitValue[] point array"));
colWidths=new float[]{20f,40f,80f,160f};
table = new Table(UnitValue.createPointArray(colWidths));
table.setWidth(tableWidth);
fillTable(table);
doc.add(table);
doc.add(new Paragraph("Using a UnitValue[] percent array"));
colWidths=new float[]{1,2,3,4};
table = new Table(UnitValue.createPercentArray(colWidths));
table.setWidthPercent(tableWidth);
fillTable(table);
doc.add(table);
doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
//Using fixedLayout
doc.add(new Paragraph("Using fixedLayout"));
doc.add(new Paragraph("Using a float[]"));
colWidths =new float[]{1,2,3,4};
table = new Table(colWidths);
table.setFixedLayout();
table.setWidthPercent(tableWidth);
fillTable(table);
doc.add(table);
doc.add(new Paragraph("Using a UnitValue[] point array"));
colWidths=new float[]{20f,40f,80f,160f};
table = new Table(UnitValue.createPointArray(colWidths));
table.setWidthPercent(tableWidth);
table.setFixedLayout();
fillTable(table);
doc.add(table);
doc.add(new Paragraph("Using a UnitValue[] percent array"));
colWidths=new float[]{1,2,3,4};
table = new Table(UnitValue.createPercentArray(colWidths));
table.setWidthPercent(tableWidth);
table.setFixedLayout();
fillTable(table);
doc.add(table);
doc.close();
}
public void fillTable(Table table){
table.addCell("Water");
table.addCell("Fire");
table.addCell("Heaven");
table.addCell("As I'm grounded here on");
table.addCell("It's waving oceans are calling");
table.addCell("is burning me deep inside");
table.addCell("can wait for me");
table.addCell("Earth");
}
我没有足够的声誉来发表评论,所以我添加了 C# 版本作为附加答案。还针对更高版本的 IText7 进行了更新,因为不推荐使用 SetWidthPercentage。
public static void CreatePdf(String dest)
{
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdfDoc = new PdfDocument(writer);
Document doc = new Document(pdfDoc);
float tableWidth = 100f;
//Using defaults
doc.Add(new Paragraph("default/auto-layout"));
doc.Add(new Paragraph("Using a float[]"));
float[] colWidths = { 1, 2, 3, 4 };
Table table = new Table(colWidths);
table.SetWidth(UnitValue.CreatePercentValue(tableWidth));
fillTable(table);
doc.Add(table);
doc.Add(new Paragraph("Using a UnitValue[] point array"));
colWidths = new float[] { 20f, 40f, 80f, 160f };
table = new Table(UnitValue.CreatePointArray(colWidths));
table.SetWidth(300); //20 + 40 + 80 + 160
fillTable(table);
doc.Add(table);
doc.Add(new Paragraph("Using a UnitValue[] percent array"));
colWidths = new float[] { 1, 2, 3, 4 };
table = new Table(UnitValue.CreatePercentArray(colWidths));
table.SetWidth(UnitValue.CreatePercentValue(tableWidth));
fillTable(table);
doc.Add(table);
doc.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
//Using fixedLayout
doc.Add(new Paragraph("Using fixedLayout"));
doc.Add(new Paragraph("Using a float[]"));
colWidths = new float[] { 1, 2, 3, 4 };
table = new Table(colWidths);
table.SetFixedLayout();
table.SetWidth(UnitValue.CreatePercentValue(tableWidth));
fillTable(table);
doc.Add(table);
doc.Add(new Paragraph("Using a UnitValue[] point array"));
colWidths = new float[] { 20f, 40f, 80f, 160f };
table = new Table(UnitValue.CreatePointArray(colWidths));
table.SetWidth(UnitValue.CreatePercentValue(tableWidth));
table.SetFixedLayout();
fillTable(table);
doc.Add(table);
doc.Add(new Paragraph("Using a UnitValue[] percent array"));
colWidths = new float[] { 1, 2, 3, 4 };
table = new Table(UnitValue.CreatePercentArray(colWidths));
table.SetWidth(UnitValue.CreatePercentValue(tableWidth));
table.SetFixedLayout();
fillTable(table);
doc.Add(table);
doc.Close();
}
public static void fillTable(Table table)
{
table.AddCell("Water");
table.AddCell("Fire");
table.AddCell("Heaven");
table.AddCell("As I'm grounded here on");
table.AddCell("It's waving oceans are calling");
table.AddCell("is burning me deep inside");
table.AddCell("can wait for me");
table.AddCell("Earth");
}