ImageGear for C and C++ on Linux v18.10 - 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++ Linux console application and use ImageGear to load PDF image and convert it to a new raster format.

  1. Create file ig_sample.cpp with the content below using gedit Linux utility:
    C++
    Copy Code
    #include <iostream>
    #include "gear.h"
    #include "i_PDF.h"
    int main(int argc, char* argv[])
    {
        // Number of ImageGear errors that occurred
        AT_ERRCOUNT nErrCount = 0;
        // Image handle
        HIGEAR hImage = (HIGEAR)NULL;
        // By default, save the image as BMP uncompressed
        AT_LMODE outputFormat = IG_SAVE_BMP_UNCOMP;
        if (argc < 3)
        {
            std::cout << "Usage: ./ig_sample <input_pdf_file> <output_file> <output_format>\n";
            std::cout << "Where output_format can be BMP, PNG, JPG, TIF\n";
            exit(1);
        }
        if (argc >= 4) // output format has been passed
        {
            if (strcmp(argv[2], "BMP") == 0)
                outputFormat = IG_SAVE_BMP_UNCOMP;
            else if (strcmp(argv[3], "PNG") == 0)
                outputFormat = IG_SAVE_PNG;
            else if (strcmp(argv[3], "JPG") == 0)
                outputFormat = IG_SAVE_JPG;
            else if (strcmp(argv[3], "TIF") == 0)
                outputFormat = IG_SAVE_TIF_UNCOMP;
        }
        // Initialize ImageGear
        nErrCount = IG_initialize(NULL, 0, FALSE);
        if (nErrCount != 0)
        {
            exit(-1);
        }
        // License ImageGear with a Runtime License
        // Uncomment the next three lines and update all the parameters to
        // these licensing functions to use Runtime Licensing
        //IG_lic_solution_name_set("YourSolutionName");
        //IG_lic_solution_key_set(12345, 12345, 12345, 12345);
        //IG_lic_OEM_license_key_set("2.0...");
        // Initialize PDF component
        IG_comm_comp_attach("PDF");
        IG_PDF_initialize(NULL);
        // Load image file
        nErrCount = IG_load_file(argv[1], &hImage);
        if (nErrCount != 0)
        {
            exit(-1);
        }
        // Check if loaded image is valid
        if (IG_image_is_valid(hImage))
        {
            // Rasterize PDF image
            HIGEAR hImageRasterized;
            nErrCount = IG_vector_data_to_dib(hImage, &hImageRasterized);
            if (nErrCount == 0)
            {
                // Save the PDF image with the new raster format
                nErrCount = IG_save_file(hImageRasterized, argv[2], outputFormat);
                // Delete rasterized image
                IG_image_delete(hImageRasterized);
            }
            // Delete the image
            IG_image_delete(hImage);
        }
        if (nErrCount != 0)
        {
            exit(-1);
        }
        // Terminate PDF component
        IG_PDF_terminate();
        // Close ImageGear
        IG_close(NULL);
        return 0;
    }
    

    The above sample takes at least  two arguments, an input PDF filename and an output image filename where the resulting output image will be stored.
    A third optional parameter may be used to specify the format of the output image file.
    By default, the output format is BMP.

    C++
    Copy Code
    #include "gear.h"
    #include "i_PDF.h"
    

    The header files that are necessary for the main ImageGear functionality and for working with PDF component.

    C++
    Copy Code
    // Initialize ImageGear
    nErrCount = IG_initialize(NULL, 0, FALSE);
    

    This function initializes the ImageGear engine. This line is required before any other ImageGear function calls on Linux.

    Copy Code
    // License ImageGear
    

    To unlock the toolkit for deployment, you must call the IG_lic_solution_name_set(), IG_lic_solution_key_set() and possibly the IG_lic_OEM_license_key_set() functions.
    See Licensing section for more details.

    C++
    Copy Code
    // Initialize PDF component
    IG_comm_comp_attach("PDF");
    IG_PDF_initialize(NULL);
    

    The first function attaches the PDF component to the main ImageGear module. The second one initializes the PDF engine.

    C++
    Copy Code
    // Load image file
    nErrCount = IG_load_file(argv[1], &hImage);
    

    This function loads the input image. The first parameter is the name of the image file. The second parameter is a  returned handle to the loaded image. This handle is used in all subsequent operations on the loaded image.

    C++
    Copy Code
    // Check if loaded image is valid
    if (IG_image_is_valid(hImage))
    

    This function checks whether the image handle is valid or not.

    C++
    Copy Code
    // Rasterize PDF image
    HIGEAR hImageRasterized;
    nErrCount = IG_vector_data_to_dib(hImage, &hImageRasterized);
    

    This function flushes the PDF data to device independent raster format and returns a handle of new raster image in hImageRasterized argument.

    C++
    Copy Code
    // Save the rotated image with the new raster format
    nErrCount = IG_save_file(hImageRasterized, argv[2], outputFormat);
    

    This function saves the rasterized image to the specified file in the specified format.

    C++
    Copy Code
    // Delete the image
    IG_image_delete(hImage);
    

    This function deletes the image handle after it is no longer needed and releases all the memory used by the image.

    C++
    Copy Code
    // Terminate PDF component
    IG_PDF_terminate();
    

    This function de-initializes the PDF engine.

    C++
    Copy Code
    // Close ImageGear
    IG_close(NULL);
    

    This function closes ImageGear and should be called before the application program exits.

  2. Create CMakeList.txt file with the following content:
    Copy Code
    cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
     project(ig_sample)
    # relative path to your ImageGear include directory;
    include_directories(${CMAKE_SOURCE_DIR}/../../include)
    # relative path to your ImageGear bin directory;
    link_directories(${CMAKE_SOURCE_DIR}/../../bin/)
    # reference to main ImageGear library  IGCORE18
     set(LIBRARY_NAME IGCORE18)
    add_definitions(-D_UNIX64)
    # specify input and output files;
    add_executable(ig_sample ig_sample.cpp)
    # link the program with ImageGear library.
    target_link_libraries(ig_sample ${LIBRARY_NAME}
    
  3. Run the following commands:
    cmake.
    make

    The executable file, ig_sample, will be created in the current directory.

  4. Copy any valid PDF file to the current directory (say, sample.pdf) and run the program:
    Copy Code
    ./ig_sample sample.pdf sample.png PNG
    

    The file, sample.pdf, will be loaded, rasterized and saved as a PNG image in sample.png.