You must Sign In to post a response.
  • Category: ASP.NET

    Export asp.net page or gridview to PDF?

    Please help me to exporting the contents of the GridView or asp.net page in to a PDF format in asp.net application?
    Thanks
  • #271931
    use ABCpdf.NET

  • #271945
    http://july-code.blogspot.com/2008/06/export-gridview-to-pdf.html

    you can download this itextsharp dll from
    http://sourceforge.net/projects/itextsharp/
    sample
    using iTextSharp.text;
    using iTextSharp.text.pdf;

    protected void Page_Load(object sender, EventArgs e)
    {
    ExportToPDF();
    }

    private void ExportToPDF()
    {
    Document document = new Document(PageSize.A4, 0, 0, 50, 50);
    System.IO.MemoryStream msReport = new System.IO.MemoryStream();

    try {
    // creation of the different writers
    PdfWriter writer = PdfWriter.GetInstance(document, msReport);

    // we add some meta information to the document
    document.AddAuthor("eJuly");
    document.AddSubject("Export to PDF");

    document.Open();

    iTextSharp.text.Table datatable = new iTextSharp.text.Table(7);

    datatable.Padding = 2;
    datatable.Spacing = 0;

    float[] headerwidths = { 6, 20, 32, 18, 8, 8, 8 };
    datatable.Widths = headerwidths;

    // the first cell spans 7 columns
    Cell cell = new Cell(new Phrase("System Users Report", FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.BOLD)));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    cell.Leading = 30;
    cell.Colspan = 7;
    cell.Border = Rectangle.NO_BORDER;
    cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.Gray);
    datatable.AddCell(cell);

    // These cells span 2 rows
    datatable.DefaultCellBorderWidth = 1;
    datatable.DefaultHorizontalAlignment = 1;
    datatable.DefaultRowspan = 2;
    datatable.AddCell("No.");
    datatable.AddCell(new Phrase("Full Name", FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.NORMAL)));
    datatable.AddCell("Address");
    datatable.AddCell("Telephone No.");

    // This cell spans the remaining 3 columns in 1 row
    datatable.DefaultRowspan = 1;
    datatable.DefaultColspan = 3;
    datatable.AddCell("Just Put Anything");

    // These cells span 1 row and 1 column
    datatable.DefaultColspan = 1;
    datatable.AddCell("Col 1");
    datatable.AddCell("Col 2");
    datatable.AddCell("Col 3");

    datatable.DefaultCellBorderWidth = 1;
    datatable.DefaultRowspan = 1;

    for (int i = 1; i < 20; i++) {
    datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
    datatable.AddCell(i.ToString());
    datatable.AddCell("This is my name.");
    datatable.AddCell("I have a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long address.");
    datatable.AddCell("0123456789");

    datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
    datatable.AddCell("No");
    datatable.AddCell("Yes");
    datatable.AddCell("No");
    }

    document.Add(datatable);
    }
    catch (Exception e) {
    Console.Error.WriteLine(e.Message);
    }

    // we close the document
    document.Close();

    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
    Response.ContentType = "application/pdf";
    Response.BinaryWrite(msReport.ToArray());
    Response.End();
    }

  • #271989
    Hi Milind Mahajan,
    Thanks Mahajan,
    I don't know what is this "ABCpdf.NET"

    Hi purushothaman,
    I don't want to use third party control or dll.
    Can you help me to implement pdf export custom control?

    Thanks and Regards,

  • #272030
    Then I think you can use
    Interop.Acrobat.dll
    for exporting to pdf

  • #272496
    Hi Purushothaman,
    Thank you very much for your information.
    I will try and let you know the details.
    Thanks

  • #274793
    using iTextSharp.text;
    using iTextSharp.text.pdf;

    protected void Page_Load(object sender, EventArgs e)
    {
    ExportToPDF();
    }

    private void ExportToPDF()
    {
    Document document = new Document(PageSize.A4, 0, 0, 50, 50);
    System.IO.MemoryStream msReport = new System.IO.MemoryStream();

    try {
    // creation of the different writers
    PdfWriter writer = PdfWriter.GetInstance(document, msReport);

    // we add some meta information to the document
    document.AddAuthor("eJuly");
    document.AddSubject("Export to PDF");

    document.Open();

    iTextSharp.text.Table datatable = new iTextSharp.text.Table(7);

    datatable.Padding = 2;
    datatable.Spacing = 0;

    float[] headerwidths = { 6, 20, 32, 18, 8, 8, 8 };
    datatable.Widths = headerwidths;

    // the first cell spans 7 columns
    Cell cell = new Cell(new Phrase("System Users Report", FontFactory.GetFont(FontFactory.HELVETICA, 16, Font.BOLD)));
    cell.HorizontalAlignment = Element.ALIGN_CENTER;
    cell.Leading = 30;
    cell.Colspan = 7;
    cell.Border = Rectangle.NO_BORDER;
    cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.Gray);
    datatable.AddCell(cell);

    // These cells span 2 rows
    datatable.DefaultCellBorderWidth = 1;
    datatable.DefaultHorizontalAlignment = 1;
    datatable.DefaultRowspan = 2;
    datatable.AddCell("No.");
    datatable.AddCell(new Phrase("Full Name", FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.NORMAL)));
    datatable.AddCell("Address");
    datatable.AddCell("Telephone No.");

    // This cell spans the remaining 3 columns in 1 row
    datatable.DefaultRowspan = 1;
    datatable.DefaultColspan = 3;
    datatable.AddCell("Just Put Anything");

    // These cells span 1 row and 1 column
    datatable.DefaultColspan = 1;
    datatable.AddCell("Col 1");
    datatable.AddCell("Col 2");
    datatable.AddCell("Col 3");

    datatable.DefaultCellBorderWidth = 1;
    datatable.DefaultRowspan = 1;

    for (int i = 1; i < 20; i++) {
    datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT;
    datatable.AddCell(i.ToString());
    datatable.AddCell("This is my name.");
    datatable.AddCell("I have a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long address.");
    datatable.AddCell("0123456789");

    datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER;
    datatable.AddCell("No");
    datatable.AddCell("Yes");
    datatable.AddCell("No");
    }

    document.Add(datatable);
    }
    catch (Exception e) {
    Console.Error.WriteLine(e.Message);
    }

    // we close the document
    document.Close();

    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
    Response.ContentType = "application/pdf";
    Response.BinaryWrite(msReport.ToArray());
    Response.End();
    }

  • #325941
    Thanks a lot for this


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.