Click here to Skip to main content
15,868,141 members
Articles / Desktop Programming / MFC

PDF from Image Files in One Click

Rate me:
Please Sign up or sign in to vote.
4.53/5 (9 votes)
30 Apr 2022CPOL7 min read 5.7K   592   8   1
Simple solution for PDF performance from Image files of any kind
In this article, a simple solution has been provided for PDF performance from Image files of any kind.

Image 1

Introduction

In some of my projects, the PDF performance of graphics was required. The only problem that occurred was that I did not have any instrument for conversion of the images to PDF at the moment. And as usual, I set off in quest of happiness into CodeProject. And as usual, I was lucky to find the fine article, Simplest PDF Generating API for JPEG Image Content, by Нао Нu. And once again, the code was fine and the idea was fine, but the demo presentation provided was a little bit poor.

Therefore, I've dared to improve the performance for more simple use with a few clicks using standard MFC procedures and to extend the range of the images from jpeg upto any images valid for the class CImage.

Background

I borrowed the Jpeg2PDF.cpp codes from the article by Нао Нu. In the codes above, I've just put in comments some parts that are not fit for a new MFC project. All other performance has been arranged with the standard MFC Application Wizard using class CImage for all kinds of image handling.

Before you start building the project provided, it is highly recommended to have a look to the demo presentation enclosed in order to get an idea of the output expected.

Demo Explanations

The executable PdfFromImg.exe in the PdfFromImgDemo directory has been built with MSVS-2015 pro using the instruments of MSVS-2010. Therefore the PdfFromImg.exe is valid even for Windows-XP. Just if you want to start in Windows-XP, please do not forget to insert mfc100.dll, mfc100u.dll, msvcp100.dll, msvcr100.dll files into the PdfFromImgDemo directory (or into the ..windows\system32 directory).

Some menu and some special Accelerator keys arranged in order to demonstrate the PdfFromImg project implementation:

Image 2

  • Menu File->Append Image(s) - selecting the file(s) with the image to append to the list in the ListBox control
  • Menu File->Insert Image(s) - selecting the file(s) with the image to insert into the list in the ListBox control before the item is selected
  • Menu File->Delete Image(s) - deleting the file(s) selected from the list in the ListBox control
  • Menu File->Clear All - deleting all the files from the list in the ListBox control
  • Menu File->Create PDF - creating the PDF file from all the files from the list in the ListBox control (also OK button)
  • Menu File->Play - play last PDF file created
  • ListBox Control - list of the names of the files containing images to create PDF
  • OK button - creating the PDF file from all the files from the list in the ListBox control (also Menu File->Create PDF)
  • Cancel Button - just close the dialog
  • Portrait Button - PDF performance to create as portrait
  • Landscape Button - PDF performance to create as landscape
  • Progress Control - specifying number of images accepted in PDF at the moment while creating

The order of working is as follows:

  • Once started, the list of files' names from default PDFImages path from the root directory appeared. You may edit this list if required with Menu commands: Append Image(s), Insert Image(s), Delete Image(s) and Clear All.
  • With the Append Image(s) and Insert Image(s) commands, the standard FileDialog box appeared. This FileDialog box has the multiselection property, therefore you may append and insert in the list the image files of any kind from anywhere (just keep in mind that the sizes of images must be more or less equal, better exactly equal).
  • With the Create PDF command (or OK button pressed), the standard FileDialog box for saving appeared. The default PDFFiles path from the root directory suggested for saving. It is not compulsory, you may save the PDF file with any name in any place where you like. After creating completion, the MessageBox with the pathname created appeared.
  • The last PDF created may be started with File->Play command.

Thus, if you've originally prepared all image files required for your PDF in the default PDFImages folder, once started, just press the OK button and you'll receive the PDF file in the root directory in one click.

All the commands above also are available on Help Dialog Box, just press F1 button:

Image 3

The Help dialog box is non-modal, therefore you may select any command in the list control and press the OK button or double click Mouse Left button.

In the About dialog box, the reference to the original article by Hao Hu is presented:

Image 4

Building Notes

Solution configuration must be installed as Release and the platform to be x86.

The project provided has been developed with MSVS-2012 pro using the instruments of MSVS-2010. Therefore, the EXE files are valid even for Windows-XP. If you do not need to run the application in Windows-XP, just change the instruments to MSVS-2012 .

The default coding property is UNICODE; nevertheless MBCS coding is also available, just change the property.

Even if you are working for the first time with MSVS, just select menu Debug->Start without debugging and the program PdfFromImg.exe should start building and working.

Project Source and Data Storage

Standard source codes in PdfFromImgproj path have been created with the standard MFC Application Wizard:

  • PdfFromImg.cpp - defines the standard class behaviors for the application
  • PdfFromImgDlg.cpp - implementation of the standard CDialogEx class; messages handling procedures created by the author using standard MFC Application Wizard procedures
  • Jpeg2PDF.cpp - borrowed from the article "Simplest PDF Generating API for JPEG Image Content" by Нао Нu; I've just put in comments some parts not fit for new MFC project.
  • PdfFromImg.rc and resource.h - menu, dialog, accelerator resources created by the author using the standard Resource Wizard procedures

The default PDFImages path in the root directory containing the sample images have been obtained from my former article, "Masking Texture in OpenGL using Class CImage". It is not compulsory and you may change the contents of this path as you like or prepare some other directory for images storage.

The result PDF created suggested for saving in the root directory. It is not compulsory, you may save the PDF file with any name in any place that you like.

Codes Explanations

All the Menu and Accelerator commands have been done with standard MFC AppWizard technologies. Therefore, I feel nothing to explain better than in the standard tutorials. The multiple file selection use in the standard FileDialog Box was explained in my former Code Project article, "Avi from Image Files in One Click".

All the image files are converting to jpg format in the temporary path using class CImage:

C++
for(int i=0; i< m_imgArray.GetSize(); i++)            //for all file names in list box:
{
CString fString = m_imgArray.GetAt(i);                //pick up current file name
CString strExt = GetFileExtention(fString);           //extract the extension
CString strN = strFormat(_T("\\%000d.jpg"), i+1);     //create numerated file name
CString tString = tempDir + strN;                    
if(strExt == _T("jpg"))                               //if extension is jpg
	CopyFile(fString, tString, FALSE);                //just copy current file
else                                                  //otherwise
{
	CImage img;
	img.Load(fString);                                //load file as CImage
	img.Save(tString);                                //save CImage as jpg
}
}  

Pls note that the pathname of the file selected appended to m_listArray at one time as the file name appended to the m_listBox. Thus, it is possible to create the PDF from the images of any kind located in separate directories.

As for struct Jpeg2PDF in Jpeg2PDF.cpp, I think better to refer to the author of the article by Нао Нu.

Your Own Applications Development Using the Project Provided

You may pick up all this project, rename it with the project of my former CodeProject article, "MFC Project from Existing Code in One Click" and combine and improve the codes as you like.

Or you may pick up the Jpeg2PDF.cpp file from this project and include it in any of your images handling projects with menu Project->Existing Item.

Or you may simply use the PdfFromImg.exe from the demo provided to arrange your images in PDF file.

Your references to my codes, if any, should be highly appreciated.

Points of Interest and Acknowledgements

Sure you can use the technology of the images collected into PDF with usual monster programs as PDFElement or some other PDF handling progs. But all these sophisticated programs do it with the closed sources. Therefore, you cannot arrange some tricks and effects as you like.

Many thanks to Нао Нu for his fine job.

History

  • 30th April, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDid you check Irfanview? Pin
bandikah3-May-22 0:53
bandikah3-May-22 0:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.