Numerical Recipes Forum  

Go Back   Numerical Recipes Forum > Obsolete Editions Forum > General Computing and Open Discussions

Thread Tools Display Modes
Old 10-09-2009, 10:12 AM
MPD78 MPD78 is offline
Registered User
Join Date: Dec 2008
Location: Pittsburgh, PA
Posts: 196
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.

Reply With Quote
Old 10-09-2009, 10:57 AM
davekw7x davekw7x is offline
Registered User
Join Date: Jan 2008
Posts: 453
Originally Posted by MPD78 View Post 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

    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.



Last edited by davekw7x; 10-09-2009 at 05:29 PM.
Reply With Quote
Old 10-09-2009, 11:29 AM
MPD78 MPD78 is offline
Registered User
Join Date: Dec 2008
Location: Pittsburgh, PA
Posts: 196
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.

Reply With Quote
Old 08-24-2011, 03:47 PM
zjaved2 zjaved2 is offline
Registered User
Join Date: Aug 2011
Posts: 1
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


Reply With Quote
Old 06-27-2014, 04:15 PM
piciurica piciurica is offline
Registered User
Join Date: Jun 2014
Posts: 1
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,
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

All times are GMT -5. The time now is 10:15 PM.

Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.