Bite my bytes

What I learn by day I blog at night.

  Home :: Contact :: Syndication  
  776 Posts :: 3633 Comments :: 235 Trackbacks

Search

Recent Comments.

Recent Posts

Most popular posts

Categories

My Projects

Archives

Stuff


Copyright © by David Vidmar
 
Contact me!
 
LinkedIn Profile
 
 

I never though about it, but printing (not creating!)  PDF documents from code without user intervention is not a trivial task. Nearly everyone has Adobe Reader or an alternative PDF viewer installed doesn't help a lot.

Let's look at the options:

But if you dig deep enough you will find out that there is a way to automate printing. By using DDE, of all things (if you don't know what DDE is, you probably didn't use a phone with a rotary dial, either).

Here is a sample code I used to print PDF files:

bool tryStart = false;
bool connected = false;
do
{
    try
    {
        // Connect to the server.  It must be running or an exception will be thrown.
        client.Connect();
        connected = true;
    }
    catch (DdeException)
    {
        // try running Adobe Reader
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.FileName = "AcroRd32.exe";
        p.Start();
        p.WaitForInputIdle();
        // try this once
        tryStart = !tryStart;                            
    }
} while (tryStart && !connected);

// sucessfully connected?
if (connected)
{
    // Synchronous Execute Operation
    client.Execute("[DocOpen(\"C:\\Test.pdf\")]", 60000);
    client.Execute("[FilePrintSilent(\"C:\\Test.pdf\")]", 60000);
    client.Execute("[DocClose(\"C:\\Test.pdf\")]", 60000);
    client.Execute("[AppExit]", 60000);
}

Since .NET doesn't natively support DDE, I used free .NET library that was published on GotDotNet, which was moved to MSDN Code Gallery. The sample was not ported so it isn't available for download, which is a real shame. There is another .NET DDE library on CodePlex, which will work too.

Next best thing is an C++ article on CodeProject that inspired the code above.

 

Technorati tags: , , , , ,
Posted on Monday, April 14, 2008 10:46 PM | Filed under: Developement |

 

Feedback

# re: Printing PDF documents in C# 5/13/2008 9:20 AM Resimler
thnx

# re: Printing PDF documents in C# 6/2/2008 11:57 PM Coz
Greetings!

Is there a way to pass-in the Printer Name using the GotDotNet library you used? I have downloaded the CodePlex library but I can't get to the documentation.

Would you have the name of the GotDotNet library so that I may search for it in MSDN?

Thank you for your time!

Coz

# re: Printing PDF documents in C# 6/5/2008 1:36 PM David
I'm not sure, but I would say that it will print to default printer.

# re: Printing PDF documents in C# 6/9/2008 10:19 PM Chad
Code looks good, but what is client a instance of?

Can you upload your c# file?

# re: Printing PDF documents in C# 6/9/2008 10:40 PM David
Chad, "client" is an instance of DDE client object. I'm sorry, but I don't have my code anymore, it's been a throw away project. Not that I really throw away and of my code, but this time I did. :(

# re: Printing PDF documents in C# 6/28/2008 12:22 PM Deepak kataria
If we want to save .ps file.

Then what should we have to do?

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 4 and 5 and type the answer here: