1. Home
  2. Foxit PDF SDK
  3. Developer Guide for Foxit PDF SDK (.NET)

Developer Guide for Foxit PDF SDK (.NET)

Introduction to Foxit PDF SDK

Have you ever thought about building your own application that can do everything you want with PDF files? If your answer is “Yes”, congratulations! You just found the best solution in the industry that allows you to build stable, secure, efficient and full-featured PDF applications.

Why Foxit is your choice

Foxit is an Amazon-invested leading software provider of solutions for reading, editing, creating, organizing, and securing PDF documents. Customers choose Foxit products for the following reasons:

  • High performance – Very fast on PDF parsing, rendering and conversion.
  • Lightweight footprint – Do not exhaust system resource and deploys quickly.
  • Cross-platform support – Support Microsoft Windows, Linux etc.
  • Great value/affordability – Right features at right price with email and phone support.
  • Security – Safeguards confidential information.

In addition, Foxit products are fully supported by our dedicated support engineers if support and maintenance are purchased. Updates are released on a regular basis. Developers may focus more on their solution building rather than spending time on PDF specification. Foxit will be the right choice if you need solutions with excellent features and low cost!

Evaluation

Foxit PDF SDK allows users to download trial version to evaluate SDK. The trial version has no difference from a standard version except for the 30-day limitation trial period and the trail watermarks that will be generated on the PDF pages. After the evaluation period expires, customers should contact Foxit sales team and purchase licenses to continue using Foxit PDF SDK.

License

Developers should purchase licenses to use Foxit PDF SDK in their solutions. Licenses grant users permissions to release their applications based on PDF SDK libraries. However, users are prohibited to distribute any documents, sample codes, or source codes in the SDK released package to any third party without the permission from Foxit Software Incorporated.

About this guide

This guide is intended for the developers who need to integrate Foxit PDF .NET SDK into their own applications. It aims at introducing the installation package, and the usage of SDK.

Getting Started

It’s very easy to setup Foxit PDF SDK and see it in action! This guide will provide you with a brief introduction on how to integrate Foxit PDF SDK into the projects based on .NET Framework 4.0 or higher. The following sections introduce the contents of system requirements, the installation package as well as how to run a demo, and create your own project.

System Requirements

Windows XP, Vista, 7, 8 and 10 (32-bit, 64-bit)

Windows Server 2003, 2008 and 2012 (32-bit and 64-bit)

The release package includes a 32 bit version and native 64 bit version DLL library for Windows 32/64.

Visual Studio 2010/2015 installed with .NET Framework 4.0 or higher

Note: This guide provides support for Windows 8/10 classic, but not for Store App or UWP app.

What is in the package

Download Foxit PDF SDK zip for .NET package and extract it to a new directory “foxitpdfsdk_6_0_win_dotnet”, which is shown in Figure 2 1.

NOTE: the highlighted rectangle in the figure is just the version of Foxit PDF SDK. Here the SDK version is 6.0, so it shows 6_0 in the package name.

The release package contains the following folders:

docs: API references, quick start guide
lib: libraries and license files
samples: sample projects and demos

Figure 2-1

In the “examples” folder, there are two types of demos. “examples\simple_demo” contains more than 20 demos that cover a wide range of PDF applications. “examples\view_demo” contains a UI demo that realizes a lite PDF viewer.

How to run a demo

Simple demo projects provide examples to show .NET developers about how to effectively apply PDF SDK APIs to complete their applications.

To run a demo in Visual Studio 2010 or 2015 (expect security demo which will be introduced later), you can follow the steps below:

  1. Load the visual studio solution files “simple_demo_vs2010.sln” in the “examples\simple_demo” folder.
  2. Build all the demos by clicking “Build > Build Solution”. Alternatively, if you merely want to build a specific demo, you can right-click it and then choose “Build” or load the “*.csproj” file in the folder of a specific demo project and then build it.
  3. Figure 2-2
  4. Run a specific executable file, just double-click it.
    Some demos will generate output files (pdf, text or image files) to a folder named by the project name under “example\simple_demo\output_files\” folder.
    Note: If you want to see the detailed executing processes, you can run it in command line. Start “cmd.exe”, navigate to “examples\simple_demo\bin”, and run a specific executable file.

As for the security demo, you should install the certificates “foxit.cer” and “foxit_all.pfx” found in “examples\simple_demo\input_files” before running the demos.

  1. To install “foxit.cer”, double-click it to start the certificate import wizard. Then select “Install certificate… > Next > Next > Finish”.
  2. To install “foxit_all.pfx”, double-click it to start the certificate import wizard. Then select “Next > Next > (Type the password for the private key in the textbox) and click Next > Next > Finish”.
  3. Run the demo following the steps as the other demos.

View Demo

This view demo provides an example for .NET developers to realize a PDF reader using PDF SDK APIs.

To run the demo in Visual Studio 2010 or 2015, load “PDFReader_VS2010.sln” in the “examples\view_demo\PDFReader” folder, and then click “Debug > Start Without Debugging” to run it. After the demo starts, you will see the following window as shown in Figure 2 3.

Figure 2-3

Drag a PDF file to the above window (Figure 2-3) to open it. Browse the content by scrolling down or moving the PDF page by holding the left mouse button. A screenshot of the demo is shown in Figure 2 4.

You can click on File > Open or click the directory icon to open a PDF file, for example, we open a PDF document named “AboutFoxit.pdf” found in “examples\simple_demo\input_files” folder.
This demo provides the features like rendering a PDF document, zooming, page rotation, text selection and search, and page turning. For example, click the Next Page button to view the next page, which is shown in Figure 2 4.

Figure 2-4

How to create a simple project

