Add PDF functionality with Visual C++ with MFC

This guide explains how to integrate Nutrient .NET SDK (formerly known as GdPicture.NET SDK) into your application.

About Nutrient .NET SDK

Nutrient .NET SDK is a cross-platform developer solution for building intelligent PDF and document processing applications. This toolkit enables you to compose, display, capture, edit, and print documents. You can build applications using Nutrient .NET SDK with numerous development environments and deploy these applications to different platforms.

Prerequisites

Before proceeding with this guide, ensure your development environment meets the following requirements.

.NET 8.0 or higher (cross-platform)

  • .NET SDK 8.0 or higher — Install the latest stable version from the Microsoft website(opens in a new tab).
  • Supported operating systems — Windows, macOS, and Linux.
  • Development tools — Visual Studio 2022 (17.6+) or Visual Studio Code with C# extension.
  • Command-line tools — Ensure the .NET CLI is available and configured in your system path.

.NET Framework 4.6.2 or higher (Windows)

  • .NET Framework 4.6.2+ — Preinstalled on Windows or available from the official Microsoft Download Center.
  • Supported operating systems — Windows 10 or later.
  • Development tools — Visual Studio 2019 (or newer) with the .NET framework development workload installed.

For more information on the supported frameworks and operating systems, refer to our system compatibility guide.

Installing Nutrient .NET SDK

The best way to install Nutrient .NET SDK is through NuGet repositories. To view a list of available packages, refer to the download Nutrient .NET guide.

Alternatively, use our ready-made bootstrap project for new setups.

Download example project

Activating the trial license

To activate a trial license, follow the steps below:

  1. In Visual Studio, create a LicenseManager object in the code file where the application loads before using any of the methods of Nutrient .NET SDK.
  2. Pass the license key to the RegisterKEY method of the LicenseManager object.

Set the license using the LicenseManager once before making any calls to the SDK. This activates the license for the entire assembly. Make sure your application initializes and sets the license only once.

LicenseManager licenseManager = new LicenseManager();
licenseManager.RegisterKEY("");

Leaving the license key as an empty string activates the SDK in trial mode. After purchasing a commercial license, replace the empty string with your license key.

Starting from version 14.2.94, a trial key is no longer required. As shown in the examples above, setting the key to an empty string ("") enables trial mode.

Integrating with Visual C++ with MFC

This section provides you with a step-by-step guide to getting started using Nutrient .NET SDK with Visual C++ (up to VS 2017) with an MFC environment.

You can find an example using COM Interop in the [INSTALLATION FOLDER]\Samples\WinForm\c++ mfc folder.

IntelliSense is fully supported in Visual C++.

  1. Start a new project in Visual Studio > File > New > Project. Then select MFC/ATL > MFC Application.

    Visual Studio > MFC/ATL > MFC Application
  2. Set your Application Type to Dialog based.

    Dialog based Application Type
  3. Copy GdPicture.NET.14.dll and GdPicture.NET.14.tlb to your application folder.

  4. To import the DLL, use Project > Class Wizard (available until VS 2017), choose the Add Class... dropdown, and select MFC Class From TypeLib...

    Select MFC Class From TypeLib
  5. Under Add class from: in the wizard dialog, select File. Then, click the ... button on the right-hand side, next to the Location field, to select the GdPicture.NET.14.tlb file located in [INSTALLDIR]/Redist/COM Interop or in your application directory if you copied it.

  6. Select the interfaces you need from the list to generate the C++ wrappers.

    Add class and select interfaces

    Each Nutrient .NET SDK class, which also contains events, will be split into two classes: one defining events, and another one defining properties and methods. For example, to import the GdPicturePDF class, select __GdPicturePDF and GdPicturePDF.

  7. Change the way the DLL is imported in generated header files (if needed). Change no_namespace to raw_interfaces_only in the #import code line in the generated headers.

    Change `no_namespace` to `raw_interfaces_only`
  8. Initialize COM objects in your application by adding HRESULT hr = CoInitialize(NULL); under the main function.

    Initialize COM objects
  9. Include the generated headers in your file and build the application.

    Include the generated headers

GdViewer integration

This section provides you with a step-by-step guide to getting started creating a simple MFC application using Nutrient .NET SDK with Visual C++ (up to VS 2017) and containing a GdViewer ActiveX component.

You can find an example using COM Interop in the [INSTALLATION FOLDER]\Samples\WinForm\c++ mfc folder.

