Integrate the PSPDFKit .NET Library into a .NET Framework/Mono Project

The PSPDFKit .NET Library was built to work with a wide variety of .NET frameworks. In this guide, we’ll show how to integrate the PSPDFKit .NET Library into a .NET/Mono project.

Requirements

macOS

  • Mono (brew install mono)
  • GDI Plus (brew install mono-libgdiplus, for rendering only)

Linux

  • Mono (sudo apt install mono-devel)
  • msbuild (sudo apt install msbuild)
  • GDI Plus (sudo apt install libgdiplus, for rendering only)

Windows

.NET Solution Requirement

In order for the PSPDFKit .NET Library to function, the project and solution build configuration must be set to either x86 or x64. Any CPU will not work because the application does not understand which native binary to use. For more information on configuration, please see the Microsoft documentation.

Create a New .NET Project

There are many ways to create a project using the .NET framework. For Windows, Visual Studio is a good option, and on macOS, Visual Studio for Mac is an option. On Linux, you may want to opt for MonoDevelop. For a cross-platform solution, JetBrains Rider is an option. The decision mainly comes down to your project’s supported operating systems and business considerations.

Add the PSPDFKit .NET Library from nuget.org (Suggested)

For simple integration directly into your project, the PSPDFKit .NET Library is hosted on the public nuget.org repo.

Add the PSPDFKit .NET Library via Visual Studio Windows

It’s possible to add the PSPDFKit .NET Library within the Visual Studio GUI if you’re working in Windows. Please see the Microsoft guide on installing packages in Visual Studio Windows for instructions on how to add a package, and use PSPDFKit.NET when searching for the package.

Add the PSPDFKit .NET Library via Visual Studio Mac

If you’d like to add the PSPDFKit .NET Library using the Visual Studio for Mac IDE, please see the Microsoft guide on installing packages in Visual Studio Mac for instructions on how to add a package, and use PSPDFKit.NET when searching for the package.

Manually Add the NuGet Pacakge

Create the PSPDFKit NuGet Source

Rather than targeting a package held at nuget.org, you will have to set up a configuration to point to a local package.

The local configuration is described by a file named nuget.config, which is placed next to the solution file (e.g Test.sln).

The contents of the file should contain an XML element, packageSources, which describes where to find NuGet packages — as a child of a root node named configuration.

If nuget.config Already Exists

If nuget.config already exists, add the extra packageSources entry shown below:

Copy
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <packageSources>
       ...
       <add key="PSPDFKitSource" value="path/to/directoryContainingNupkg" />
       ...
   </packageSources>
</configuration>

If There Is No nuget.config File Present

If there is no nuget.config file present, create the nuget.config file and copy and paste the following source into the newly created file:

Copy
1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <packageSources>
       <add key="PSPDFKitSource" value="path/to/directoryContainingNupkg" />
   </packageSources>
</configuration>

Edit the value of the contents to correctly refer to the location of the directory containing the PSPDFKit.NET-*.*.*.nupkg package — for example, /Users/me/nugetPackages.

Add the PSPDFKit NuGet Package

Now that you have defined your NuGet source, it’s possible to add the PSPDFKit.NET package.

To do this, you can add a PackageReference to the project file, or if a package already exists, add a reference in the packages.config. The recommended process is to use a PackageReference, which is the process that will be explained here. (If you would like to migrate from a packages.config, please refer to the Microsoft documentation, or if you would like to continue using the packages.config file, a guide on how to add references can be found at packages.config reference.)

  • With a text editor, open the project file you would like to add the PSPDFKit Libraries package to (e.g Test.csproj).
  • Add a PackageReference to this file and replace the version number with the PSPDFKit Libraries version you require. You may use * in the version to support floating versions:
1
2
3
4
5
<ItemGroup>
    <!-- ... -->
    <PackageReference Include="PSPDFKit.NET" Version="*.*.*" />
    <!-- ... -->
</ItemGroup>
  • Install the package from the directory holding the solution file (Test.sln).
    • Mac: /Library/Frameworks/Mono.framework/Versions/Current/bin/msbuild restore
    • Linux: msbuild restore
    • Windows: msbuild.exe restore

You now have your project set up to use the PSPDFKit .NET package. You will need an internet connection to retrieve the dependency projects.

Package Installation Options

It should be noted that there are a variety of ways to add a package to a project:

Please refer to the relevant link above for more specific information.

Use the PSPDFKit .NET Library

Now that PSPDFKit .NET is added, you can use the SDK in your application. You will have to call the Initialize method prior to calling any other SDK method.

Here’s a small example showing how to import Instant JSON and save the document:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using PSPDFKit;

namespace Test
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Sdk.Initialize("YOUR_LICENSE_KEY_GOES_HERE");
            var document = new Document(new FileDataProvider("myDocument.pdf"));
            document.ImportJson(new FileDataProvider("myJson.json"));
            document.Save(new DocumentSaveOptions
            {
                incremental = true
            });
        }
    }
}

For more examples of how to use the SDK, please refer to both the Catalog example in PSPDFKit.NET-{version}.zip and the API documentation.