Converting Multi-page Images

Take the following steps to create and run a program that converts multipage image files between PDF and TIFF. This tutorial covers implementation of LEADTOOLS functions.

NOTE: Blue highlights denote an instance of a LEADTOOLS object.

1. Start Visual Studio 2008, and from the main menu select File->New->Project.

2.  In the New Project dialog box, choose Visual C++ in the Projects Types, and choose MFC Application in the Templates. Type the project name as ImageConversion in the Project Name field.

3. In the Location field, use the Browse button to navigate to the Examples subdirectory (such as C:\LEAD Technologies\LEADTOOLS xx\Examples\CDLL). Uncheck both Create directory for solution and Add to Source Control options, then choose OK.

4. In the Win32 Application Wizard, click Next>.  

5. Select Dialog based, MFC standard, Use MFC in a shared DLL and deselect (uncheck) Use Unicode libraries, then hoose Next>
NOTE: If unicode character representation option is selected, it will create unnecesary errors related to char handling when compiling the project.

6. Make sure that only System menu and About box are selected (checked), then choose Next> again

7. Make sure only ActiveX controls, and Common Control Manifest options are selected, (checked), choose Next> again, then Finish.

Three folders will be created titled: Header Files,Resource Files, and Source Files. They will contain these files:

Header Files
ImageConversion.h
ImageConversionDlg.h
Resource.h
stdafx.h

Resource Files
ImageConversion.ico
ImageConversion.rc
ImageConversion.rc2

Source Files
ImageConversion.cpp
ImageConversionDlg.cpp
stdafx.cpp

1. In order to simplify pasting code provided in this tutorial, set up Visual Studio 2008 to display line numbers:

In Visual Studio 2008 toolbar choose Tools->Options->Text Editor->All Languages and in Display select Line Numbers. You should see the line numbers being displayed to the left margin in all files in the project. Make sure this feature is enabled, as this tutorial will rely on default line numbers to paste the code.

Make sure you do NOT change the default code line numbers by accidentally pressing the Enter key, and do not change the code provided by default in Visual Studio 2008 before completing this tutorial --  only paste the code in blue highlights and within //////ADDED CODE////// block. Follow the Line number direction of where to place the code in the file after the specified line number.

2. Add the following code to the ImageConversionDlg.h in the Header Files:
 

//Add code after Line 15 
/////////////////////////////ADDED CODE///////////////////////////////////// 
CString m_strFileIn; 
CString m_strFileOut; 
//////////////////////////////////////////////////////////////////////////// 
//Add code after Line 27 
/////////////////////////////ADDED CODE///////////////////////////////////// 
BOOL DisplayError( char* szMsg, L_INT nRet ); 
//////////////////////////////////////////////////////////////////////////// 
//Add code after Line 37 
////////////////////////////ADDED CODE///////////////////////////////////// 
afx_msg void OnGo(); 
//////////////////////////////////////////////////////////////////////////// 

1. Add to Resource.h the code below.

//Add code after Line 9 
/////////////////////////////ADDED CODE///////////////////////////////////// 
#define IDC_GO  1000 
#define IDC_FILE_IN 1001 
#define IDC_FILE_OUT 1002 
/////////////////////////////////////////////////////////////////////////// 

1. Add to stdafx.h the following code (Make sure you are adding to the .h header file and not to the .cpp C++ file):

//Add code after Line 51 
/////////////////////////////ADDED CODE///////////////////////////////////// 
#include "..\..\..\include\l_bitmap.h" 
//////////////////////////////////////////////////////////////////////////// 

1. Add to ImageConversion.rc the following code:

//Add code after Line 92 
/////////////////////////////ADDED CODE///////////////////////////////////// 
PUSHBUTTON "GO",IDC_GO,89,87,50,14 
LTEXT  "Input",IDC_STATIC,7,166,18,8 
LTEXT  "Output",IDC_STATIC,7,185,22,8 
EDITTEXT  IDC_FILE_IN,33,162,127,14,ES_AUTOHSCROLL 
EDITTEXT  IDC_FILE_OUT,33,179,127,14,ES_AUTOHSCROLL 
//////////////////////////////////////////////////////////////////////////// 

1. Add to ImageConversionDlg.cpp the following code:

