?
Solved

Printing PDF Files

Posted on 2005-03-21
18
Medium Priority
?
498 Views
Last Modified: 2010-04-05
I am using Delphi 7.0 /ADO/MS Access DB.


I need to print some Adobe Acrobat files, which the path to resides in
a database field. Each files uses the same path, just different file
name.


I Then use the following to print:


  ShellExecute( 0, 'print', PAnsiChar(PDFPath + '\' +
frmData.tblParents.FieldByName­('File').asString), nil, nil, SW_NORMAL);


to print the files. Which seems to work great when sent to a local
printer. However, to print to a network printer, I sometimes miss some
of the children, or the parent for that matter.


Any help on this would be greatly appreciated!


See entire code below:


procedure TfrmMain.btnPrintParentClick(S­ender: TObject);
var
 PrintChildren: Boolean;
begin
 if FileExists(PDFPath + '\' +
frmData.tblParents.FieldByName­('File').asString) then
 begin
  frmData.qryGetChildren.Close;
  frmData.qryGetChildren.SQL.Cle­ar;
  frmData.qryGetChildren.SQL.Add­('Select * From Children Where
ParentId=:AParentId');
  frmData.qryGetChildren.Paramet­ers.ParamByName('AParentId').D­ataType:=
ftInteger;
  frmData.qryGetChildren.Paramet­ers.ParamByName('AParentId').V­alue:=
frmData.tblParents.FieldByName­('Id').asInteger;
  frmData.qryGetChildren.Prepare­d:= True;
  frmData.qryGetChildren.Open;
  if frmData.qryGetChildren.RecordC­ount > 0 then
  begin
   if MessageDlg('Print Child Drawings?', mtConfirmation, [mbYes,
mbNo], 0) = mrYes then
    PrintChildren:= True
   else
    PrintChildren:= False;
  end
  else
   PrintChildren:= False;
  frmPrinting.Show;
  frmPrinting.Update;
  Application.ProcessMessages;
  ShellExecute( 0, 'print', PAnsiChar(PDFPath + '\' +
frmData.tblParents.FieldByName­('File').asString), nil, nil, SW_NORMAL);
  if PrintChildren = True then
  begin
   frmData.qryGetChildren.First;
   while not frmData.qryGetChildren.Eof do
   begin
    if FileExists(PDFPath + '\' +
frmData.qryGetChildren.FieldBy­Name('File').asString) then
    begin
    ShellExecute( 0, 'print', PAnsiChar(PDFPath + '\' +
frmData.qryGetChildren.FieldBy­Name('File').asString), nil, nil,
SW_NORMAL);
    end //if FileExists(PDFPath + '\' +
frmData.qryGetChildren.FieldBy­Name('File').asString) then
    else
     ShowMessage('CHILD DRAWING : ' + #10#13 + PDFPath + '\' +
frmData.qryGetChildren.FieldBy­Name('File').asString + #10#13 + ' does
not exist!');
    frmData.qryGetChildren.Next;
   end; //while not frmData.qryGetChildren.Eof do
   frmData.qryGetChildren.Next;
  end;
  frmData.qryGetChildren.Close;
 end
 else
 ShowMessage('PARENT DRAWING : ' + #10#13 + PDFPath + '\' +
frmData.tblParents.FieldByName­('File').asString + #10#13 + ' does not
exist!');
 frmPrinting.Hide;
 frmPrinting.Close;
 Application.ProcessMessages;
end;


0
Comment
Question by:shaneholmes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 9
18 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 13593665
Before printing, set the current drive and path to that of the PDF file:

From http://www.cpcug.org/user/clemenzi/technical/Languages/FileIO.htm
See SysUtils / File-management routines and System / I/O routines

Change Drive  
      ChDir('c:')  
 
Change Directory  
      SetCurrentDir('c:\path')
      ChDir('c:\path')  
0
 
LVL 11

Author Comment

by:shaneholmes
ID: 13593788
Allready do that, when I pass in the PDFPath Variable....

  PDFPath + '\' + frmData.tblParents.FieldByName­('File').asString

See code fragment i posted .....

sholmes
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13593835
<<Allready do that when I pass in the PDFPath Variable>>

No you aren't explicity issuing these commands PRIOR to invoking the ShellExecute.  I don't think that passing the path does exactly the same thing as changing the current drive and current directory.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Author Comment

by:shaneholmes
ID: 13594281
Nope - I get tthe same thing.....


if FileExists(PDFPath + '\' + frmData.tblParents.FieldByName('File').asString) then
 begin
  ChDir(ExtractFileDrive(PDFPath));
  SetCurrentDir(PDFPath);
  ChDir(PDFPath);  
  .
  .
  .


 sholmes
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13594590
What database access method are you using?

If using ADO,
frmData.qryGetChildren.RecordC­ount <> 0

doesn't provide as reliable an indicator of records as:
frmData.qryGetChildren.EOF
0
 
LVL 11

Author Comment

by:shaneholmes
ID: 13594666

I am getting the info from the database just fine. How I know all this, is that the program runs great on a local machine, printing the PDFS to a local printer. The problem arrises when printing accross the network to a network printer. Then the children get lost.

Sholmes
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13595064
1. What do the fields in the database look like (data)?
2. Where is PDFPath being built and what does it look like (data)?
3. What does the directory structure look like for parent documents and child documents?  
4. Is the directory structure the same locally and on the server?
0
 
LVL 11

Author Comment

by:shaneholmes
ID: 13595106
Also, I used CodeSite to follow the info. I watched as each record was pulled from the table (both parent & child records) and diplayed in codesite. So its getting the path & file names correctly. Again, it will print the Parent & maybe one more more child. There seams to be no consistency

sholmes
0
 
LVL 11

Author Comment

by:shaneholmes
ID: 13595111
To tell you the truth, i would also like to get away from having to print using Adobe Acrobat. But that may seem in feasible

Sholmes
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13595139
So SOME children print and other children DO NOT print?!?  Is that correct?

My question was geared toward some difference between the location of the parent and child documents or the means by which the path is specified (missing mapped drive letter).
0
 
LVL 11

Author Comment

by:shaneholmes
ID: 13595206
No, all parent & children PDF files reside in the same place.

Yes, there is no consistency in the printing.

One time, iot will print the parent - and of the three children. the first two
Next it may print the Parent and only the first child

Next it may print the parent & only the last two children.

It is so weird - and hard to pin point the problem!

SHolmes
0
 
LVL 46

Accepted Solution

by:
aikimark earned 500 total points
ID: 13595534
This may be a timing problem.  Add a Sleep(10000) after each ShellExecute.

For instance, if the network printer device were configured to print directly to the printer instead of spooling, then it is possible that the printer's internal buffers can't keep up with the demand.
0
 
LVL 11

Author Comment

by:shaneholmes
ID: 13595913
I tried that as well. that was my initial guess, and i still get the same thing. Im gonna do some more network testing and see what I get.
I'll get back to you with this one.

Sholmes
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13596352
look closely at how the printer device is defined (direct print vs. spool).
look at the size of the spool file.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13597424
To help debug this, add
longintegervariable = ShellExecute();

or use GetLastError (interpreted with SysErrorMessage)

=================================
Is the current process/window handle = 0?

0
 
LVL 11

Author Comment

by:shaneholmes
ID: 13597523
No, it was not 0, it was 42.

sholmes
0
 
LVL 46

Expert Comment

by:aikimark
ID: 13597700
<<No, it was not 0, it was 42.>>

Then why are you using "ShellExecute( 0, "?
0
 
LVL 11

Author Comment

by:shaneholmes
ID: 13606062
Yep, i eventually solved it by beefing up the sleep functionality. Sleep after each shellexecute then sleep after all shellexecutes for

(1000 * numberOfChildren) + 1000  Note: Additional 1000 for parent.

This gives the procedure ample amount of time to allow the program to process all print requests, before going into the close adobe code.

sholmes
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question