Bite my bytes

What I learn by day I blog at night - A blog from Microsoft Consultant working from Ljubljana, Slovenia

  Home :: Contact :: Syndication  
  999 Posts :: 7691 Comments :: 235 Trackbacks

Search

Most popular posts
in last 30 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# 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/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."

# re: Printing PDF documents in C# 2/16/2010 11:48 AM Pavel Stanek
Dear sir,

Could you send me the ForensicTools.NET PDF /Image Printer DLL, please ?
(jahn.brown@seznam.cz)

Thank you very much!!!

Pavel

# re: Printing PDF documents in C# 2/18/2010 7:43 AM Saravanan
Hi,

Can you send me the ForensicTools.NET PDF /Image Printer DLL to my mail ID (hi2everybody06@gmail.com)

# re: Printing PDF documents in C# 3/3/2010 9:50 AM Th Nguyet
Dear sir,

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

regards.





# re: Printing PDF documents in C# 3/5/2010 7:41 AM Shalini
hi,
could you please send me the DLL, PLssssssss
my EmailId is : shalini.garg@fiserv.com

Thanks & Regards
Shalini Garg

# re: Printing PDF documents in C# 3/16/2010 12:31 PM laptop tamiri
Can you send me the ForensicTools.NET PDF /Image Printer DLL?

# re: Printing PDF documents in C# 3/29/2010 12:23 PM Etsub
it is the only reference for me so please add some additional information for me thanks

# re: Printing PDF documents in C# 4/2/2010 11:09 PM roger
Please send me the ForensicTools.NET PDF /Image Printer DLL @ schweppr@mindspring.com? Thanks Roger

# re: Printing PDF documents in C# 4/3/2010 5:29 AM Watcharagorn
Dear sir,

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

Thx & Regards.



# re: Printing PDF documents in C# 4/5/2010 11:10 PM Dave
Hello,
Has anyone come across the ForensicTools.NET PDF /Image Printer DLL? If so, can you send a copy to me at dave@customapps.net.

Thanks in advance for your help.

Regards,
Dave

# re: Printing PDF documents in C# 4/8/2010 2:40 PM rjy
this is a joke

# re: Printing PDF documents in C# 4/11/2010 6:28 PM sale
You can download ForensicTools.NET PDF library here: (at the bottom of page)
http://www.forensictools.net/Price.aspx

#  Printing PDF documents in C# 5/7/2010 7:04 PM balaji
sir im in need of a dll and coding to print a pdf document which i have created.so please send me the dll and the coding for that..

thanks

# re: Printing PDF documents in C# 5/12/2010 6:58 PM PRAD
Hi all ,

Please post the dll and code for priting dll if any get the solution .

# re: Printing PDF documents in C# 5/20/2010 12:37 AM dcowan
Here is a alternate solution that worked for me:

/// <summary>
/// Print PDF file silently using Acrobat SDK.
/// Requires:
/// 1. Add to file: using Acrobat;
/// 2. Add Reference to project. With Acrobat installed the following will appear under the COM tab:
/// Acrobat, version 1.0, c:\Program Files\Adobe\Acrobat 9.0\Acrobat\Acrobat.tlb
/// </summary>
/// <param name="fullFileName">Fully qualified name of PDF file to print.</param>
private void printPDF(string fullFileName)
{
try
{
AcroAVDoc doc = new AcroAVDoc();
doc.Open(fullFileName, string.Empty);
AcroPDDoc di = (AcroPDDoc)doc.GetPDDoc();
int pageCount = di.GetNumPages();
// Arguments to PrintPagesSilent:
// firstPage, lastPage, 3=PostScript level, 1=may contain binary data, 1=shrink page to fit
doc.PrintPagesSilent(0, pageCount-1, 3, 1, 1);
doc.Close(1); // 1 => don't save
}
catch(Exception ex)
{
showMessage("Exception while printing: " + ex.Message, true); // Display exception
}
}


# re: Printing PDF documents in C# 5/21/2010 4:40 PM AH
"Starting Adobe Reader with command line switch is only possible with very old version of the Reader and it doesn't work with current versions"

Not true. Works fine with Adobe Reader 9.

# re: Printing PDF documents in C# 7/28/2010 2:36 PM Dironn
Dear sir,

Could you send me the ForensicTools.NET PDF /Image Printer DLL?
--> dironn@msn.com

Thx in advance for your help

regards.

# re: Printing PDF documents in C# 8/12/2010 1:07 AM Sussel
Can you send me the ForensicTools.NET PDF /Image Printer DLL?


regards.




