Bite my bytes

What I learn by day I blog at night.

  Home :: Contact :: Syndication  
  850 Posts :: 5042 Comments :: 235 Trackbacks

Search

Recent Comments.

Recent Posts

Most popular posts

Categories

My Projects

Archives

Stuff


Copyright © by David Vidmar
 
Contact me!
 
LinkedIn Profile
 
 
 
Server Monitor

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?

# re: Printing PDF documents in C# 7/31/2008 8:52 AM Turkey
Thanx You.. Perfect Docs

# re: Printing PDF documents in C# 10/10/2008 5:11 AM michael
is it possible to select a printer to use in printing using this? :)

Thanks!

# re: Printing PDF documents in C# 12/18/2008 3:52 PM egon
Yes it's possible to select a printer. I've used the following code:

using System;
using System.Diagnostics;
using System.Threading;

using NDde;
using NDde.Client;

namespace egrath.tools.dlxprinter.pdf
{
public sealed class PdfPrinter
{
private DdeClient m_Client;

public PdfPrinter()
{
m_Client = new DdeClient( "Acroview", "Control" );
}

public bool Connect()
{
bool connected = false;
bool tryAgain = false;

// Trying to connect to Acrobat DDE Server
while( ! connected || tryAgain )
{
try
{
m_Client.Connect();
connected = true;
tryAgain = false;

Console.Out.WriteLine( "PdfPrinter.Connect(): Connected to DDE" );
}
catch( DdeException de )
{
Console.Out.WriteLine( de.Message );
// Start Acrobat
Process proc = new Process();
proc.StartInfo.FileName = "AcroRd32.exe";
proc.Start();
proc.WaitForInputIdle();

tryAgain = true;
}
}

return m_Client.IsConnected;
}

public bool PrintDocument( string fileName, string printerName )
{
Console.Out.WriteLine( "PdfPrinter.PrintDocument(): got request to print {0}", fileName );

string escapedFileName = fileName.Replace( "\\", "\\\\" );
string escapedPrinterName = printerName.Replace( "\\", "\\\\" );

if( ! OpenDocument( fileName ))
return false;

try
{
string ddeExecutionString = String.Format( "[FilePrintTo(\"{0}\",\"{1}\",\"\",\"\")]",
escapedFileName, printerName
);

m_Client.Execute( ddeExecutionString, 50000 );

}
catch( DdeException )
{
return false;
}
finally
{
m_Client.Execute( String.Format( "[DocClose(\"{0}\")]", escapedFileName ), 50000 );
}

return true;
}

public bool OpenDocument( string fileName )
{
Console.Out.WriteLine( "PdfPrinter.OpenDocument(): got request to open {0}", fileName );

if( ! m_Client.IsConnected )
Connect();

try
{
m_Client.Execute( String.Format( "[DocOpen(\"{0}\")]", fileName.Replace( "\\", "\\\\" )), 50000 );
}
catch( DdeException )
{
return false;
}

return true;
}
}
}

# re: Printing PDF documents in C# 2/2/2009 8:31 PM Ian
DDE functionality was removed in Acrobat Reader 9.0. Do you have a new solution?

# re: Printing PDF documents in C# 3/24/2009 1:50 PM Tommy2Tone
I did not read this article in detail, so maybe I am missing something - but we have a PC which has a user logged on constantly that we can use to create PDF/s and send to a printer. The following command from VB sends the document to a specified printer.

Shell("AcroRd32.exe" /t "document.pdf" \\{server}\{printer}, AppWinStyle.MinimizedNoFocus, False)

The /t indicates to print the document.pdf to the printer specified in the command line. I have not tested this with version 9. Maybe this is too simple


# re: Printing PDF documents in C# 4/8/2009 6:58 AM BOSOTA
O my GOD! I feel that this page contain many information which i need, but i can't understand it because i very bad learn English in the school... =(((
So.. Where are my English dictionary...

# re: Printing PDF documents in C# 4/23/2009 8:58 PM orked
Error 1 The type or namespace name 'NDde' could not be found (are you missing a using directive or an assembly reference?) D:\graduation project\WebSite27\Default.aspx.cs 14 7 D:\graduation project\WebSite27\
this message appear when i run the problem.
how i can solve it

# re: Printing PDF documents in C# 6/23/2009 11:15 AM test
test

Post Feedback

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