c# - C# PDF表格在错误的位置添加行
问题描述
[由于对答案的误解而编辑]
我正在用 C# 做一个简单的程序,用 iText7 创建 PDF 文件。在此 PDF 中,我添加了一个表格,其第一个单元格从文件中的某个位置开始。
我不知道我是否正确设置了位置,但是每次我添加另一个单元格时tab.StartNewRow()
,生成的新表都会重新定位,将最后一个单元格作为位置参考,将先前添加的单元格从该点向上放置,而我想添加单元格从那时起。
我应该使用哪种方法?那是我的代码:以前我使用设置第一个表格单元格的位置tab1.SetFixedPosition(20, heigh, width);
然后,为了添加其他单元格:
if (mylistbox.Items.Count > 0)
{
tab1.AddCell("FIRST CELL");
tab1.StartNewRow();
for (int i = 0; i < mylistbox.Items.Count; i++)
{
tab1.AddCell(mylistbox.Items[i].ToString());
tab1.StartNewRow();
}
doc.Add(tab1);
}
[编辑#2]为了更好地解释我的问题
我必须放置 5 个表格,它们必须从某个点向下增长,在文档中以相同的距离、相同的高度和宽度放置。这张图片解释了它应该如何产生:
解决方案
在 WPF 应用程序中,我有ListBox
5 个项目,编号从 1 到 5。这应该与 WinForms 非常相似。
它CreatePercentArray
的大小等于一行中的列数。
一篇关于表格的有趣文章:链接
private void CreateListBoxTable(Document pdfDoc)
{
// Create an array where each item has an equal width, and use the entire pdf width
// The CreatePercentArray takes a size which is equal to the amount of columns in a row
// By using percentages, they will automatically adapt
// Use CreatePointArray for exacter measurements
var table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
if (!MyListBox.Items.IsEmpty)
{
foreach (var listBoxItem in MyListBox.Items)
{
table.AddCell(((ListBoxItem) listBoxItem).Content.ToString());
}
}
// Adds table to document
pdfDoc.Add(table);
// Closes document
pdfDoc.Close();
}
推荐阅读
- azure - Azure cosmos DB 文档大小约为 30KB 时速度慢
- php - 从 php 5 升级到 php 7 时数组到字符串的转换
- css - 为什么 css 变量在 @page 规则中不起作用
- php - Woocommerce 购物车商品折扣在结账时重置
- android - 谁用 Expo 编译应用程序
- razor-pages - 修复使用 sendgrid 发送验证电子邮件时剃刀页面中的错误 OperationCanceledException
- angular - 根据选中的选择框动态添加选择框和文本框的删除和获取数据
- msbuild - 错误 CS0012:“对象”类型是在未引用的程序集中定义的。您必须添加对程序集 'netstandard, Version=2.0.0.0 的引用
- ruby-on-rails - 将数组数组拆分,将 [0] 拆分为一个,将 [1] 拆分为另一个
- ionic-framework - 无法在 Ionic 的 For 循环中正确设置变量