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 (2010 or later):
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; } } |
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. // |
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; } |
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);
|
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); |
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.