Gabriel
Gabriel

Reputation: 969

Sync pdf print and standard print

i need to print a pdf file, a standar print, other pdf file, other standar print, and so on. But, when i sent to the printer the sheets are mixed.

i desire:

   PDF
   PrintPage
   PDF
   PrintPage
   PDF
   PrintPage

but, i got (for example):

   PDF
   PDF
   PrintPage
   PrintPage
   PrintPage
   PDF

I'm using the following code to achive the task:

while( ... ) {
    ProcessStartInfo starter = new ProcessStartInfo("path to acrobt32.exe", "/t mypdf001.pdf");
    starter.CreateNoWindow = true;
    starter.RedirectStandardOutput = true;
    starter.UseShellExecute = false;
    Process process = new Process();
    process.StartInfo = starter;
    process.Start();


    PrintDocument pd = new PrintDocument();
    pd.DocumentName = "Work";
    pd.PrintPage += new PrintPageEventHandler(pd_PrintPageHandler);
    pd.Print();
}

Any help will be welcome. thanks.

Upvotes: 1

Views: 1851

Answers (3)

Kevin Stricker
Kevin Stricker

Reputation: 17388

Since you're using an external process to print the PDFs it might help to wait for that process to exit in order to keep the print operations synchronized.

i.e. After the call to the asynchronous:

process.Start();

Add a call to process.WaitForExit(); to keep things running in order.

You probably do need to do the same for the PrintDocument. In that case, you should be able to just block the thread until the OnEndPrint event is triggered: example

Upvotes: 1

ChrisLively
ChrisLively

Reputation: 88072

ProcessStartInfo runs asynchronously. So you your kicking off 1 or more acrobat32 exes, each of which take time to load and run their print function. In the meantime your PrintDocument class is running it's own set of print procedures... So all of the docs are showing up in an unpredicatble order.

See this: Async process start and wait for it to finish

and this: http://blogs.msdn.com/b/csharpfaq/archive/2004/06/01/146375.aspx

You'll need to start acrobat, wait for it to finish. Then start your PrintDocument (whatever that is) and wait for it to finish. Rinse and Repeat.

PrintDocument looks like it's asynchronous as well... due to the event handler call, but that's hard to tell for sure.

Upvotes: 1

Mridul Kashatria
Mridul Kashatria

Reputation: 4197

I cannot fully understand the issue from this small example but my guess is that the pd.Print() method is asynchronous.

You want to make the printing synchronous. The best way would be to wrap the code in a function and call that function from the pd_PrintPageHandler which I assume is called when the page gets printed.

A quick example to show what I mean,

function printPage(pdfFilePath)
{
    ProcessStartInfo starter = new ProcessStartInfo("path to acrobt32.exe", pdfFilePath);
    starter.CreateNoWindow = true;
    starter.RedirectStandardOutput = true;
    starter.UseShellExecute = false;
    Process process = new Process();
    process.StartInfo = starter;
    process.Start();


    PrintDocument pd = new PrintDocument();
    pd.DocumentName = "Work";
    pd.PrintPage += new PrintPageEventHandler(pd_PrintPageHandler);
    pd.Print();

}

and in the pd_PrintPageHandler method, call this printPage function with the next PDF file.

Upvotes: 2

Related Questions