使用iTextSharp将TableLayoutPanels复制到pdf

时间:2017-04-27 19:52:55

标签: c# winforms pdf itext tablelayoutpanel

大家下午好。我有一个问题涉及在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的方法?

1 个答案:

答案 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。