Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem printing PDF with ShellExecute

632 views
Skip to first unread message

Mark Redman

unread,
Sep 5, 2002, 4:24:30 PM9/5/02
to
Hi All,

I need to print a pdf, I found the code supplied recently in this group by
Peter Below but cannot seem to get it to work, Searching the archives I have
seen this more than once so I am obviously doing something wrong. I have
tried this on WinXP and Win2000 Server to no avail?

The code is shown below, ShellExecute returns 42?

Any Ideas

I obviously have Acrobat Reader installed.

procedure TForm1.Button1Click(Sender: TObject);
var
Device : array[0..255] of char;
Driver : array[0..255] of char;
Port : array[0..255] of char;
S, strPDFFile : String;
hDeviceMode : THandle;
nErrorCode : Integer;
begin
strPDFFile := 'c:\Test.pdf';
if PrintDialog1.Execute then
begin
Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
S:= Format('"%s" "%s" "%s"',[Device, Driver, Port]);
nErrorCode := ShellExecute( handle, 'printto', PChar(strPDFFile),
Pchar(S), Nil, SW_HIDE);
ShowMessage('Error Code = ' + IntToStr(nErrorCode));
end;
end;

Thanks

Mark Redman


Mark Redman

unread,
Sep 5, 2002, 5:16:18 PM9/5/02
to
Good thinking Batman!

That was indeed the problem.

Thank you very much.

Mark Redman

"Gary Williams" <no.mail...@nonexistant.domain.com> wrote in message
news:3d77c24a$1...@newsgroups.borland.com...
>
> "Mark Redman" <Ma...@RedmanSoftware.com> wrote in message
> news:3d77...@newsgroups.borland.com...


> > Hi All,
> >
> > I need to print a pdf, I found the code supplied recently in this group
by
> > Peter Below but cannot seem to get it to work, Searching the archives I
> have
> > seen this more than once so I am obviously doing something wrong. I have
> > tried this on WinXP and Win2000 Server to no avail?
> >
> > The code is shown below, ShellExecute returns 42?
> >
> > Any Ideas
>

> please refer to
>
> http://www.gw.ro/pascal/AcrobatReaderBug.html
>
> I'm betting you're running your application from the IDE.
>
> -Gary
>
>
>


Gary Williams

unread,
Sep 5, 2002, 4:45:07 PM9/5/02
to

"Mark Redman" <Ma...@RedmanSoftware.com> wrote in message
news:3d77...@newsgroups.borland.com...
> Hi All,
>
> I need to print a pdf, I found the code supplied recently in this group by
> Peter Below but cannot seem to get it to work, Searching the archives I
have
> seen this more than once so I am obviously doing something wrong. I have
> tried this on WinXP and Win2000 Server to no avail?
>
> The code is shown below, ShellExecute returns 42?
>
> Any Ideas

please refer to

Mark Redman

unread,
Sep 5, 2002, 5:45:23 PM9/5/02
to
I now have the printing working although acrobat is opening, how do I
prevent this?

Mark Redman


Gary Williams

unread,
Sep 5, 2002, 6:46:54 PM9/5/02
to
"Mark Redman" <Ma...@RedmanSoftware.com> wrote in message
news:3d77...@newsgroups.borland.com...
> I now have the printing working although acrobat is opening, how do I
> prevent this?


I know of two ways to print a PDF programmatically. Neither seems to work
when run under the IDE on Win2K. The first method works properly when run
outside the IDE, but the second method will leave Acrobat Reader running on
the desktop.


uses
ShellAPI, Printers;

type
TPrinterInfo = record
Device: String;
Driver: String;
Port: String;
end;

TStringBuffer = array[0..MAX_PATH - 1] of Char;

function GetPrinterInfo: TPrinterInfo;

var
DeviceBuffer: TStringBuffer;
DriverBuffer: TStringBuffer;
PortBuffer: TStringBuffer;
DeviceMode: THandle;

