Bite my bytes

What I learn by day I blog at night.

  Home :: Contact :: Syndication  
  894 Posts :: 5822 Comments :: 235 Trackbacks

Search

Recent Comments.

Recent Posts

Most popular posts
in last 180 days

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?

# 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

# re: Printing PDF documents in C# 8/23/2009 2:43 AM laptop tamiri
Thanks Good Share very Mach

# re: Printing PDF documents in C# 8/26/2009 11:14 PM Doug
FYI, the commandline option offered by Tommy2Tone does indeed work with Adobe 9, however it does leave the Adobe window open. Only Reader is required (thats all I have).

# re: Printing PDF documents in C# 8/28/2009 9:31 PM John LeHawk
See below. It is easy and usable. Use Forensictools.net PDF Image Printer DLL. no adobe needed. IT IS FREE


--------------------
If you need to print pdf or image file use ForensicTools.NET PDF /Image Printer DLL. It is fully FREE. To get DLL send email to us, we will send you DLL. It easy to use. Like

sendToPrinter(PrinterName, FileLocation).

You dont have to have ADOBE PDF or other tools.

# re: Printing PDF documents in C# 9/29/2009 11:16 AM Roel
Dear sir,

Can you send me the dll?


--------------------------------------------------------
# re: Printing PDF documents in C# 8/28/2009 9:31 PM John LeHawk
See below. It is easy and usable. Use Forensictools.net PDF Image Printer DLL. no adobe needed. IT IS FREE


--------------------------------------------------------------------
If you need to print pdf or image file use ForensicTools.NET PDF /Image Printer DLL. It is fully FREE. To get DLL send email to us, we will send you DLL. It easy to use. Like

sendToPrinter(PrinterName, FileLocation).

You dont have to have ADOBE PDF or other tools.
-----------------------------------------------------------------------

best regards,
Roel

# re: Printing PDF documents in C# 10/1/2009 4:08 PM mark
Can that DLL be mailed to me too, please?

# re: Printing PDF documents in C# 10/2/2009 10:31 AM Daniel
Can you please send em the DLL?
Thanks

# re: Printing PDF documents in C# 11/2/2009 3:07 PM Dan Bachmann
The command line arguments to Adobe Acrobat Reader work great. I've just tried it in version 9.0.

# re: Printing PDF documents in C# 11/4/2009 2:12 PM DimG
Can you send me the ForensicTools.NET PDF /Image Printer DLL?

# re: Printing PDF documents in C# 11/7/2009 9:28 PM Parminder
Can you email the DLL to me

Thank you

# re: Printing PDF documents in C# 11/10/2009 6:45 PM Monika
Hi Sir,
can you send me the dll at monika_kur@yahoo.com

regards


# re: Printing PDF documents in C# 12/2/2009 2:26 PM Keith Passaur
Could you please send me the dll

Thank you

# re: Printing PDF documents in C# 12/24/2009 7:37 AM best poker games online
You can specify the font you want on the XSLT. When the PDF renders it will honor the font if the font is available.I have not used SVG with nFOP. What are you trying to do?

# re: Printing PDF documents in C# 12/26/2009 5:04 PM jeffersonkuo
Dear sir,

Can you send me the ForensicTools.NET PDF /Image Printer DLL?
(at jeffersonkuo999@gmail.com )

regards.



# re: Printing PDF documents in C# 12/29/2009 12:04 PM kirti Darji
Please send me ForensicTools.NET PDF /Image Printer DLL on given Email

# re: Printing PDF documents in C# 1/26/2010 8:25 AM Martin Zeller
Dear sir,

please send me the ForensicTools.NET PDF /Image Printer DLL - I sent an email at the home, but never got response.

Thank you very much!!!

# re: Printing PDF documents in C# 1/26/2010 10:43 AM zhuy
Dear sir,
can u send me this ForensicTools.NET PDF /Image Printer DLL ?
e-mail:nicolas860614@hotmail.com
thank u very much

# re: Printing PDF documents in C# 1/31/2010 6:35 AM ppo plans
A few years ago, I would have killed for a simple service like FreeMyPDF. Countless times people within my company would send me PDF files that had all sorts of unnecessary protections which frequently made it impossible for me to work with them the way they requested: "Hey can you print that off and bring it to the meeting?" "You locked it down with a password. I can't print it." "Oh really? Huh. I dunno what the password is."

Post Feedback

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