Integration

  1. Start a new project in Visual Studio > File > New > Project. Then select Visual C++ > MFC/ATL > MFC Application. Write the project name and confirm.

    Start a new project in Visual Studio
  2. Set your Application Type to Dialog based. Copy GdPicture.NET.14.(64).dll and GdPicture.NET.14.tlb to your application folder.

    Set Application Type and copy files
  3. To import the installed Nutrient .NET SDK toolkit (DLL) and to integrate the GdViewer control, you need to follow two steps.

    To import as an ActiveX control:

    • Use Project > Class Wizard (available until VS 2017) and select the Add Class dropdown. Then, choose MFC Class From ActiveX Control... This is necessary for controls like GdViewer.
    • The window with the wizard will appear, and from the Available ActiveX controls list, choose GdPicture14.GdViewer.
    Setting up in the Class Wizard
    • From the Interfaces list, choose IGdViewer and add it to the Generated classes list. Then click Finish and press OK.
    Add IGdViewer to the Generated classes list

    To import from TypeLib:

    • Use Project > Class Wizard (available until VS 2017) and select the Add Class dropdown. Then, choose MFC Class From TypeLib...
    • The window with the wizard will appear, and from the Available type libraries list, choose GdPicture14 - Document Imaging.
    Choose GdPicture14 - Document Imaging from Available type libraries
    • From the Interfaces list, choose ILicenseManager and __GdViewer and add them to the Generated classes list. Then click Finish and press OK.
  4. Initialize COM objects in your application by adding HRESULT hr = CoInitialize(NULL); under the main function.

    Initialize COM objects

    Here’s the adapted source code:

    BOOL CdemoApp::InitInstance()
    {
    ...
    InitCtrls.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&InitCtrls);
    CWinApp::InitInstance();
    HRESULT hr = CoInitialize(NULL);
    ...
    }
  5. Include the generated headers in your file and build the application.

    Here’s the adapted source code:

    // Add GdViewer control to the `demoDlg.h` file:
    class CdemoDlg : public CDialogEx
    {
    // Construction
    public:
    CGdViewer sampleView;
    ...
    }
    // Include headers in the `demoDlg.cpp` file:
    #include "C_GdViewer.h"
    #include "CLicenseManager.h"
    Include the generated headers

Adding code to initialize the GdViewer component

  1. Your default solution will look like what’s shown below. In Resource View, select Dialog/IDD_DEMO_DIALOG.

    Solution Explorer
  2. When the form appears, choose Button from the toolbox on the left side and place it on the form.

  3. Double-click the form, and the method for the button click will appear.

  4. Update it with this code:

    const int CTRL_ID = 280;
    void CdemoDlg::OnBnClickedButton1()
    {
    // TODO: Add your control notification handler code here.
    CLicenseManager oLicenseManager;
    oLicenseManager.CreateDispatch(L"GdPicture14.LicenseManager");
    oLicenseManager.RegisterKEY(L" XXX ");
    // Create the control, but make sure to use `WS_CHILD` and `WS_VISIBLE`.
    if (!sampleView.Create(L"PdfWnd", WS_CHILD | WS_VISIBLE, CRect(0, 0, 200, 200), this, CTRL_ID))
    {
    AfxMessageBox(L"Failed to create GdViewer control");
    return;
    }
    // Sample init settings — not necessary.
    sampleView.put_EnableMenu(false);
    sampleView.put_ContinuousViewMode(TRUE);
    sampleView.put_MouseWheelMode(2);
    sampleView.put_ScrollBars(TRUE);
    sampleView.DisplayFromFile(L"image.jpg");
    sampleView.Redraw();
    }

Adding code to dispose of the GdViewer component

The following example shows how you can release the GdViewer component. What’s important to note is that you need to dispose of the objects properly.

  1. Add a new method for handling the Close() message by choosing Resource View > Demo > Dialog > IDD_DEMO_DIALOG.

  2. Select the form that appears.

  3. Click the messages icon in the properties bar.

  4. Double-click the field next to WM_CLOSE. This will give you the option to create a method for handling Close() messages.

  5. Add the following code to the method you just created:

    • sampleView.CloseDocument();
    • sampleView.Dispose();
    Adding code to dispose of the component

To handle the Close() message, use the following code:

// The result will look like this:
void CdemoDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default.
CDialogEx::OnClose();
sampleView.CloseDocument();
sampleView.Dispose();
}