【问题标题】:Using iTextSharp to copy TableLayoutPanels to pdf使用 iTextSharp 将 TableLayoutPanels 复制到 pdf
【发布时间】:2017-04-27 19:52:55
【问题描述】:

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

【问题讨论】:

  • “我有一个问题” - 究竟是哪个问题?
  • 我的道歉认为这很清楚。问题是有没有人可以将 tablelayoutpanel 复制到 .pdf 的方法?

标签: c# winforms pdf itext tablelayoutpanel


【解决方案1】:

我自己能解决这个问题。

第 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。

【讨论】:

    猜你喜欢
    • 2015-05-19
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 2015-05-07
    相关资源
    最近更新 更多