Skip to content

Foxit PDF SDK .NET Framework Library

This section describes how to run samples and create a basic project using the Foxit PDF SDK .NET Framework library. The project demonstrates rendering the first page of a PDF document to a bitmap and saving it as a JPG image.

Prerequisites

Development environment

  • Windows .NET Framework version: ≥ 4.0
  • Foxit PDF SDK .NET library

System support

We provide detailed system support information for Windows, including operating system versions, compiler requirements, and more. Select your platform to view the details.

Windows

Run samples from the command line

See Samples for an overview of the sample projects, dependency information for specific samples, and instructions for running samples from the command line.

Run samples in Visual Studio

After building with Visual Studio, a bin folder is generated under \examples\simple_demo\, containing .exe executables. The executable name depends on the build configuration.

Run all samples

  1. Open the solution: In the \examples\simple_demo folder, open the solution file (.sln) that matches your Visual Studio version.
  2. Build the solution: In Visual Studio, select Build > Build Solution to compile all samples.
  3. Run a sample: After a successful build, double-click the generated .exe to run the corresponding sample.

Run a specific sample

  1. Build the sample project: In Visual Studio Solution Explorer, right-click the target sample project and select Build. Alternatively, double-click the .vcxproj file in the sample folder to open and build that project.
  2. Run the sample: After a successful build, double-click the generated .exe to run the sample.

To see detailed sample output, run from the command line.

  • Open cmd.exe, use cd to navigate to \examples\simple_demo\bin, then run the desired .exe.

Quick start

Create a C# .NET Framework sample project quickly

  1. Create a console application:

    • Open Visual Studio and create a C# console application named test_dotnet.
  2. Copy SDK library files:

    • Copy the lib folder from the SDK package into the test_dotnet project directory.
  3. Add a reference to fsdk_dotnet.dll:

    • In Solution Explorer, right-click the test_dotnet project and select "Add" -> "Reference...".
    • In the Reference Manager dialog, select the "Browse" tab.
    • Based on your build configuration, navigate to test_dotnet\lib\x64_vc10, test_dotnet\lib\x86_vc10, or test_dotnet\lib\anycpu_vc15.
    • Select fsdk_dotnet.dll and click "Add".

    NOTE

    • Ensure the architecture of fsdk_dotnet.dll matches your application's platform target (Win32/Win64/AnyCPU).
  4. Add the fsdk.dll native library:

    • Win32/Win64 platform target:
      • Right-click the test_dotnet project and select Add -> Existing Item....
      • Based on your build configuration, navigate to test_dotnet\lib\x64_vc15 or test_dotnet\lib\x86_vc15.
      • Select fsdk.dll and click Add.
    • AnyCPU platform target:
      • Right-click the test_dotnet project, select Add -> "New Folder...", and create a folder named x64.
      • Right-click the x64 folder, select Add -> Existing Item..., navigate to test_dotnet\lib\x64_vc15\, select fsdk.dll, and click Add.
      • Repeat the steps above to create an x86 folder and add fsdk.dll from test_dotnet\lib\x86_vc15\.

    NOTE

    • Set the Copy to Output Directory property of fsdk.dll to Copy if newer.
    • For AnyCPU, you must successfully call Library.Initialize before creating SDK objects.
  5. Add using declarations (Program.cs):

    • Add the following using declarations at the top of Program.cs:

      csharp
      using foxit;
      using foxit.common;
      using foxit.common.fxcrt;
      using foxit.pdf;
  6. Initialize the Foxit PDF SDK library:

  7. Load and parse a PDF document:

    • Copy Sample.pdf to the test_dotnet\test_dotnet folder.
    • Add code in Program.cs to load Sample.pdf and parse its first page.
    [loadpdf.cs]
    csharp
    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);
  8. Render and save the PDF page:

    • Add code to render the PDF page to a bitmap and save it as a JPG image.
    [SaveAsImage.cs]
    cs
    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.
    foxit.common.Bitmap bitmap = new foxit.common.Bitmap(width, height, foxit.common.Bitmap.DIBFormat.e_DIBArgb, System.IntPtr.Zero, 0);
    bitmap.FillRect(0xFFFFFFFF, null);
    // 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");

    :::

  9. Build the project:

    • Click Build -> Build Solution to compile the project.
    • The executable test_dotnet.exe is generated under test_dotnet\test_dotnet\bin\Debug or test_dotnet\test_dotnet\bin\Release, depending on the build configuration.

    NOTE

    • For Win32/Win64, ensure fsdk.dll and fsdk_dotnet.dll are copied to the same folder as test_dotnet.exe.
    • For AnyCPU, ensure fsdk_dotnet.dll is copied to the same folder as test_dotnet.exe, and that the x64 and x86 folders contain the corresponding fsdk.dll files.
  10. Run the project:

    • Double-click test_dotnet.exe to run the project.
    • If it runs successfully, testpage.jpg is generated in the current folder.

Complete code example:

[Program.cs]
cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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.
                    foxit.common.Bitmap bitmap = new foxit.common.Bitmap(width, height, foxit.common.Bitmap.DIBFormat.e_DIBArgb, System.IntPtr.Zero, 0);
                    bitmap.FillRect(0xFFFFFFFF, null);

                    // 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();
        }
    }
}