【问题标题】:Formatting issue on datatable printing to pdf数据表打印到pdf的格式问题
【发布时间】:2014-04-23 10:19:40
【问题描述】:

我正在为我的应用程序使用 C#、ASP.NET、iTextSharp。 将数据表打印为 PDF 时出现格式问题。

我有两个数据表:dt 和 dt_child

    foreach (DataRow dr in dt.Rows)
        {
        foreach (DataColumn dc in dt.Columns)
            {
            stringWrite.Write(dr[dc]);
            }
            indentno=dr["REF"].ToString();
            dt_child = getDataTablebyProcedure("ChildProcedure",ref);
            stringWrite = getStringWriterbyDataTable(dt_child, stringWrite);
        }

代码运行良好。我有格式问题。 图为目前的显示方式。

我想要

1:要显示的dt的标题行。目前未显示。

2:dt行以表格形式显示。目前我正在迭代每一列中的行。

【问题讨论】:

  • 我是否建议您最有可能在 Visual Studio 上进行编码,并且您很可能有水晶报告。您只需要认真地绘制报告然后2行代码。 myreportdocument.SetDatasource(myDataSet); 和 myreportdocument.Export("somepath",fileformat.PDF);(语法不准确但非常接近)

标签: c# asp.net printing datatable


【解决方案1】:

您需要为 PDF 设置 UI。

如果有帮助就试试这个

private void ExportDataToPDFTable()
    {
      Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
         try
          {
           string pdfFilePath = Server.MapPath(".") + "/pdf/myPdf.pdf";
           //Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
           PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));

           doc.Open();//Open Document to write

           Font font8 = FontFactory.GetFont("ARIAL", 7);

           //Write some content
            Paragraph paragraph = new Paragraph("Using ITextsharp I am going to show how to create simple table in PDF document ");

            DataTable dt = GetDataTable();

            if (dt != null)
                {
                 //Craete instance of the pdf table and set the number of column in that table
                 PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
                 PdfPCell PdfPCell = null;


                 //Add Header of the pdf table
                 PdfPCell = new PdfPCell(new Phrase(new Chunk("ID", font8)));
                 PdfTable.AddCell(PdfPCell);

                 PdfPCell = new PdfPCell(new Phrase(new Chunk("Name", font8)));
                 PdfTable.AddCell(PdfPCell);


                 //How add the data from datatable to pdf table
                 for (int rows = 0; rows < dt.Rows.Count; rows++)
                    {
                     for (int column = 0; column < dt.Columns.Count; column++)
                         {
                           PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8)));
                            PdfTable.AddCell(PdfPCell);
                            }
                        }

                        PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table

                        doc.Add(paragraph);// add paragraph to the document
                        doc.Add(PdfTable); // add pdf table to the document

                    }

                }
                catch (DocumentException docEx)
                {
                    //handle pdf document exception if any
                }
                catch (IOException ioEx)
                {
                    // handle IO exception
                }
                catch (Exception ex)
                {
                    // ahndle other exception if occurs
                }
                finally
                {
                    //Close document and writer
                    doc.Close();

       }
 }

礼貌 Add table into existing PDF using iTExtsharp

供参考 http://www.codeproject.com/Tips/573907/Generating-PDF-using-ItextSharp-with-Footer-in-Csh

【讨论】:

  • 我需要它是动态的。我有很多专栏。我需要用更好的东西替换foreach (DataColumn dc in dt.Columns)stringWrite.Write(dr[dc]);}。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
相关资源
最近更新 更多