![]() |
|
#1
|
|||
|
|||
|
How to open a .pdf file from a win32 console application
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 |
|
#2
|
|||
|
|||
|
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. Code:
//
// 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;
}
Regards, Dave Last edited by davekw7x; 10-09-2009 at 04:29 PM. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|