//Add code after Line 51 inside of CImageConversionDlg::CImageConversionDlg(CWnd* pParent /*=NULL*/) 
/////////////////////////////ADDED CODE///////////////////////////////////// 
m_strFileIn = _T("Multipage.pdf"); 
m_strFileOut = _T("Multipage1.tif"); 
//////////////////////////////////////////////////////////////////////////// 
//Add code after Line 61 inside of CImageConversionDlg::DoDataExchange(CDataExchange* pDX) 
/////////////////////////////ADDED CODE///////////////////////////////////// 
DDX_Text(pDX, IDC_FILE_IN, m_strFileIn); 
DDX_Text(pDX, IDC_FILE_OUT, m_strFileOut); 
//////////////////////////////////////////////////////////////////////////// 
//Add code after Line 72 
/////////////////////////////ADDED CODE///////////////////////////////////// 
ON_BN_CLICKED(IDC_GO, OnGo) 
//////////////////////////////////////////////////////////////////////////// 
//Add code after Line 163 
/////////////////////////////ADDED CODE/////////////////////////////////////// 
void CImageConversionDlg::OnGo() 
{ 
   L_SetLicenseFile(MY_LICENSE_FILE, MY_DEVELOPER_KEY); 
   UpdateData(TRUE); 
   char* szFileIn = NULL; 
   char* szFileOut = NULL; 
   szFileIn = (LPSTR)(LPCTSTR)m_strFileIn; 
   szFileOut = (LPSTR)(LPCTSTR)m_strFileOut; 
   int i; 
   L_INT nRet; 
   FILEINFO FileInfo; 
   ZeroMemory( &FileInfo, sizeof(FILEINFO) ); 
   nRet = L_FileInfo( szFileIn, &FileInfo, sizeof(FILEINFO), FILEINFO_TOTALPAGES, NULL ); 
   if( !DisplayError( "L_FileInfo", nRet ) ) 
   { 
      BITMAPHANDLE bitmap; 
      LOADFILEOPTION LoadInfo; 
      SAVEFILEOPTION SaveInfo; 
      FILEPDFOPTIONS PdfInfo; 
      ZeroMemory( &LoadInfo, sizeof(LOADFILEOPTION) ); 
      ZeroMemory( &SaveInfo, sizeof(SAVEFILEOPTION) ); 
      ZeroMemory( &PdfInfo, sizeof(FILEPDFOPTIONS) ); 
      L_GetDefaultLoadFileOption( &LoadInfo, sizeof(LOADFILEOPTION)); 
      L_GetDefaultSaveFileOption( &SaveInfo, sizeof(SAVEFILEOPTION)); 
      for( i = 1; i <= FileInfo.TotalPages; i++ ) 
      { 
         LoadInfo.PageNumber = i; 
         nRet = L_LoadBitmap( szFileIn, &bitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGRORGRAY, &LoadInfo, NULL ); 
         if( !DisplayError( "L_LoadBitmap", nRet ) ) 
         { 
            SaveInfo.PageNumber = i; 
            if(strstr(szFileOut,".pdf")!=NULL) 
            { 
               int ret = MessageBox("Saving in PDF format","",0); 
               nRet = L_SaveBitmap( szFileOut, /*File name given in the input box*/ 
               &bitmap,                     /*Pointer to the bitmap handle containing image data */ 
               FILE_RAS_PDF_JPEG,           /*File format, see Portable Document Format (PDF)*/ 
               0,                           /*Resulting pixel depth, see Portable Document Format (PDF)*/ 
               0,                           /*QFactor does not apply to PDF*/ 
               &SaveInfo                    /*Extended save options, pass NULL to use default*/); 
            } 
            else if(strstr(szFileOut,".tif")!=NULL) 
            { 
               int ret = MessageBox("Saving in TIF format","",0); 
               /*Refer to help files for BPP values supported by TIF format*/ 
               /*See: Tagged Image File Format (TIFF)*/ 
               nRet = L_SaveBitmap( szFileOut, &bitmap, FILE_TIF, 0, 0, &SaveInfo ); 
            } 
         } 
         else 
         { 
            int ret = MessageBox("Saving with an invalid file extension\n     Try to save as .pdf or .tif  ","",0); 
            return; 
         } 
         if( DisplayError( "L_SaveBitmap", nRet ) ) 
            break; 
      } 
      else 
         break; 
   } 
   AfxMessageBox("Done"); 
} 
BOOL CImageConversionDlg::DisplayError(char* szMsg, L_INT nRet) 
{ 
   if( nRet != SUCCESS ) 
   { 
      CString cs; 
      cs.Format("LEAD Error %d at %s", nRet, szMsg); 
      AfxMessageBox(cs); 
      return TRUE; 
   } 
   return FALSE; 
} 
//////////////////////////////////////////////////////////////////////////////////////////////////// 

1. In Solution Explorer, right click on the Source Files, then Add->New Item. In Categories, choose Code. In the Templates, select C++ File(.cpp). In Name field type Imports. Choose Add. Inspect the newly created source file and make sure it is empty. Add to Imports.cpp the following code:

//Add at Line 1 
//////////////////////////////////////ADDED CODE//////////////////////////////////////////////////// 
#include "Stdafx.h" 
#include "ImageConversion.h" 
#include "ImageConversionDlg.h" 
#if defined(WIN64) 
   #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\x64\\Ltkrn_x.lib") 
   #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\x64\\Ltdis_x.lib") 
   #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\x64\\Ltfil_x.lib") 
   #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\x64\\Ltdlgkrn_x.lib") 
   #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\x64\\Ltdlgfile_x.lib") 
   #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\Win32\\Ltkrn_u.lib") 
   #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\Win32\\Ltdis_u.lib") 
   #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\Win32\\Ltfil_u.lib") 
   #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\Win32\\Ltdlgkrn_u.lib") 
   #pragma comment(lib, "..\\..\\..\\Lib\\CDLL\\Win32\\Ltdlgfile_u.lib") 
#endif // #if defined(WIN64) 
//////////////////////////////////////////////////////////////////////////////////////////////////// 

1. Compile and run the code to test it.

NOTE: The file to convert for this application must be placed in the project directory. The output file will be saved to the same directory. Place your image in the ImageConversion folder. (For example: C:\LEADTOOLS 19\Examples\CDLL\ImageConversion\YourImage).

For more information, refer to:

Implementing PDF Features

LEADTOOLS Basic Data Types

L_DlgFileConversion

L_FileInfo

L_SaveBitmap

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Raster Imaging C API Help