Domanda

I'm using C#, ASP.NET, iTextSharp for my application. I have formatting issues when I print my datatable to PDF.

I have two datatables: dt and 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);
        }

Code is working fine. I have an issue with formatting. The picture shows how it is currently displayed.format

I want

1: The header row of dt to be displayed. It is currently not displayed.

2: The row of dt to be displayed in table form. Currently I'm iterating rows in each column.

È stato utile?

Soluzione

You will need to set UI for PDF.

Try this if it helps

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();

       }
 }

Courtesy Add table into existing PDF using iTExtsharp

For reference http://www.codeproject.com/Tips/573907/Generating-PDF-using-ItextSharp-with-Footer-in-Csh

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top