ImageGear for C and C++ on Windows v20.1 - Updated
Tutorial: Create Your First PDF Project
User Guide > How to Work with ... > Formats with Additional Functionality > PDF > Getting Started with PDF > Tutorial: Create Your First PDF Project

In this tutorial, you will create a C/C++ Windows console application and use ImageGear components to open a PDF file and save it as a new file.

The following tutorial refers specifically to 64-bit installations. For 32-bit installations, throughout these instructions, substitute x64 with x86.

Using Visual Studio (2015 or later):

  1. Create a new "Win32 Console Application" project named "IG_Tutorial_PDF".
  2. Create a new solution platform for platform x64.
  3. Set the solution configuration to either "Debug|x64" or "Release|x64" and apply the following changes:
    1. Set Configuration Properties\General\Output Directory to the absolute path to the ImageGear binaries directory.
      e.g. [INSTALLDIR]\ImageGear for C and C++ vXX\Build\Bin\x64\
    2. Modify project setting Configuration Properties\C/C++\General\Additional Include Directories to include the absolute path to ImageGear header files directory.
      e.g. [INSTALLDIR]\ImageGear for C and C++ vXX\Build\Include
    3. Modify project setting Configuration Properties\Linker\General\Additional Library Directories to include the absolute path to ImageGear libraries directory.
      e.g. [INSTALLDIR]\ImageGear for C and C++ vXX\Build\Lib\x64
    4. Modify project setting Configuration Properties\Linker\Input\Additional Library Dependencies to link with the ImageGear CORE component import library, igCOREXXd.lib.
  4. At this point, your project is ready for some sample code. We will highlight key snippets in the following steps. But first, open the source file, IG_Tutorial_PDF.cpp, which Visual Studio created automatically, and replace with the following:
    C
    Copy Code
    // IG_Tutorial_PDF.c : Defines the entry point for the console application.
    //
    // Copyright Accusoft Corporation, Tampa Florida. All Rights Reserved.
    
    #include "stdafx.h"
    #include <Windows.h>
    #include <iostream>
    #include <string>
    #include <direct.h>
    using namespace std;
    
    #include "gear.h"
    #include "i_PDF.h"
    
    bool Initialize()
    {
       // Attach and initialize the PDF component.
       if ((IG_comm_comp_attach("PDF") != IGE_SUCCESS) || (IG_PDF_initialize(NULL) != IGE_SUCCESS))
       {
           // Usually this is because a DLL or the Resource directory is not found.
           cout << "PDF component error." << endl;
           return false;
       }
       return true;
    }
    
    void Terminate()
    {
       // Terminate the PDF component.
       IG_PDF_terminate();
    }
    
    int main()
    {
       AT_ERRCOUNT errCount = 0;
       HMIGEAR document = 0;
       AT_CHAR inputPath[260];
       AT_CHAR currentDir[MAX_PATH + 1];
       LPSTR outputPath = "myFirstSavedPDF.pdf";
       UINT pageCount = 0;
    
       memset(inputPath, 0, sizeof(inputPath)*(sizeof(inputPath[0])));
    
       fprintf(stdout, "Enter the complete path to the PDF file: ");
       fscanf_s(stdin, "%259[^\n]%*c", inputPath);
    
       if (Initialize( ) == false)
       {
           fprintf(stdout, "Unable to initialize the PDF library.");
           return EXIT_FAILURE;
       }
    
       // Open the file.
       IG_mpi_create(&document, 0);
       IG_mpi_file_open(inputPath, document, IG_FORMAT_PDF, IG_MP_OPENMODE_READONLY);
    
       // Inform the user that the output will be located in the current working directory.
       _getcwd(currentDir, sizeof(currentDir));
       fprintf(stdout, "The saved document will be placed in the folder: %s", currentDir);
    
       // Save the document with a new name.
       IG_mpi_page_count_get(document, &pageCount);
       errCount = IG_mpi_file_save(outputPath, document, 0, 0, pageCount,
           IG_FORMAT_PDF, IG_MPI_SAVE_OVERWRITE);
    
       // Release the file.
       IG_mpi_delete(document);
    
       if (errCount == 0)
       {
           Terminate();
           fprintf(stdout, "Saved %s", outputPath);
           return 0;
       }
       else
       {
           fprintf(stdout, "Failed to save %s", outputPath);
           return 1;
       }
    }
    
    C++
    Copy Code
    // IG_Tutorial_PDF.cpp : Defines the entry point for the console application.
    //
    // Copyright Accusoft Corporation, Tampa Florida. All Rights Reserved.
    
    #include "stdafx.h"
    #include <Windows.h>
    #include <iostream>
    #include <string>
    #include <direct.h>
    using namespace std;
    
    #include "gear.h"
    #include "i_PDF.h"
    
    bool Initialize()
    {
       // Attach and initialize the PDF component.
       if ((IG_comm_comp_attach("PDF") != IGE_SUCCESS) || (IG_PDF_initialize(NULL) != IGE_SUCCESS))
       {
           // Usually this is because a DLL or the Resource directory is not found.
           cout << "PDF component error." << endl;
           return false;
       }
       return true;
    }
    
    void Terminate()
    {
       // Terminate the PDF component.
       IG_PDF_terminate();
    }
    
    int main()
    {
       cout << "Enter the complete path to the PDF file: ";
       string inputPath;
       getline(cin, inputPath);
    
       if (Initialize( ) == false)
       {
           cout << "Unable to initialize the PDF library." << endl;
           return EXIT_FAILURE;
       }
    
       // Open the file.
       HMIGEAR document = 0;
       IG_mpi_create(&document, 0);
       IG_mpi_file_open((LPSTR)inputPath.c_str(), document, IG_FORMAT_PDF, IG_MP_OPENMODE_READONLY);
    
       // Inform the user that the output will be located in the current working directory.
       char currentDir[MAX_PATH + 1];
       _getcwd(currentDir, sizeof(currentDir));
       cout << "The saved document will be placed in the folder: " << currentDir << endl;
    
       // Save the document with a new name.
       LPCHAR outputPath = "myFirstSavedPDF.pdf";
       UINT pageCount;
       IG_mpi_page_count_get(document, &pageCount);
       AT_ERRCOUNT errCount = IG_mpi_file_save(outputPath, document, 0, 0, pageCount,
           IG_FORMAT_PDF, IG_MPI_SAVE_OVERWRITE);
    
       // Release the file.
       IG_mpi_delete(document);
    
       if (errCount == 0)
       {
           Terminate();
           cout << "Saved " << outputPath;
           return EXIT_SUCCESS;
       }
       else
       {
           cout << "Failed to save " << outputPath;
           return EXIT_FAILURE;
       }
    }
    
  5. If you are using a Deployment license (also known as "Runtime" license), then add the license initialization code. This is not necessary if you are using an Evaluation license or Development license (also known as a "Toolkit" license).
    C
    Copy Code
       //
       // To unlock ImageGear for deployment you must call the
       // IG_lic_solution_name_set() and IG_lic_solution_key_set() and possibly
       // IG_lic_OEM_license_key_set() functions.
       // See Licensing section in ImageGear User Manual for more details.
       //
    
    C++
    Copy Code
       //
       // To unlock ImageGear for deployment you must call the
       // IG_lic_solution_name_set() and IG_lic_solution_key_set() and possibly
       // IG_lic_OEM_license_key_set() functions.
       // See Licensing section in ImageGear User Manual for more details.
       //
    
  6. To work with PDF documents you always need to attach and initialize the PDF component:
    C
    Copy Code
    bool Initialize()
    {
       // Attach and initialize the PDF component.
       if ((IG_comm_comp_attach("PDF") != IGE_SUCCESS) || (IG_PDF_initialize(NULL) != IGE_SUCCESS))
       {
           // Usually this is because a DLL or the Resource directory is not found.
           cout << "PDF component error." << endl;
           return false;
       }
       return true;
    }
    
    C++
    Copy Code
    bool Initialize()
    {
       // Attach and initialize the PDF component.
       if ((IG_comm_comp_attach("PDF") != IGE_SUCCESS) || (IG_PDF_initialize(NULL) != IGE_SUCCESS))
       {
           // Usually this is because a DLL or the Resource directory is not found.
           cout << "PDF component error." << endl;
           return false;
       }
       return true;
    }
    
  7. We load the PDF document using:
    C
    Copy Code
       // Open the file.
       IG_mpi_create(&document, 0);
       IG_mpi_file_open(inputPath, document, IG_FORMAT_PDF, IG_MP_OPENMODE_READONLY);
    
    C++
    Copy Code
        // Open the file.
       HMIGEAR document = 0;
       IG_mpi_create(&document, 0);
       IG_mpi_file_open((LPSTR)inputPath.c_str(), document, IG_FORMAT_PDF, IG_MP_OPENMODE_READONLY);
    
  8. We save the loaded document with a new file name:

    The majority of ImageGear functions return either an error code, enumIGErrorCodes, or an error count, AT_ERRCOUNT. For brevity, we ignore ImageGear return values in this sample except for saving. This will allow us to indicate that saving was successful with an appropriate message and exit code.

    C
    Copy Code
       // Save the document with a new name.
       IG_mpi_page_count_get(document, &pageCount);
       errCount = IG_mpi_file_save(outputPath, document, 0, 0, pageCount,
           IG_FORMAT_PDF, IG_MPI_SAVE_OVERWRITE);
    
    C++
    Copy Code
       // Save the document with a new name.
       LPCHAR outputPath = "myFirstSavedPDF.pdf";
       UINT pageCount;
       IG_mpi_page_count_get(document, &pageCount);
       AT_ERRCOUNT errCount = IG_mpi_file_save(outputPath, document, 0, 0, pageCount,
           IG_FORMAT_PDF, IG_MPI_SAVE_OVERWRITE);
    
  9. Finally, release any ImageGear resources and terminate the component before exiting:
    C
    Copy Code
       // Release the file.
       IG_mpi_delete(document);
    
       // Terminate the PDF component.
       IG_PDF_terminate();
    
    C++
    Copy Code
       // Release the file.
       IG_mpi_delete(document);
    
       // Terminate the PDF component.
       IG_PDF_terminate();
    

Now that you've created your first PDF project in ImageGear, you're ready for more advanced functionality. You can start with our suite of PDF Samples, browse the How to... topics, or dig right into the PDF API.

Is this page helpful?
Yes No
Thanks for your feedback.