大家下午好。我有一个问题涉及在tablelayoutpanel中获取数据并使用iTextSharp将其放入.pdf(除非有人知道更好的技术)。 tableLayout面板由1列组成,默认为1行,并且根据数据返回的内容动态添加行。
以下是我打印的内容:
private void btnPrint_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.Title = "Save file as...";
dialog.Filter = "Pdf File |*.pdf";
if (dialog.ShowDialog() == DialogResult.OK)
{
Document doc = new Document(PageSize.LETTER);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(dialog.FileName, FileMode.Create));
doc.Open();
Paragraph entry1 = new Paragraph("Hello World!");
//Page 1 Printing
PdfPTable LegendsForTable = new PdfPTable(this.tblPnlLayLFT.ColumnCount);
doc.Add(entry1);
doc.Close();
MessageBox.Show("File saved");
}
}
catch (Exception exception)
{
MessageBox.Show(@"ERROR: Issue encountered while trying to print. " + Environment.NewLine
+ @"Contact ITSupport with the following the following error" + Environment.NewLine
+ exception);
}
}
有没有人知道将tablelayoutpanel
复制到.pdf的方法?
答案 0 :(得分:0)
我能够自己解决这个问题。
步骤1是创建一个循环,遍历表格布局面板并将我想要的顺序放入列表中。
int reIterator = 1; int replicateIterator = 1;
List<string> table1List = new List<string>();
for (int counter = 0; counter < 6; counter++)
{
while (reIterator < 7)
{
string currentLabel = "LblRE" + reIterator + "R" + replicateIterator;
Label reLabel = this.Controls.Find(currentLabel, true).FirstOrDefault() as Label;
if (reLabel.Text != null)
{
table1List.Add(reLabel.Text);
reIterator = reIterator + 1;
}
else
{
table1List.Add(reLabel.Text = "");
reIterator = reIterator + 1;
}
}
//Builds next row
if (reIterator == 7)
{
replicateIterator = replicateIterator + 1;
reIterator = 1;
}
}
然后使用iTextSharp我可以循环使用列表并将数据添加到PDF。