How to open a .pdf file from a win32 console application
MPD78
10-09-2009, 09:12 AM
Hello all,
Can someone show me the correct code to open a .pdf file from a C++ console application?
What I want the program to do is simply open a .pdf file that corresponds to a numerical input.
For example,
Enter a 1 to view ".pdf on how to view a .pdf"
User enters 1 and the .pdf is displayed for the user to read, print, save, etc ...
My attempt so far has been to use the ShellExecute() function but I am not having any luck.
Thanks
Matt
davekw7x
10-09-2009, 09:57 AM
...open a .pdf file from a C++ console application...
For a console application, I think it's simpler to use the standard library function system() rather than a Windows-specific function and all of that "handle" stuff. My methodology is also portable to other compilers and other operating systems, which could be useful if you ever find your neck no longer under the crushing foot of the world's dominant operating system.
First of all, find the path of whatever pdf rendering program that is on your system.
On my Windows XP workstation it is "C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe" I discovered that by right-clicking on the "Adobe Reader" icon and selecting "Properties" The complete path name appears on the "Target" line.
Then, here's the program that I used. Note that I almost always put system-dependent parameters (such as the path to to the reader) in their own definitions, as I did here, rather than hard-coding them into statements where they are actually used. That way it's easy see what to alter if things change in my system.
//
// Illustration of using Adobe Reader from a C++ program
//
// Tested with Microsoft, Borland and GNU compilers on
// Windows XP.
//
// Notes:
// 1: The path name must be surrounded by \" \"
// 2: Forward slashes in the path name work just fine, with Microsoft, Borland
// and GNU compilers. Really.
//
//
// davekw7x
//
#include <iostream>
#include <string>
using namespace std;
int main()
{
string readername("\"C:/Program Files/Adobe/Reader 9.0/Reader/AcroRd32.exe\"");
string filename;
string cmd;
cout << "Enter the name of the pdf file: ";
getline(cin, filename);
cmd = readername + " " + filename;
cout << "cmd is " << cmd << endl; // For debugging purposes
system(cmd.c_str());
return 0;
}
At the program prompt, if the file is in the same directory from which you invoked my program, just enter the file name. If it is somewhere else, enter a path name (can be relative or absolute). Note that path names from the command line work perfectly well with forward slashes on my Windows XP box. If the path name and/or the file name contains spaces, use " " quote marks around the name. Of course if you know the file name in advance, just define it inside the program instead of asking the user to enter the name.
Regards,
Dave
MPD78
10-09-2009, 10:29 AM
Thanks again Dave.
Now all I have to do is modify the code slightly to allow the user to enter a number, store all of the pdf files in their needed locations, and I am done.
Thanks
Matt
zjaved2
08-24-2011, 02:47 PM
hey there
i'm also trying to do the same though i've added a few options for editing later on, the problem i'm facing is that my file happens to be in the same directory and folder yet it displays a msg "file not found" though it opens the adobe reader.
So my question is do i have to enter the complete location of the file, and whether the results will be different if i use the code in C++ 2008 or 2010
Regards
Zain
piciurica
06-27-2014, 03:15 PM
Hi Guys,
I try do make a database with C++ with my books. It is just for personal use only with the purpose of better accessing my digital books. By accessing I mean to select only the books with a given word/sequence of words in the title and then choose which one to open. In order to open the selected book, i used the code indicated by davekw7x. Many thanks! But i have the following question: after i open the file with the Adobe, i cannot control the console anymore. In order to be able to continue my menu selection in the console, i need to close the Adobe first. Is it a way to have both the pdf file opened and to be able to continue my application in the console (manipulating the console for selecting menus/submenus)?
Many Thanks,
Piciurica