# re: Printing PDF documents in C# 8/25/2010 9:09 AM Hidden Friend
you can check this one too

http://waqarahmadbhatti.blogspot.com/

# re: Printing PDF documents in C# 8/25/2010 9:16 AM Hidden Friend
http://waqarahmadbhatti.blogspot.com/2010/08/silent-printing-pdf-file-in-c-and.html

# re: Printing PDF documents in C# 9/29/2010 9:51 PM Mimi
This is for dcowan:

I tried your code and is working. But how about printing recto-verso pages? I explain: I want to print on the both sides of paper, if the document has 4 pages I don't want to print 4 pages but 2 papers fulled on both sides.

Thx for ur help!

# re: Printing PDF documents in C# 10/18/2010 10:32 PM Yeong Shih
Could you please send me the ForensicTools.NET PDF /Image Printer DLL?

Thanks a lot!

# re: Printing PDF documents in C# 10/21/2010 11:54 AM Javier
Hi Sir,
can you send me the dll?

regards



# re: Printing PDF documents in C# 10/28/2010 10:53 AM Ju
Has anyone used the ForensicTools pdf print dll?

I have a download, but being suspecious do not want to call a dll from an unknown source.



# re: Printing PDF documents in C# 11/23/2010 10:09 PM James
Acrobat X seems to have killed DDE. Anyone concur?

# re: Printing PDF documents in C# 1/14/2011 6:59 AM ansar
hi can any one show me the way to download acrobat sdk.

# re: Printing PDF documents in C# 1/24/2011 10:52 PM Randy
@James: Adobe changed the server name for version 10. See http://blogs.adobe.com/pdfdevjunkie/2011/01/big-changes-in-the-dde-naming-convention-for-acrobat-x.html for details.

@ansar: I think the Acrobat SDK has to be purchased from Adobe.

# re: Printing PDF documents in C# 6/7/2011 1:06 PM Arun PS
Acrobat.AcroAVDoc doc = new AcroAVDoc();
doc.Open(filepath, string.Empty);
AcroPDDoc di = (AcroPDDoc)doc.GetPDDoc();
int pageCount = di.GetNumPages();
doc.PrintPagesSilent(0, pageCount - 1, 3, 1, 1);
doc.Close(1);
I did the following code but I got an error when I run the program.

Error was:Retrieving the COM class factory for component with CLSID {72498821-3203-101B-B02E-04021C009402} failed due to the following error: 80040154.

Please help me

Thanks in advance.


# re: Printing PDF documents in C# 9/8/2011 1:03 PM Dov
Can you please send me the ForensicTools.NET PDF /Image Printer DLL?
dov.miller@tnet.co.il
Thank you!

# re: Printing PDF documents in C# 10/5/2011 7:20 PM ava
Hi

Please can someone send me the ForensicTools.net pdf printer dll?
aveshn1@hotmail.com

Thanks

#  Printing PDF documents in C# 12/14/2011 7:23 PM kishore
Hi i am kishore. i Have Excel .In that Excel have so many tabs in that one tab pdf. i want to import that excel to .Net and print the Excel Pdf.
Give me the solution of Print Excel Pdf Code in .Net.

# re: Printing PDF documents in C# 2/15/2012 9:23 PM Carole
Hi

Please can someone send me the ForensicTools.net pdf image printer dll?
carolem@riversoft.net

Thanks!


# re: Printing PDF documents in C# 2/28/2012 10:42 PM michal
i dont know how can i convert aspx to pdf(in c#)-i have some codes but it missimg a using is anyone can help me to my email mbl4842@gmail.com

SautinSoft.PdfVision v = new SautinSoft.PdfVision();
byte[] pdf = v.ConvertHtmlFileToPDFStream(@"http://sautinsoft.net/pdf-to-word-images-convert.aspx");

//show PDF
if (pdf != null)
{
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/PDF";
Response.BinaryWrite(pdf);
Response.Flush();
Response.End();
}


# re: Printing PDF documents in C# 2/28/2012 11:46 PM michal
i dont know how can i convert aspx to pdf(in c#)-i have some codes but it missimg a using is anyone can help me to my email mbl4842@gmail.com

SautinSoft.PdfVision v = new SautinSoft.PdfVision();
byte[] pdf = v.ConvertHtmlFileToPDFStream(@"http://sautinsoft.net/pdf-to-word-images-convert.aspx");

//show PDF
if (pdf != null)
{
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/PDF";
Response.BinaryWrite(pdf);
Response.Flush();
Response.End();
}


Comments have been closed on this topic.