Creating Pdf file through binarydata in asp.net

Last post 07-28-2009 8:47 AM by Md Eqbal Ansari. 8 replies.

Sort Posts:

  • Creating Pdf file through binarydata in asp.net

    07-28-2009, 2:48 AM
    • Member
      point Member
    • anandraaz
    • Member since 09-04-2008, 11:06 AM
    • Posts 7

     hi ,

    I am developing a aspx page where users upload a document.i am saving it as a binary data in database.now i want to create a pdffile using itextsharp.how to create a pdffile using the binary data from database.can anyone help me in this issue.

     

    regards

    anandraj

  • Re: Creating Pdf file through binarydata in asp.net

    07-28-2009, 3:04 AM
    • Contributor
      2,980 point Contributor
    • santa_1975
    • Member since 06-30-2008, 6:20 AM
    • Posts 496

    Hi,

    Hope the below URL helps.

    http://blog.rubypdf.com/2006/10/25/itextsharpitext-example-in-aspnet/


  • Re: Creating Pdf file through binarydata in asp.net

    07-28-2009, 3:05 AM
    • Participant
      1,470 point Participant
    • Heloril
    • Member since 10-30-2007, 1:38 PM
    • Belgium
    • Posts 255

     Hi,

    You can do it directly in the page in the page load event of a page:

    protected void Page_Load(object sender, EventArgs e)
            {
                //Report issue 
                if (IssueList != null)
                {
                    byte[] pdfFile = IssueListReport.GeneratePdf(IssueList);
    
                    Response.ContentType = "application/pdf";
                    Response.OutputStream.Write(pdfFile, 0, pdfFile.Length);
                }
            }

    Or by using an httpHandler.

     

    /// <summary>
        /// Summary description for $codebehindclassname$
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        public class Handler1 : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                byte[] pdfFile = IssueListReport.GeneratePdf(IssueList);
    context. Response.ContentType = "application/pdf"; context.OutputStream.Write(pdfFile, 0, pdfFile.Length); } public bool IsReusable { get { return false; } } }

    Regards,
    Helori.

    Mark as Answer if this reply helps you

    My blog: http://www.notesfor.net/
  • Re: Creating Pdf file through binarydata in asp.net

    07-28-2009, 6:42 AM
    • Member
      point Member
    • anandraaz
    • Member since 09-04-2008, 11:06 AM
    • Posts 7

     

    hi  thanks for ur reply..

    i tried bud its not working..and most importtant thing its not creating pdf file.

    regards

    AnandRaj

  • Re: Creating Pdf file through binarydata in asp.net

    07-28-2009, 6:50 AM
    • Star
      9,218 point Star
    • hans_v
    • Member since 01-29-2007, 4:03 PM
    • Posts 1,596

    anandraaz:
    I am developing a aspx page where users upload a document.
     

    What kind of documents are we dealing with?

  • Re: Creating Pdf file through binarydata in asp.net

    07-28-2009, 7:01 AM
    • Member
      point Member
    • anandraaz
    • Member since 09-04-2008, 11:06 AM
    • Posts 7

    hi ..

    We r uploading pdf files and saving as binarydata in database.now i want to create a pdf file with uploaded data(ie saved binary data in database) usinf itextsharp.Plz anyone help me..

    regards

    AnandRaj

     

  • Re: Creating Pdf file through binarydata in asp.net

    07-28-2009, 7:36 AM
    • Star
      9,218 point Star
    • hans_v
    • Member since 01-29-2007, 4:03 PM
    • Posts 1,596

    In that case, you don't need iTextSharp. Create a generic handler (.ashx file), retrieve the binary data from the database in the ProcessRequest Method and send it to the client.

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim pdfID As Integer = Convert.ToInt32(context.Request.QueryString("pdfID"))
        Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
    
            Const SQL As String = "SELECT [pdfData] FROM [pdfTable] WHERE [pdfID] = @pdfID"
            Dim myCommand As New SqlCommand(SQL, myConnection)
            myCommand.Parameters.AddWithValue("@pdfID", pdfID)
    
            myConnection.Open()
            Dim myReader As SqlDataReader = myCommand.ExecuteReader
    
            If myReader.Read Then
              context.Response.ContentType = "application/pdf"
              context.Response.BinaryWrite(myReader("pdfData"))
              context.Response.AddHeader("Content-Disposition", "attachment; filename=download.pdf" 
            End If
    
            myReader.Close()
            myConnection.Close()
        End Using
    End Sub


     To retrieve the pdf, call the ashx file like:

    <asp:HyperLink ID="HyperLink1" runat="server" navigetUrl="~/pdf.ashx?pdfID=1" Text="Download" />

     

  • Re: Creating Pdf file through binarydata in asp.net

    07-28-2009, 8:31 AM
    • Member
      point Member
    • anandraaz
    • Member since 09-04-2008, 11:06 AM
    • Posts 7

    hi,

    Thanks for ur reply..to display binary data in pdf file is not a problem.see my requirement is to restrict the print option

    of a pdffile .so what im trying to do is  creating a new pdf file with no menu bar and will display in a iframe.creating pdf file with hiding menu bar and file tool bar is possible with iTextSahrp.The problem is creating pdffile with binarydata stored in database.

    Any suggestions  and help ll be appriciated...

    regards

    Anandraj

     

  • Re: Creating Pdf file through binarydata in asp.net

    07-28-2009, 8:47 AM
    • Member
      78 point Member
    • Md Eqbal Ansari
    • Member since 05-05-2009, 1:43 AM
    • New Delhi
    • Posts 58

    public bool CreatePDF(string path)

        {

     

            bool bRet = false;

            Document document = new Document();

            try

            {

                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));

                document.Open();

     

                //document.AddAuthor("author name");

                document.AddCreationDate();

               

     

                iTextSharp.text.Table aTable = new iTextSharp.text.Table(3);

                aTable.BorderWidth = 0;

                aTable.Cellspacing = 0;

                aTable.Cellpadding = 1;

                aTable.Width = 100;

                aTable.TableFitsPage = true;

     

                Chunk Heading = new Chunk("Data value to be print on the PDF file");

                Heading.Font.SetStyle(Font.BOLD);

                Heading.Font.Size = 12;

                Heading.Font.SetStyle(Font.UNDERLINE);

                Cell tblTop = new Cell(Heading);

                tblTop.BorderWidth = 0;

                tblTop.Colspan = 3;              

                tblTop.HorizontalAlignment = 1;            

                aTable.AddCell(tblTop);

     

     

                Chunk Detail = new Chunk("Data value to be print on the PDF file");

                Detail.Font.Size = 10;

                Detail.Font.SetStyle(Font.BOLD);

                Cell tblHeader = new Cell(Detail);

                tblHeader.BorderWidth = 0;

                tblHeader.Colspan = 3;

                tblHeader.HorizontalAlignment = 1;

                aTable.AddCell(tblHeader);

    // similarly add cells as much you want.........

    document.Add(aTable);

     

                bRet = true;

            }

            catch (DocumentException de)

            {

                Response.Write(de.Message);

            }

            catch (IOException ioe)

            {

                Response.Write(ioe.Message);

            }

           

            document.Close();

            //if (bRet)

               // Response.Write(path + " has been created");

            return bRet;

        }

    try this..may be help you to generate pdf file.

    Eqbal
Page 1 of 1 (9 items)