locked
How To Print Files (To Specific Printers) RRS feed

  • Question

  • How can i print files to specific printers?  They are pdf documents but that shouldnt make a difference.   They are network printers and I have the path to them.
    Saturday, November 8, 2008 1:40 AM

Answers

  • Here's some snippets from my notebook, most of which I haven't tried. Maybe can help. 




    You may be able to print a PDF by these steps (uses Adobe 7 object model).

    (1) Tools > Choose ToolBox items > COM > AdobePDf

    (2) In the Toolbox, in the General tab, look for the Adobe icon.

    (3) Drop it on the form.

    And then use code like this - well, this first sample embeds the PDf into the Form:

    axAcroPDF1.LoadFile(@"C:\Crystal.pdf");

    axAcroPDF1.Show(); //actually this is probabl optional

    Now here's what I found using the Print command

    axAcroPDF1.LoadFile(@"C:\Crystal.pdf");

    axAcroPDF1.PrintAll()

    The first time you do this,it opens up an Adobe window with a question "A Script has requested printing. Do you wish to continue printing." The user has to manually do two things:

    (1) Checkmark "Do Not show this message again."

    (2) Click Yes.

    From then on, printing will be automatic.









    You can print a PDF like this (in fat this one prints a collection/arraylist of file).

    While intArrayCount > intCounter

    Dim command As Process = New Process

    command.StartInfo.Verb = "print"

    command.StartInfo.FileName = arrFileToPrint.Item(intCounter).ToString

    command.StartInfo.CreateNoWindow = True

    command.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

    command.Start()

    command.StartInfo.po()

    command.CloseMainWindow()

    command.Close()

    command.Dispose()

    command = Nothing

    intCounter += 1

    End While

    Or like this:

    Dim pathToExecutable As String = "AcroRd32.exe"

    Dim sReport = "C:Test.PDF" ‘Complete name/path of PDF file

    Dim SPrinter = "HP Officejet 5600 seriese" ‘Name Of printer

    Dim starter As New ProcessStartInfo(pathToExecutable, "/t " + sReport + " " + sPrinter + "")

    Dim Process As New Process()

    Process.StartInfo = starter Process.Start()

    ‘try and close the process with 20 seconds delay

    System.Threading.Thread.Sleep(30000)

    Process.CloseMainWindow()

    Dim iLoop As Int16 = 0

    ‘check the process has exited or not

    If Process.HasExited = False Then

    ‘if not then loop for 100 time to try and close the process‘with 10 seconds delay

    While Not Process.HasExited

    System.Threading.Thread.Sleep(10000)

    Process.CloseMainWindow()

    iLoop = CShort(iLoop + 1)

    If iLoop >= 100 Then

    Exit While

    End If

    End While

    End If

    Process.Close()

    Process.Dispose()

    Process = Nothing

    starter = Nothing

    it.







    You may also be able to use ITextSharp which i think is free from SourceForge.

    http://itextsharp.sourceforge.net/tutorial/

    specifically pdf

    http://itextsharp.sourceforge.net/tutorial/ch01.html









    Free library: An excellent PDF library is the TallComponents PDFKit.net. The assemblies are made for .net framework 1.1 and up and have extreme flexibility as far as PDF files goes. The component is paid, however the free version works just fine. The only thing that's a bit annoying with it is that it adds "PDFKit.NET" at the top of the PDF file if you don't pay for it.

    PDF Creator is free and has an autoSave feature such that, when set, it won't prompt you for the name of the file on each print--to-pdf page.





    • Marked as answer by Harry Zhu Friday, November 14, 2008 11:59 AM
    Friday, November 14, 2008 5:34 AM

