LEADTOOLS Raster Imaging C DLL Help > Tutorials > 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.
Start Visual Studio 2008, and from the main menu select File->New->Project.
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.
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.
In the Win32 Application Wizard, click Next>.
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.
Make sure that only System menu and About box are selected (checked), then choose Next> again
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
In order to simplify pasting code provided in this tutorial, setup 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 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.
Add to the ImageConversionDlg.h in the Header Files the following code:
//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();
////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////
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"
////////////////////////////////////////////////////////////////////////////
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
////////////////////////////////////////////////////////////////////////////
//Comment out the line of code below:
//CTEXT "TODO: Place dialog controls here.",IDC_STATIC,10,96,300,8
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;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
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)
////////////////////////////////////////////////////////////////////////////////////////////////////
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 ImageConversion folder. (For example: C:\LEADTOOLS 18\Examples\CDLL\ImageConversion\YourImage).
For more information, refer to:
Implementing PDF Plug-in Features
See also: