ASP.NET - write pdf file using asp.net - Asked By Anto Bilson on 26-Sep-11 08:01 AM



hi,

when im going to write .aspx page content  to pdf file format ,i got an exception like that

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

how can i solve the problem

my code:

 try
      {       
        this.Get_Student_Details();

        // Let's output HTML of GridView
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition",
            "attachment;filename=student_Details.pdf");

        StringWriter swriter = new StringWriter();
        HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
        HtmlForm frm = new HtmlForm();      
        this.div_id.Parent.Controls.Add(frm);  
        frm.Controls.Add(this.div_id);
        frm.RenderControl(hwriter);     
        Response.Write(swriter.ToString());
        Response.End();
      }
      catch (Exception ex)
      {
      }
Suchit shah replied to Anto Bilson on 26-Sep-11 08:05 AM

Try this PDF Duo .Net converting component for converting http://www.duodimension.com/ application without using additional dlls.

You can pass the HTML string or file, or stream to generate the PDF. Use the code below (Example C#):

string file_html = @"K:\hdoc.html";
string file_pdf = @"K:\new.pdf";
try
{
DuoDimension.HtmlToPdf conv = new DuoDimension.HtmlToPdf();
conv.OpenHTML(file_html);
conv.SavePDF(file_pdf);
textBox4.Text = "C# Example: Converting succeeded";
}

Info + C#/VB examples you can find at: http://www.duodimension.com/html_pdf_asp.net/component_html_pdf.aspx




Most HTML to PDF converter relies on IE to do the HTML parsing and rendering. This can break when user updates their IE. Here is one that does not rely on IE:

http://www.essentialobjects.com/Products/EOPdf/Default.aspx

The code is something like this:

EO.Pdf.HtmlToPdf.ConvertHtml(htmlText, pdfFileName);

Like many other converters, you can pass text, file name, or Url. The result can be saved into a file or a stream.

smr replied to Anto Bilson on 26-Sep-11 08:22 AM
HI

try this

This short article explains how to create PDF documents from ASP.NET web pages using this free library: http://sourceforge.net/projects/itextsharp/.

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
partial class ShowPDF : System.Web.UI.Page
{
 
  protected void Page_Load(object sender, System.EventArgs e)
  {
    if (Request.QueryString["id"] == 1) {
      ShowHello();
    } else {
      ShowTable();
    }
  }
  public ShowPDF()
  {
    Load += Page_Load;
  }
}


public void ShowTable()
{
  Document doc = new Document();
  PdfWriter.GetInstance(doc, new FileStream(Request.PhysicalApplicationPath + "\\2.pdf", FileMode.Create));
  doc.Open();
  Table table = new Table(3);
  table.BorderWidth = 1;
  table.BorderColor = new Color(0, 0, 255);
  table.Padding = 3;
  table.Spacing = 1;
  Cell cell = new Cell("header");
  cell.Header = true;
  cell.Colspan = 3;
  table.AddCell(cell);
  cell = new Cell("example cell with colspan 1 and rowspan 2");
  cell.Rowspan = 2;
  cell.BorderColor = new Color(255, 0, 0);
  table.AddCell(cell);
  table.AddCell("1.1");
  table.AddCell("2.1");
  table.AddCell("1.2");
  table.AddCell("2.2");
  table.AddCell("cell test1");
  cell = new Cell("big cell");
  cell.Rowspan = 2;
  cell.Colspan = 2;
  cell.HorizontalAlignment = Element.ALIGN_CENTER;
  cell.VerticalAlignment = Element.ALIGN_MIDDLE;
  cell.BackgroundColor = new Color(192, 192, 192);
  table.AddCell(cell);
  table.AddCell("cell test2");
  doc.Add(table);
  doc.Close();
  Response.Redirect("~/2.pdf");
}



follow
http://www.codeproject.com/KB/aspnet/Creating_PDF_documents_in.aspx
http://weblogs.asp.net/sreejukg/archive/2011/03/07/create-pdf-document-using-itextsharp-in-asp-net-4-0-and-memorymappedfile.aspx
Anoop S replied to Anto Bilson on 26-Sep-11 08:22 AM
you can do like this

byte
[] bytes = System.IO.File.ReadAllBytes("myfile.pdf");
 
System.IO.File.WriteAllBytes("myfile.pdf", bytes);

Easiest way:
 
byte[] buffer;
using (Stream stream = new IO.FileStream("file.pdf"))
{
   buffer = new byte[stream.Length - 1];
   stream.Read(buffer, 0, buffer.Length);
}
 
using (Stream stream = new IO.FileStream("newFile.pdf"))
{
   stream.Write(buffer, 0, buffer.Length);
}
dipa ahuja replied to Anto Bilson on 26-Sep-11 08:32 AM
protected void Page_Load(object sender, EventArgs e)
{
    string strS = @"d:\test.PDF";
    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + strS);
    Response.TransmitFile(strS);
    Response.End();
    Response.Flush();
    Response.Clear();
}
Anto Bilson replied to Anto Bilson on 27-Sep-11 10:04 AM
 int columnCount = 2;// grdUser.Columns.Count;
          int rowCount = 8;// grdUser.Rows.Count;
          int tableRows = rowCount + 3;
          iTextSharp.text.Table grdTable =
          new iTextSharp.text.Table(columnCount, tableRows);
          grdTable.BorderWidth = 1;
          grdTable.BorderColor = new iTextSharp.text.Color(0, 0, 255);
          grdTable.Cellpadding = 5;
          grdTable.Cellspacing = 5;
          Cell c1 = new Cell("Exporting paging enabled GridView to PDF example");
          //c1.Header = true; c1.Colspan = 2;
          //grdTable.AddCell(c1);
          Cell c2 = new Cell("By amiT jaiN , amit_jain_online@yahoo.com");
          //c2.Colspan = 2;
          //grdTable.AddCell(c2);
          grdTable.AddCell("First Name");
          grdTable.AddCell(tbl.Rows[0]["FirstName"].ToString());
          grdTable.AddCell("Last Name");
          grdTable.AddCell(tbl.Rows[0]["LastName"].ToString());
          grdTable.AddCell("User Name");
          grdTable.AddCell(tbl.Rows[0]["UserName"].ToString());
          grdTable.AddCell("Password");
          grdTable.AddCell(tbl.Rows[0]["Password"].ToString());
          grdTable.AddCell("Gender");
          grdTable.AddCell(tbl.Rows[0]["Gender"].ToString());
          grdTable.AddCell("Email");
          grdTable.AddCell(tbl.Rows[0]["Email_id"].ToString());
          grdTable.AddCell("University Name");
          grdTable.AddCell(tbl.Rows[0]["UniversityName"].ToString());
          grdTable.AddCell("Phone");
          grdTable.AddCell(tbl.Rows[0]["PhoneNumber"].ToString());
          //for (int rowCounter = 0;
          //rowCounter < rowCount; rowCounter++)
          //{
          //    for (int columnCounter = 0; columnCounter < columnCount; columnCounter++)
          //    {
          //      string strValue = grdUser.Rows[rowCounter].Cells[columnCounter].Text;
          //      grdTable.AddCell(strValue);
          //    }
          //}
          Document Doc = new Document();
          PdfWriter.GetInstance(Doc, Response.OutputStream);
          Doc.Open(); Doc.Add(grdTable);
          Doc.Close();
          Response.ContentType = "application/pdf";
          Response.AddHeader
          ("content-disposition", "attachment; filename=" + tbl.Rows[0]["FirstName"].ToString() + "");
          Response.End();