Blog Stats
  • Posts - 132
  • Articles - 0
  • Comments - 28
  • Trackbacks - 0

 

Simple PDF Printing

I’ve been playing around today, wanting to print PDF’s automatically triggering them from my mobile device.

I was left with the final hurdle of needing to programmatically print a PDF unattended running on a server.

I tried all sorts, of COM interoperability to Acrobat,  which just seemed too difficult.   I wanted this thing to be simple.

We’ll here’s what I came up with.   The good old fashioned command line print, using Acrobat…

This bit of C# will print a PDF (then delete it).    It does the trick nicely.

public class PrintIt
    {
        private string quoteme(string instr)
        {
            string retstr=instr;
            if (!retstr.StartsWith(@""""))
            {
                retstr = "\"" + retstr + "\"";
            }
            return retstr;
        }
        public bool Print(string filename)
        {
            if (!File.Exists(filename)) return false;
 
            string acropath = ConfigurationSettings.AppSettings["acropath"];
            if (!File.Exists(acropath)) return false;
            
            string printer = ConfigurationSettings.AppSettings["printer"];
 
            
            Process proc = new Process();
            proc.StartInfo.FileName = quoteme(acropath);
            proc.StartInfo.Arguments = @"/t "+quoteme(filename)+" " + quoteme(printer);
            proc.StartInfo.CreateNoWindow = true;
            proc.Start();
            Thread.Sleep(10000);
            try
            {
                proc.Kill();
            }
            catch
            {
 
            }
            File.Delete(filename);
            return true;
 
        }

 

I use a couple of values in a config file as follows -

 

<add key="printer" value="Laser"/>  <----------- name of printer
<add key="acropath" value="C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"/>    <---------- Acrobat Reader path

 

I don’t like having to use the Thread.Sleep statement in the above, but for a multi-threaded application this isn’t too much of a big deal.

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Feedback

No comments posted yet.


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

 

 

Copyright © Richard Jones