ASP.NET MVC Web Applications and Foxit Quick PDF Library
Foxit Quick PDF Library can be used in ASP.NET MVC Web Application projects to add rich PDF features to web applications. It works in much the same way as it does for regular desktop or server applications.
This tutorial is done using Visual Studio 2013 and C#, but the steps are mostly the same if you are using Visual Basic and Visual Studio 2008 or 2012. It demonstrates how to create an ASP.NET MVC Web Application with Foxit Quick PDF Library to create a PDF file with some text in it.
Contents
Steps
- Open Visual Studio and go to File > New > Projects. Then under Visual C# > Web select ASP.NET MVC 4 Web Application.
- Choose Basic MVC 4 project template and view engine as Razor.
- In Solutions Explorer right-click on your project and select Add ASP.NET Folder > App_Code. This is where we will put the import files for the DLL edition of Foxit Quick PDF Library.
- In the Solutions Explorer right-click on the App_Code folder and select Add Existing Item from the menu. Navigate to the “C:\Program Files (x86)\Debenu\PDF Library\DLL\Import\CSharp” folder and select the DebenuPDFLibraryDLL1113.cs file and click Add.
- Under App_Code folder right click on the newly added class file and go to its properties. Then change its Build Action from Content to Compile.
- Right-click on the project again and and select New Folder from the menu and name it Lib. This is where the Foxit Quick PDF Library 32-bit and 64-bit DLL’s will be stored. Make sure that you have downloaded and installed Foxit Quick PDF Library. The default installation location on most machines is “C:\Program Files (x86)\Debenu\PDF Library”. Click on the DLL folder.
- In the Solutions Explorer right-click on the Lib folder and select Add Existing Item from the menu. Navigate to “C:\Program Files (x86)\Debenu\PDF Library\DLL\” and change the file types that the dialog is showing (bottom right) to include “All Files“. Now select the the 32-bit and 64-bit DLL’s from the DLL folder and click on Add.
- Right click on the Controller folder and add a new controller called HomeController.
- In the HomeController.cs file add the DebenuPDFLibraryDLL1113 namespace to the using directives section at the top of your file.
- Right click under the Index action and click on Add View.
- Set the View name be Index and click on Add button.
- Add the following code to Index.cshtml page which will be found under Views > Home.
- Add the following code under HomeController partial class. Also change the version number, we have used version 11.13 in this tutorial but it will work with other versions, just change the version numbers as required. Also create the CreatePdf action in HomeController class.
- Add static class Output in HomeController.cs file.
- Under CreatePdf action add the following code.
- Add the highlighted code under Index action.
- Now add you trial licence key by navigating to “C:\Program Files (x86)\Debenu\PDF Library\ TRIAL_LICENSE_KEY.txt“. Enter the licence key in the area highlighted for the UnlockKey function.
- You will run into permissions issues if you try to use functions such as SaveToFile to save files into the root directory of your project. For testing create a “test” folder in the root project directory and then you can save a test file to it like this: DPL.SaveToFile(Server.MapPath(“~/test/HelloWorldDll.pdf”));.
- Run the Web Application and you will see the library version used. Then click on the Create pdf button will create a HelloWorldDll.pdf file under the “test” folder with “Hello World” text written in it.
Sample Code
This is only code for the HomeController.cs file the code for other files should be taken from the screenshots as outlined above.
// HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DebenuPDFLibraryDLL1113;
namespace DebenuPdfMvcWebApplication.Controllers
{
public class HomeController : Controller
{
// DLL Library setup
PDFLibrary DPL;
//Library Version setup
int DPLVer = 1113;
//Output class instance
public HomeController()
{
}
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult CreatePdf()
{
// Different filename for 32-bit and 64-bit DLL
string DLLprefix = "DebenuPDFLibraryDLL";
string DLL64prefix = "DebenuPDFLibrary64DLL";
string dllName;
// Debenu Quick PDF Library is provided in 32-bit and 64-bit verisons. We can
// automatically check which should be used based on what arcitecture the website
// project is targeting.
// Check to see if IntPtr size is 4. If 4 then it's 32-bit, if 8 then it is 64-bit.
if (IntPtr.Size == 4)
{
dllName = DLLprefix + DPLVer.ToString("D4") + ".DLL"; // 32 bits
}
else
{
dllName = DLL64prefix + DPLVer.ToString("D4") + ".DLL"; // 64 bits
}
// Load the library from the Lib folder
DPL = new PDFLibrary(Server.MapPath("~/Lib/" + dllName));
// Check to see if library loaded successfully, LibraryVersion can be called before UnlockKey function is used
if (DPL.LibraryVersion() != "")
{
// LibraryVersion has returned a result so lets display it on our web page
string LibVer = DPL.LibraryVersion();
Output.LibraryVersion = LibVer;
}
else
{
// If this is returned then the library was not successfully initialized.
Output.LibraryVersion = "version not found";
}
// Now we can unlock the library using our
// license key to get the library fully working
if (DPL.UnlockKey("...insert_license_key_here...") != 0)
{
Output.DllUnlocked = "Debenu Quick PDF Library was unlocked successfully.";
// Lets do a test to see if we have permissions to
// save files locally to disk. Please note: due to
// permissions restrictions you won't be able to save
// files to the root folder of your website project.
DPL.DrawText(100, 100, "Hello world");
DPL.SaveToFile(Server.MapPath("~/test/HelloWorldDll.Pdf"));
}
else
{
// If this is returned one of the following
// things has happened:
// 1. The library was not successfully initialized.
// 2. The license key is not valid (either it is an
// expired trial license key or it is a license key
// for an older version of the library.
Output.DllUnlocked = "Debenu Quick PDF Library could not be unlocked.";
}
return RedirectToAction("Index");
}
}
public static class Output
{
public static string LibraryVersion { get; set; }
public static string DllUnlocked { get; set; }
}
}
This article refers to a deprecated product. If you are looking for support for Foxit PDF SDK, please click here.
Updated on May 16, 2022