begin
Printer.GetPrinter(DeviceBuffer, DriverBuffer, PortBuffer, DeviceMode);

Result.Device := DeviceBuffer;
Result.Driver := DriverBuffer;
Result.Port := PortBuffer;
end;

// This does not appear to work under the IDE on Win2000
procedure PrintPDFDocument1(const FileName: String);

var
Params: String;

begin
with GetPrinterInfo do
Params := Format('"%s" "%s" "%s"', [Device, Driver, Port]);

ShellExecute(0, 'printto', PChar(FileName), PChar(Params), nil, SW_HIDE);
end;

// This does not appear to work under the IDE on Win2000
// This will leave Acrobat running on the desktop.
procedure PrintPDFDocument2(const FileName: String);

var
Params: String;

begin
with GetPrinterInfo do
Params := Format('/t "%s" "%s" "%s" "%s"', [FileName, Device, Driver,
Port]);

ShellExecute(0, 'open', 'AcroRd32', PChar(Params), nil, SW_HIDE);
end;

const
FileName = 'C:\Temp.pdf';

procedure TForm1.Button1Click(Sender: TObject);

begin
PrintPDFDocument1(FileName);
end;

procedure TForm1.Button2Click(Sender: TObject);

begin
PrintPDFDocument2(FileName);
end;

-Gary

Gary Williams

unread,
Sep 5, 2002, 9:10:36 PM9/5/02
to
Aaaaargh!

I spent the last three hours conducting extensive tests with Acrobat Reader
version 3.02, 4.05, 5.0, and 5.05, running both under the Delphi IDE and
directly from Explorer, under Windows 2000. The results were surprising, to
say the least.

View the fruit of my research here:

http://www.gw.ro/pascal/AcrobatReaderHell.html

-Gary

Gary Williams

unread,
Sep 5, 2002, 10:02:15 PM9/5/02
to
"Mike Williams (TeamB)" <mi...@remove.aps-soft.com> wrote in message
news:Xns9280DB56DB...@207.105.83.65...
> On 05 Sep 2002, "Gary Williams" <no.mail...@nonexistant.domain.com>

> wrote:
>
> > I spent the last three hours conducting extensive tests with Acrobat
> > Reader version 3.02, 4.05, 5.0, and 5.05, running both under the
> > Delphi IDE and directly from Explorer, under Windows 2000. The
> > results were surprising, to say the least.
>
> Keep in mind that a big part of this has to do with how the file type is
> registered to handle "Open", "PrintTo", and "Print" (which is a registered
> for PDF on my system that you don't appear to be using). I'm not sure
that
> you even need to call AcroRd32 if things are registered properly. If
> various combinations are required for different versions of acrobat it
> could be because of Adobe not registering things properly.

I know it shouldn't be necessary to call AcroRd32 directly, but I wanted to
see if there would be any variation in behavior based on how it was called.

After I uploaded the web page, it did occur to me that I didn't try the
generic 'print' shell command. I will try that tomorrow. I'll also try to
document the registry settings for each version of Acrobat Reader that I
have available.

My goal is to compile everything I can about AR and Delphi together on that
one page so I can refer people to it when they post about having problems.

I would be interested in finding out if people using other versions of AR or
Windows (particularly XP) get consistent or differing results compared to my
tests.

> With some more
> work you might be able to check the registration yourself to decide which
> method to use up front.

That would be a good weekend project.

-Gary

Mark Redman

unread,
Sep 6, 2002, 5:43:48 AM9/6/02
to
Thanks for the Reply and and all the work you've put in to this,

The other thing I need to do is possibly print one or more PDF's
consecutively, it seems that once Acrobat is open, the second PDF will not
print?

I will also have a go and see if I can work things out, I didnt think this
PDF printing would be such an issue.

Mark Redman

"Gary Williams" <no.mail...@nonexistant.domain.com> wrote in message

news:3d780093$1...@newsgroups.borland.com...

0 new messages