Pdf 如何使用iText创建一个表,使所有列的宽度相同,而不考虑内容?
我已经用iText编写了一些C#代码来创建一个表,该表呈现数据源中的数字。我遇到的问题是,表中大部分都包含一位数,但偶尔也会出现类似于Pdf 如何使用iText创建一个表,使所有列的宽度相同,而不考虑内容?,pdf,itext,Pdf,Itext,我已经用iText编写了一些C#代码来创建一个表,该表呈现数据源中的数字。我遇到的问题是,表中大部分都包含一位数,但偶尔也会出现类似于100的数字,当使用100呈现PDF时,表中包含100的列会变宽,相邻列会变窄 我已经做了一个设计决定,我希望所有的列都是相同的宽度,无论内容如何。我已经使用Cell.SetWidth指定了列的宽度,但是仍然没有得到想要的结果 我需要做什么来确保表单元格呈现一定的宽度,而不管其内容如何 下面是呈现三行(每行48个单元格)的示例代码。我只希望单元格呈现相同的大小,而
100
的数字,当使用100
呈现PDF时,表中包含100
的列会变宽,相邻列会变窄
我已经做了一个设计决定,我希望所有的列都是相同的宽度,无论内容如何。我已经使用Cell.SetWidth指定了列的宽度,但是仍然没有得到想要的结果
我需要做什么来确保表单元格呈现一定的宽度,而不管其内容如何
下面是呈现三行(每行48个单元格)的示例代码。我只希望单元格呈现相同的大小,而不考虑内容。现在,如果文本可能溢出到通话之外或被剪辑,这没关系,但我关心的是使单元格大小相同
using System;
using System.Collections.Generic;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream(@"c:\temp\test2.pdf", FileMode.Create);
PdfWriter writer = new PdfWriter(fs);
PdfDocument pdfDocument = new PdfDocument(writer);
pdfDocument.SetDefaultPageSize(PageSize.LETTER.Rotate());
Document document = new Document(pdfDocument);
document.SetMargins(18, 18, 18, 18);
Table table = new Table(CreateRepeatingValueArray(48, 16f))
.SetBorder(new SolidBorder(1));
// Fill our array of values with all zeros, and then make one value 100 to
// show how column widths are perturbed.
var values = CreateRepeatingValueArray<byte>(48 * 3, 0);
values[2] = 100;
foreach (byte value in values)
{
Cell cell = new Cell(1, 1)
.Add(new Paragraph(Convert.ToString(value)))
.SetFontSize(9)
.SetWidth(16f)
.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
table.AddCell(cell);
}
document.Add(table);
document.Close();
}
static T[] CreateRepeatingValueArray<T>(int length, T value)
{
List<T> list = new List<T>(length);
for (int i = 0; i < length; i += 1)
{
list.Add(value);
}
return list.ToArray();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用iText.Kernel.Geom;
使用iText.Kernel.Pdf;
使用iText.Layout;
使用iText.Layout.Borders;
使用iText.Layout.Element;
名称空间控制台AP3
{
班级计划
{
静态void Main(字符串[]参数)
{
FileStream fs=newfilestream(@“c:\temp\test2.pdf”,FileMode.Create);
PdfWriter writer=新的PdfWriter(fs);
PdfDocument PdfDocument=新的PdfDocument(writer);
pdfDocument.SetDefaultPageSize(PageSize.LETTER.Rotate());
文件=新文件(PDF文件);
文件。设置页边距(18、18、18、18);
Table Table=新表(CreateRepeatingValueArray(48,16f))
.新订单(新SolidBorder(1));
//用全零填充我们的值数组,然后将一个值设为100
//显示列宽是如何受到干扰的。
var values=CreateRepeatingValueArray(48*3,0);
数值[2]=100;
foreach(值中的字节值)
{
单元格=新单元格(1,1)
.Add(新段落(Convert.ToString(value)))
.SetFontSize(9)
.SetWidth(16f)
.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
表2.AddCell(cell);
}
文件。添加(表);
document.Close();
}
静态T[]CreateRepeatingValueArray(整数长度,T值)
{
列表=新列表(长度);
对于(int i=0;i
我找到了解决这个问题的方法。我创建一个段落
对象来保存每个数字,然后设置段落
对象的宽度。到目前为止,这似乎效果很好