Craeate a PDF file

Last post 07-06-2007 5:03 PM by fchivu. 40 replies.

Sort Posts:

  • Re: Craeate a PDF file

    07-26-2004, 11:29 AM
    • Member
      10 point Member
    • krussell
    • Member since 01-07-2003, 1:21 PM
    • Posts 2
    Thanks SomeNewKid2,

    That worked.
    Now, I have another problem. I have two web pages.
    the first web page has a hyperlink to the second web page. this hyperlink target is set to new. The second page opens the pdf file in a new window(using Adobe Reader 6.0) and it displays correctly.

    when i close the second page, i get an iexplorer.exe error- the instruction at '' referenced memory at '0x00000000'. the memory could not be "read".

    thanks for your help.
    Russell171
  • Re: Craeate a PDF file

    07-26-2004, 11:44 AM
    • All-Star
      45,864 point All-Star
    • SomeNewKid
    • Member since 08-10-2003, 12:16 AM
    • Western Australia
    • Posts 8,027
    > when i close the second page, i get an iexplorer.exe error

    I'd guess this is related to simultaneously closing both the IE window and its embedded instance of Acrobat Reader.

    Is this is a public webpage that I can test? If not, try testing your page on another browser or (ideally) another computer with a different version of Acrobat Reader.
    Alister
  • Re: Craeate a PDF file

    07-26-2004, 2:28 PM
    • Contributor
      2,575 point Contributor
    • rx
    • Member since 08-29-2003, 10:16 PM
    • Los Angeles, CA
    • Posts 515
    Hi,
    How do I create and open PDF on the fly without create and save it to the folder using iTextSharp?

    Thanks a lot!
    Richard
    Richard Xin
    MCAD(charter member),MCDBA,MCSD
    Web Site: www.richardxin.com (For ASP.Net tips and Code snippet)
  • Re: Craeate a PDF file

    07-26-2004, 3:10 PM
    • All-Star
      45,864 point All-Star
    • SomeNewKid
    • Member since 08-10-2003, 12:16 AM
    • Western Australia
    • Posts 8,027
    > How do I create and open PDF on the fly without create and save it to the folder

    Rather than stream the PDF to a file on the server, we instead stream the PDF to the server's memory. Once we've dynamically created the PDF on-the-fly, we can then send it to the user's browser. This way, the PDF is never written to the server's file system.

    Following is the codebehind needed to achieve this (for exactly the same ASPX page as in my downloadable sample). This is in C#. If you want VB.NET, and cannot convert it yourself, let me know.

    using System;
    
    using System.IO;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using iTextSharp;
    using iTextSharp.text;
    using iTextSharp.text.pdf;

    namespace test
    {
    public class StreamPDFcs: Page
    {

    protected Label Message;
    protected TextBox ParagraphTextbox;
    protected Button Submit;

    protected void Submit_Click( object sender, EventArgs e )
    {
    // Here, we just pick out the text the user has entered
    // If they have entered some text, we pass it along to
    // the Sub StreamNewPDF
    if (ParagraphTextbox.Text == string.Empty )
    {
    Message.Text = "Please enter some text";
    }
    else
    {
    StreamNewPDF( ParagraphTextbox.Text );
    }
    }

    private void StreamNewPDF( string paragraphText )
    {
    try
    {
    // Rather than streaming the PDF to a new file on the server,
    // we instead stream it to the server's memory browser.
    MemoryStream pdfStream = new MemoryStream();

    // Initialise the PDF we are about to create
    Document pdfDoc = new Document( PageSize.A4 );
    PdfWriter.getInstance( pdfDoc, pdfStream );

    // Now, create the PDF
    pdfDoc.Open();
    pdfDoc.Add( new Paragraph( paragraphText ) );
    pdfDoc.Close();

    // Finalize the stream
    pdfStream.Flush();
    pdfStream.Close();

    // Success!
    // Stream the result to the user's browser

    // First, convert the memorystream into an array of bytes
    byte[] byteArray = pdfStream.ToArray();

    // Second, clear all content buffered in the Response
    Response.Clear();

    // Third, add the necessary headers
    Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf");
    Response.AddHeader("Content-Length", byteArray.Length.ToString());

    // Fourth, set the MIME type
    Response.ContentType = "application/pdf";

    // Finally, send the PDF data to the user's browser
    Response.BinaryWrite(byteArray);
    }
    catch
    {
    // Oh oh! Something went wrong.
    // If this occurs, you can comment out the Try/Catch lines, so that the
    // actual exception message appears in the browser
    Message.Text = "Sorry. PDF creation was unsuccessful.";
    }
    }

    protected override void OnInit(EventArgs e)
    {
    this.Submit.Click += new System.EventHandler(this.Submit_Click);
    base.OnInit(e);
    }

    }

    }
    Alister
  • Re: Craeate a PDF file

    07-26-2004, 5:07 PM
    • Contributor
      2,575 point Contributor
    • rx
    • Member since 08-29-2003, 10:16 PM
    • Los Angeles, CA
    • Posts 515
    Thanks, I tried your code, I had some problems
    1. Sometimes, I got "file not exists" error
    2. The "file download" messagebox apperaed twice.
    3. I tried to put html string as input of StreamNewPDF, the pdf only display html, what I want to do is exporting an HTML table (runat=server) to pdf.

    Thanks,


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim _strWtr As New StringWriter
    Dim _htmlWtr As New HtmlTextWriter(_strWtr)
    Me.tblSIRView.RenderControl(_htmlWtr)
    'tblSIRView is HTML table (runat=server)
    StreamNewPDF(_strWtr.ToString)
    End Sub


    Private Sub StreamNewPDF(ByVal paragraphText As String)
    Try
    'stream to the server's memory browser.
    Dim _pdfStream As MemoryStream = New MemoryStream
    Dim pdfDoc As New Document(PageSize.A4)
    PdfWriter.getInstance(pdfDoc, _pdfStream)
    ' Now, create the PDF
    pdfDoc.Open()
    pdfDoc.Add(New Paragraph(paragraphText))
    pdfDoc.Close()
    'Finalize the stream
    _pdfStream.Flush()
    _pdfStream.Close()

    'Stream the result to the user's browser
    '1. convert the memorystream into an array of bytes
    Dim byteArray As Byte() = _pdfStream.ToArray()
    '2. clear all content buffered in the Response
    Response.Clear()
    '3. add the necessary headers
    Response.AddHeader("Content-Disposition", "attachment; filename=test.pdf")
    Response.AddHeader("Content-Length", byteArray.Length.ToString())
    '4. set the MIME type
    Response.ContentType = "application/pdf"
    '5. Finally, send the PDF data to the user's browser
    Response.BinaryWrite(byteArray)
    Catch ex As Exception
    Dim s As String = ex.Message
    'TODO: handle error here..
    End Try
    End Sub
    Richard Xin
    MCAD(charter member),MCDBA,MCSD
    Web Site: www.richardxin.com (For ASP.Net tips and Code snippet)
  • Re: Craeate a PDF file

    07-27-2004, 5:00 AM
    • All-Star
      45,864 point All-Star
    • SomeNewKid
    • Member since 08-10-2003, 12:16 AM
    • Western Australia
    • Posts 8,027
    The samples I have provided were just to help developers get started with the iTextSharp library. As such, the samples were only ever intended to create bare-bones PDF files, just as a "proof of concept". (That is, proof that we can dynamically create PDF files with .NET, using a free component.)

    This forum thread is the wrong place for us to collectively develop a PDF component. However, each developer is free to create his or her own.

    Earlier in this thread I provided a link to a very basic HTML to PDF converter, and also a link to documentation for creating tables within iTextSharp ... which should help with your specific query about PDF tables.
    Alister
  • Re: Craeate a PDF file

    07-27-2004, 2:48 PM
    • Member
      10 point Member
    • krussell
    • Member since 01-07-2003, 1:21 PM
    • Posts 2
    SomeNewKid2,
    I found out why i was getting that error (the iexplorerer error). I did some more searching and found out in another forum (Adobe Forum) the following:
    1 - My explorer was crashing because I have the MSN Toolbar installed. I don't know if
    uninstalling it would fix it, but the next step corrected my issue.
    2 - (From Adobe Forum)
    it seems that you can solve this problem by deleting the following registry key:

    HKEY_CLASSES_ROOT\ CLSID\
    {CA8A9780-280D-11CF-A24D-444553540000}\
    InprocServer32\
    ThreadingModel=Apartment

    By deleting this key, you are telling Internet Explorer that the pdf control is a single-
    threaded component rather than apartment-threaded.
    COM will do some extra work synchronizing the calls to the control's
    objects which seems to solve the problem.

    Thanks for your help.

    I hope this helps the next iTextSharp developer.
    Russell171
  • Re: Craeate a PDF file

    07-30-2004, 9:35 AM
    • All-Star
      45,864 point All-Star
    • SomeNewKid
    • Member since 08-10-2003, 12:16 AM
    • Western Australia
    • Posts 8,027
    Thanks, Russell171, for providing that fix.
    Alister
  • Re: Craeate a PDF file

    07-06-2007, 5:03 PM
    • Member
      48 point Member
    • fchivu
    • Member since 02-02-2007, 12:08 PM
    • Posts 24
    HTML Source EditorWord wrap

    Hi,

    The HTML to PDF library for .NET  from http://www.dotnet-reporting.com or the HTML to PDF converter from http://www.winnovative-software.com is what you need. It's pure .NET library, it doesn't use a printer driver. There is also a free html to pdf converter application built on top of this library. The conversion can be done with only a few lines of code:

    PdfConverter pdfConverter = new PdfConverter();
    pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
    pdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait;
    pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal;
    pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = true;
    pdfConverter.PdfDocumentOptions.ShowFooter = false;
    pdfConverter.PdfDocumentOptions.ShowHeader = false;
    pdfConverter.LicenseFilePath = Server.MapPath(@"~/Bin");
    byte[] downloadBytes = pdfConverter.GetPdfFromUrlBytes(MyURL);  

     

    Regards,

    Razva

  • Re: Craeate a PDF file

    07-06-2007, 5:03 PM
    • Member
      48 point Member
    • fchivu
    • Member since 02-02-2007, 12:08 PM
    • Posts 24
    HTML Source EditorWord wrap

    Hi,

    The HTML to PDF library for .NET  from http://www.dotnet-reporting.com or the HTML to PDF converter from http://www.winnovative-software.com is what you need. It's pure .NET library, it doesn't use a printer driver. There is also a free html to pdf converter application built on top of this library. The conversion can be done with only a few lines of code:

    PdfConverter pdfConverter = new PdfConverter();
    pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
    pdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait;
    pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal;
    pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = true;
    pdfConverter.PdfDocumentOptions.ShowFooter = false;
    pdfConverter.PdfDocumentOptions.ShowHeader = false;
    pdfConverter.LicenseFilePath = Server.MapPath(@"~/Bin");
    byte[] downloadBytes = pdfConverter.GetPdfFromUrlBytes(MyURL);  

     

    Regards,

    Razva

  • Re: Craeate a PDF file

    07-06-2007, 5:03 PM
    • Member
      48 point Member
    • fchivu
    • Member since 02-02-2007, 12:08 PM
    • Posts 24
    HTML Source EditorWord wrap

    Hi,

    The HTML to PDF library for .NET  from http://www.dotnet-reporting.com or the HTML to PDF converter from http://www.winnovative-software.com is what you need. It's pure .NET library, it doesn't use a printer driver. There is also a free html to pdf converter application built on top of this library. The conversion can be done with only a few lines of code:

    PdfConverter pdfConverter = new PdfConverter();
    pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
    pdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait;
    pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal;
    pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = true;
    pdfConverter.PdfDocumentOptions.ShowFooter = false;
    pdfConverter.PdfDocumentOptions.ShowHeader = false;
    pdfConverter.LicenseFilePath = Server.MapPath(@"~/Bin");
    byte[] downloadBytes = pdfConverter.GetPdfFromUrlBytes(MyURL);  

     

    Regards,

    Razva

Page 3 of 3 (41 items) < Previous 1 2 3