Integrate the PSPDFKit .NET Library into a .NET Core 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 cross-platform .NET Core project.

Requirements

Linux

  • GDI Plus (sudo apt install libgdiplus, for rendering only)

macOS

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

.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 Core Project

Microsoft has written in depth on how to create a .NET Core project with various options. If you want more information on how to manage your project, we would advise reading through the Get started with .NET Core using the .NET Core CLI article.

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

The PSPDFKit .NET Library is hosted on the public nuget.org repo.

Add the PSPDFKit .NET Library via Visual Studio Mac

A cross-platform solution for adding PSPDFKit .NET Library to your project would be to use the dotnet command line application. Run the following in your project folder to add the PSPDFKit .NET Library package:

1
dotnet add package PSPDFKit.NET --version 1.2.0

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 packages 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 we have the NuGet source, we can add the package to the project:

  • cd into your project solution.
  • Add the package by running dotnet add <PROJECT_NAME> package PSPDFKit.NET, replacing <PROJECT_NAME> with the project you want to add the package to.
  • Now restore all the newly added packages with dotnet restore. You will need an internet connection to retrieve the dependency projects.

You have set up your project to use the PSPDFKit .NET package.

Use the PSPDFKit .NET Library for .NET Core

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 on how to use the SDK, please refer to the Catalog example in PSPDFKit.NET-{version}.zip and the API documentation.

IDE Integration

The steps above are a great source for a cross-platform way of adding PSPDFKit Libraries, but it is possible to perform these steps on many IDEs. It will all depend on what your OS supports, and .NET Core Tutorials has some great information in this area.