PDFOne .NET
Powerful all-in-one PDF library for .NET
Compatibility
VS 2005/2008/2010/2012/2013

How To Create PDFs In An ASP.NET MVC Application

Using PDFOne .NET in an MVC3 web application.
By V. Subhash

PDFOne .NET can be used in "Win-Forms" and ASP.NET applications to generate and process PDF documents. You can also use PDFOne in ASP.NET MVC application. In this article, we will see how to use it in a sample ASP.NET MVC 3 web application.

In the following C# example, our MVC3 application will generate a simple HTML form that pretends to be a online money transfer application of a bank. After the form is posted, the application will generate a PDF purportedly containing the details of the transaction and send it down to the browser.

  1. Create a new ASP.NET MVC3 Web Application
    • Give the name "BankTransfer" to the solution.
    • Use the "Empty" template
  2. In Solution Explorer, add a reference to the Gnostice.PDFOne assembly.
  3. Add a new model named "TransferDetails." This will be our domain model.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace BankTransfer.Models
    {
      public class TransferDetails
      {
        public string PayeeName { get; set; }
        public string AccountNumber { get; set; }
        public string Amount { get; set; }
      }
    }
    
  4. Build the project.
  5. Add a new empty controller named "Home" (HomeController.cs). This will have a built-in Index() action method.
    public ActionResult Index() {
      return View();
    }
    
  6. In the controller, add namespaces for our domain model and the PDFOne assembly.
    using BankTransfer.Models;
    using Gnostice.PDFOne;
    
  7. Add a new strongly-typed view (index.cshtml) linked to our model for this Index() action.
  8. Create a data-entry form for the end-user, as shown below.
    @model BankTransfer.Models.TransferDetails
    
    @{
        Layout = null;
    }
    
    <html>
    <head>
        <title>Bank of Money - Initiate Money Transfer</title>
    </head>
    <body>
        <div>
    
        <h1>Initiate Money Transfer</h1>
        @using (Html.BeginForm())
        {
            <p>Enter payee account #: @Html.TextBox("AccountNumber")</p>
            <p>Enter payee name #: @Html.TextBox("PayeeName")</p>
            <p>Enter amount #: @Html.TextBox("Amount")</p>
            <input type="submit" value="Make Transfer" />
        }
        </div>
    </body>
    </html>
    
  9. Switch to the controller and add a HttpGet attribute to the existing Index() action method. This will make the controller use the method only to handle simple HTTP GET requests.
    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }
    
  10. Add the following overloaded Index() action and give it a HttpPost attribute. This will make the controller use this only method to process the "posted" form contents and generate the transaction statement.
    [HttpPost]
    public void Index(TransferDetails td)
    {
      // Create a PDF document
      PDFDocument doc = new PDFDocument("your-license-key");
      doc.WriteText("Bank of Money", 1, 1);
      doc.WriteText("---------------------------------", 1, 1.1f);
    
      // Render text
      PDFFont fntCourier = new PDFFont(StdType1Font.Courier, 16);
    
      doc.WriteText("Payee account #   :", fntCourier, 1, 1.4f);
      doc.WriteText("Payee name        :", fntCourier, 1, 1.7f);
      doc.WriteText("Amount transferred:", fntCourier, 1, 2f);
      doc.WriteText("Transaction #     :", fntCourier, 1, 2.3f);
      doc.WriteText("Date and Time #   :", fntCourier, 1, 2.6f);
    
      doc.WriteText(td.AccountNumber, fntCourier, 4, 1.4f);
      doc.WriteText(td.PayeeName, fntCourier, 4, 1.7f);
      doc.WriteText("Rs. " + td.Amount, fntCourier, 4, 2f);
      doc.WriteText((new Random()).Next(199999, 599999).ToString(), fntCourier, 4, 2.3f);
      doc.WriteText(DateTime.Now.ToString(), fntCourier, 4, 2.7f);
    
      // Write the document to the browser
      this.Response.Clear();
      this.Response.ContentType = "application/pdf";
      doc.Save(this.Response.OutputStream);
      doc.Close();
    }
    
    The values that the action method writes to the PDF are bound to the properties of the TransferDetails instance passed to it.
  11. Build the project and run it. This will open a browser and display the form.
  12. Fill up the form and submit it. The application will then display the PDF inside the browser or let you download it.

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.