C#和iText7,以绝对值设置列宽
我必须在c#和iText7库中实现如下内容: 我用以下代码实现了它:C#和iText7,以绝对值设置列宽,c#,pdf,itext7,C#,Pdf,Itext7,我必须在c#和iText7库中实现如下内容: 我用以下代码实现了它: // doc is the document created in the Main function public static void CreatePdf(Document doc) { // Initialize document Document document = doc; // a float array for columns wid
// doc is the document created in the Main function
public static void CreatePdf(Document doc) {
// Initialize document
Document document = doc;
// a float array for columns width
float[] colwidths = { 1, 3 };
// creates table with a number of col equals to how many numbers are in float array
Table table = new Table(UnitValue.CreatePercentArray(colwidths) );
table.SetWidth(523);
table.AddCell(new Cell(1,2).Add(new Paragraph("The table title goes here")));
// creates a cell that is 3rows and 1col large; vertically centers the text
table.AddCell(new Cell(3, 1).Add(new Paragraph("here goes the Text1"))
.SetVerticalAlignment(VerticalAlignment.MIDDLE) );
// adds 3 rows in the 2nd column
table.AddCell("row 1");
table.AddCell("row 2");
table.AddCell("row 3");
document.Add(table);
document.Close();
}
这样我决定第二列的宽度是第一列的3倍
但是我想用绝对值,这样我可以更精确地添加列和行,也就是说,我知道总宽度是523点,所以我希望第一列宽123点,第二列宽400点
- 我该怎么做
- 如果我把第一列宽100点,第二列宽300点,右边会留下空白吗
表
有一个接受浮点[]
的构造函数,您可以在其中指定点的列宽:
Table table = new Table(new float[] { 123, 400 });
除非您明确设置宽度(使用SetWidth
),否则生成的表宽度将是所提供列的总和,在这种情况下,每列的大小将按比例调整
例如,如果您打电话:
table.SetWidth(1046);
得到的列宽将分别加倍到226点和800点。Ahh谢谢!!现在我明白了。。。还有一个问题,一个分数是多少?因为我必须以厘米为单位设置长度,所以我找到了一个mmmerstopoint()函数,但它不能完美地完成这项工作。我最终手动编辑了尺寸,直到它符合我的需要。不过,我在这里发布了这个函数,供可能需要它的人使用:
private static float mmmerstopoints(float value){return(value/25.4f)*72f;}