All replies

  • you could either setup a menu to select the printers or use any other method to determine what the printer name should be. changing the printer that you are printing to is very easy

       System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();  
       pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage);  
       pd.PrinterSettings.PrinterName = "hp laserjet 1012";  
       pd.Print(); 

    .net 3.0 also has the System.Printing class which has many advanced functions for using and controlling printers
    Saturday, November 8, 2008 1:00 PM
  • thanks for the help


    is there a way to return the list of installed printers on the users machine?
    Sunday, November 9, 2008 6:03 AM
  • using .net 3.0 or higher, you can do it like this.

                System.Printing.PrintServer ps = new System.Printing.PrintServer();  
                System.Printing.PrintQueueCollection pqc = ps.GetPrintQueues();  
                foreach (System.Printing.PrintQueue pq in pqc)  
                {  
                    MessageBox.Show(pq.Name);  
                } 
    Sunday, November 9, 2008 2:59 PM
  • thanks for the help.  

    can you tell me what pd_PrintPage means in the first example?
     pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage); 

    i thought it might be just pd (the print document) but it wouldnt compile.  Is it missing a line?
    Monday, November 10, 2008 3:59 PM
  • that is where you add the event handler for the pd.Print(). you need to create the a method to draw the graphics. if you are using VS, that method stub can be generated automatically. to get this to happen, just type "pd.PrintPage +=" without the quotes, then if you hit the tab key twice, you will see a new method stub below your print method.
    Monday, November 10, 2008 5:28 PM
  • I got the code to compile and print a "test" string but how do I get it to print a pdf file?
    Monday, November 10, 2008 7:19 PM
  • Can anyone help with printing a pdf document that I know the path to?  I need different pdfs to go to different printers.
    Tuesday, November 11, 2008 7:54 PM


  • Someone has to know how to print a PDF document to a specific printer.

    Google isnt returning anything helpful
    Thursday, November 13, 2008 3:00 PM
  • Try the Adobe forums.
    Thursday, November 13, 2008 4:41 PM
  • Here's some snippets from my notebook, most of which I haven't tried. Maybe can help. 




    You may be able to print a PDF by these steps (uses Adobe 7 object model).

    (1) Tools > Choose ToolBox items > COM > AdobePDf

    (2) In the Toolbox, in the General tab, look for the Adobe icon.

    (3) Drop it on the form.

    And then use code like this - well, this first sample embeds the PDf into the Form:

    axAcroPDF1.LoadFile(@"C:\Crystal.pdf");

    axAcroPDF1.Show(); //actually this is probabl optional

    Now here's what I found using the Print command

    axAcroPDF1.LoadFile(@"C:\Crystal.pdf");

    axAcroPDF1.PrintAll()

    The first time you do this,it opens up an Adobe window with a question "A Script has requested printing. Do you wish to continue printing." The user has to manually do two things:

    (1) Checkmark "Do Not show this message again."

    (2) Click Yes.

    From then on, printing will be automatic.









    You can print a PDF like this (in fat this one prints a collection/arraylist of file).

    While intArrayCount > intCounter

    Dim command As Process = New Process

    command.StartInfo.Verb = "print"

    command.StartInfo.FileName = arrFileToPrint.Item(intCounter).ToString

    command.StartInfo.CreateNoWindow = True

    command.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

    command.Start()

    command.StartInfo.po()

    command.CloseMainWindow()

    command.Close()

    command.Dispose()

    command = Nothing

    intCounter += 1

    End While

    Or like this:

    Dim pathToExecutable As String = "AcroRd32.exe"

    Dim sReport = "C:Test.PDF" ‘Complete name/path of PDF file

    Dim SPrinter = "HP Officejet 5600 seriese" ‘Name Of printer

    Dim starter As New ProcessStartInfo(pathToExecutable, "/t " + sReport + " " + sPrinter + "")

    Dim Process As New Process()

    Process.StartInfo = starter Process.Start()

    ‘try and close the process with 20 seconds delay

    System.Threading.Thread.Sleep(30000)

    Process.CloseMainWindow()

    Dim iLoop As Int16 = 0

    ‘check the process has exited or not

    If Process.HasExited = False Then

    ‘if not then loop for 100 time to try and close the process‘with 10 seconds delay

    While Not Process.HasExited

    System.Threading.Thread.Sleep(10000)

    Process.CloseMainWindow()

    iLoop = CShort(iLoop + 1)

    If iLoop >= 100 Then

    Exit While

    End If

    End While

    End If

    Process.Close()

    Process.Dispose()

    Process = Nothing

    starter = Nothing

    it.







    You may also be able to use ITextSharp which i think is free from SourceForge.

    http://itextsharp.sourceforge.net/tutorial/

    specifically pdf

    http://itextsharp.sourceforge.net/tutorial/ch01.html









    Free library: An excellent PDF library is the TallComponents PDFKit.net. The assemblies are made for .net framework 1.1 and up and have extreme flexibility as far as PDF files goes. The component is paid, however the free version works just fine. The only thing that's a bit annoying with it is that it adds "PDFKit.NET" at the top of the PDF file if you don't pay for it.

    PDF Creator is free and has an autoSave feature such that, when set, it won't prompt you for the name of the file on each print--to-pdf page.





    • Marked as answer by Harry Zhu Friday, November 14, 2008 11:59 AM
    Friday, November 14, 2008 5:34 AM