In this section, we will show you how to use Foxit PDF .NET SDK to create a simple project that renders the first page of a PDF to a bitmap and saves it as a JPG image. Please follow the steps below:

  1. Open Visual Studio and create a new C# Console Application named “test_dotnet”.
  2. Copy the “lib” folder from the “foxitpdfsdk_6_0_win_dotnet” folder to the project “test_ dotnet” folder.
  3. Add Foxit PDF SDK dynamic library to References. In order to use Foxit PDF SDK APIs in the project, you must first add a reference to it.
  1. In Solution Explorer, right-click the “test_dotnet” project and click Add Reference…
  2. In the Add Reference dialog, click Browse tab, navigate to the “test_dotnet\lib\x64_vc10” or “test_dotnet\lib\x86_vc10” folder depending on your build configurations, select fsdk_dotnet.dll dynamic library, and then click OK.

Note: Please make sure that the “fsdk_dotnet.dll” architecture needs to match the platform target (Win32 or Win64) of the application.

  • Add “System.Drawing” item to References.
    Right-click the “test_dotnet” project and click Add Reference… In the Add Reference dialog, click .NET tab and select System.Drawing, and then click OK.
  • Add “fsdk.dll” as source file to the project.
    Right-click the “test_dotnet” project and click Add > Existing Item…, navigate to the “test_dotnet\lib\x64_vc10” or “test_dotnet\lib\x86_vc10” folder depending on your build configurations, select fsdk.dll dynamic library, and click Add.
  • Add using statement to the beginning of the “Program.cs”.
  • using System.Drawing;
    using foxit;
    using foxit.common;
    using foxit.common.fxcrt;
    using foxit.pdf;
  • Initialize Foxit PDF SDK library. It is necessary for apps to initialize Foxit PDF SDK using a license before calling any APIs. The trial license files can be found in the “libs” folder.
  • string sn = " ";
    string key = " ";
    ErrorCode error_code = Library.Initialize(sn, key);
    if (error_code != ErrorCode.e_ErrSuccess)
    {
       return;
    }

    Note: The value of “sn” can be got from “gsdk_sn.txt” (the string after “SN=”) and the value of “key” can be got from “gsdk_key.txt” (the string after “Sign=”)

  • Load a PDF document, and parse the first page of the document. Let us assume that you have already put a “Sample.pdf” to the “test_dotnet\test_dotnet” folder.
  • PDFDoc doc = new PDFDoc("..\..\Sample.pdf");
    error_code = doc.LoadW("");
    if (error_code != ErrorCode.e_ErrSuccess)
    {
        return;
    }
     
    // Get the first page of the document.
    PDFPage page = doc.GetPage(0);
     
    // Parse page.
    page.StartParse((int)foxit.pdf.PDFPage.ParseFlags.e_ParsePageNormal, null, false);
  • Render a Page to a bitmap and save it as a JPG file.
  • int width = (int)(page.GetWidth());
    int height = (int)(page.GetHeight());
    Matrix2D matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());
     
    // Prepare a bitmap for rendering.
    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
    Graphics draw = Graphics.FromImage(bitmap);
    draw.Clear(Color.White);
     
    // Render page
    Renderer render = new Renderer(bitmap, false);
    render.StartRender(page, matrix, null);
     
    // Add the bitmap to image and save the image.
    foxit.common.Image image = new foxit.common.Image();
    image.AddFrame(bitmap);
    image.SaveAs("testpage.jpg");
  • Click “Build > Build Solution” to build the project. The executable file “test_dotnet.exe” will be generated in “test_dotnet\test_dotnet\bin\Debug” or “test_dotnet\test_dotnet\bin\Release” folder depending on the build configurations.
    Note: Please check whether the “fsdk.dll” and “fsdk_dotnet.dll” have been copied to the same folder with the “test_dotnet.exe”. If not, you should put the dynamic libraries to the folder manually.
  • Double-click the executable file “test_dotnet.exe” to run this project, and then the “testpage.jpg” will be generated in the current folder.
  • The final contents of “Program.cs” is as follow:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
     
     
    using foxit;
    using foxit.common;
    using foxit.common.fxcrt;
    using foxit.pdf;
     
    namespace test_dotnet
    {
        class Program
        {
            static void Main(string[] args)
            {
     
                // The value of "sn" can be got from "gsdk_sn.txt" (the string after "SN=").
                // The value of "key" can be got from "gsdk_key.txt" (the string after "Sign=").
                string sn = " ";
                string key = " ";
                ErrorCode error_code = Library.Initialize(sn, key);
                if (error_code != ErrorCode.e_ErrSuccess)
                {
                    return;
                }
     
                using (PDFDoc doc = new PDFDoc("../../Sample.pdf"))
                {
                    error_code = doc.LoadW("");
                    if (error_code != ErrorCode.e_ErrSuccess)
                    {
                        Library.Release();
                        return;
                    }
     
                    using (PDFPage page = doc.GetPage(0))
                    {
                        // Parse page.
                        page.StartParse((int)foxit.pdf.PDFPage.ParseFlags.e_ParsePageNormal, null, false);
     
                        int width = (int)(page.GetWidth());
                        int height = (int)(page.GetHeight());
                        Matrix2D matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());
     
                        // Prepare a bitmap for rendering.
                        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                        Graphics draw = Graphics.FromImage(bitmap);
                        draw.Clear(Color.White);
     
                        // Render page
                        Renderer render = new Renderer(bitmap, false);
                        render.StartRender(page, matrix, null);
     
                        // Add the bitmap to image and save the image.
                        foxit.common.Image image = new foxit.common.Image();
                        image.AddFrame(bitmap);
                        image.SaveAs("testpage.jpg");
                    }
                }
                Library.Release();
            }
        }
    }
    Updated on June 20, 2018

    Was this article helpful?

    Related Articles