How to save as a PDF in asp.net

Written by jon guenther
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to save as a PDF in asp.net
(Jupiterimages/Photos.com/Getty Images)

Creating and saving Adobe PDF (Portable Document Format) files within ASP.NET does not require expensive PDF creation software. One of the most popular methods to save content as a PDF in ASP.NET pages is a free and robust solution called iTextSharp. Importing a Dynamic Link Library (DLL) file and writing a few lines of code are all that's required to build content into pages that may be saved as PDFs.

Skill level:
Moderately Challenging

Other People Are Reading

Things you need

  • Microsoft Visual Studio or Visual Web Developer Express
  • iTextSharp.DLL file
  • Adobe Acrobat Reader

Show MoreHide

Instructions

    Create a New Web Project

  1. 1

    Click "File" and then "New Web Project" in Visual Studio or Visual Web Developer Express.

  2. 2

    Select "ASP.NET Web Application" from the list of installed templates.

  3. 3

    Enter a name of your choice in the "Name" textbox.

  4. 4

    Specify the location on disk in the "Location" textbox (you may also accept the default location).

  5. 5

    Click the "OK" button.

    Download and Import iTextSharp

  1. 1
  2. 2

    Click the green "Download Now" button for the itextsharp-5.0.4-dll.zip file.

  3. 3

    Unzip the file using your preferred program for working with .zip files. This will give you access to the itextsharp.dll file.

  4. 4

    Right-click the "References" folder in your Visual Studio web project and click "Add Reference..."

  5. 5

    Click the "Browse" tab, navigate to the folder where the itextsharp.dll file was unzipped and double-click it to add to the project.

    Create the Web Page Content

  1. 1

    Double-click the "Default.aspx" page in the Solution Explorer pane and click the "Design" button to switch to the designer view.

  2. 2

    Locate the "LinkButton" control under the "Standard" group in the Toolbox, and then double-click it to add the control to the page.

  3. 3

    Change the "Text" property of the LinkButton control to "Save as PDF."

    Write the Code

  1. 1

    Double-click the LinkButton control on the designer page to display the "Default.aspx.cs" code-behind file.

  2. 2

    Type the following at the top of the file:

    using System.IO;

    using iTextSharp.text;

    using iTextSharp.text.pdf;

  3. 3

    Between the "{" and "}" brackets of the LinkButton1_Click() event, type:

    Document mySavedPDF = new Document();

    FileStream fs = new FileStream(@"C:\myPDF.PDF", FileMode.Create);

    Paragraph my Text = new Paragraph("If you see this sentence then you know the sweetness of success!");

    Press the "Enter" key.

  4. 4

    Type the following:

    PdfWriter.GetInstance(mySavedPDF, fs);

    Press the "Enter" key.

  5. 5

    Type the following:

    mySavedPDF.Open();

    mySavedPDF.Add(myText);

    mySavedPDF.CloseDocument();

    Save and Test the Application

  1. 1

    Click the "Save All" icon on the Standard toolbar to save changes.

  2. 2

    Press "Ctrl + F5" on the keyboard to build and run the program.

  3. 3

    Click the "Save as PDF" link button.

  4. 4

    Navigate to the location on the computer hard drive where the myPDF.PDF file was specified to be saved.

  5. 5

    Double-click the file to open it and verify that the following is displayed in the body of the PDF document:

    "If you see this sentence then you know the sweetness of success!"

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.