7

We have an application that basically archives files and we give the user the possibility to print these files. They can be .txt, .doc, .pdf, .jpg nothing fancy. Is there a .NET way to send these files to the printer without handling them further, ie opening them?

I already tried creating a process with the StartInfo.Verb = "print"

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = fileName;
p.StartInfo.Verb = "print"
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

p.Start();

It still opens the file which I don't want. Can someone help?

Any help would be appreciated. Tobi

Tobias
  • 4,999
  • 7
  • 34
  • 40
  • You probably don't want the word random in your title as it would imply the using functions associated with the Random class. I would rephrase it like ".NET: How to open various file types without opening them." or something like that. – Nathan Koop Jun 19 '09 at 14:51

6 Answers6

7

My understanding is that most apps will open (even briefly) when you print. Try right-clicking a MS Word document and hitting print. You'll see Word open, print, and close.

However, you might want to add this to your code to keep the process hidden and to close when finished:

p.Start();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
if (p.HasExited == false)
{
   p.WaitForExit(10000);
}

p.EnableRaisingEvents = true;
p.CloseMainWindow();
p.Close();
C-Pound Guru
  • 15,967
  • 6
  • 46
  • 67
7

It's actually very, very easy.

Use System.Drawing.Printing.PrintDocument.

Follow the example in that link, or just use the code here (which I excerpted from something doing print automation I'm using every day).

for example, to print off a .jpg (BTW, this won't open any editing application; it spools to the printer in the background)

public void SetupPrintHandler()
{
    PrintDocument printDoc = new PrintDocument();
    printDoc.PrintPage += new PrintPageEventHandler(OnPrintPage);

    printDoc.Print();
}

private void OnPrintPage(object sender, PrintPageEventArgs args)
{
    using (Image image = Image.FromFile(@"C:\file.jpg"))
    {
        Graphics g = args.Graphics;
        g.DrawImage(image, 0, 0);
    }
}
joshua.ewer
  • 3,944
  • 3
  • 25
  • 35
2

How do you suggest Windows manage to print a file without sending it to an application that knows how to handle it?

I don't think there is a way to do this, simply because Windows does not know what a pdf is (or a doc, or even a jpg).

I'm afraid you're stuck with either what you have, or including a library into your application for each format that you wish to print.

jerryjvl
  • 19,723
  • 7
  • 40
  • 55
  • Good point, thanks. Is there a way then to do this truely hidden? – Tobias Jun 19 '09 at 14:10
  • I think you will need some libraries to help you out and implement your own printing code for each format... txt and jpg won't be too bad with the .NET Framework itself, but at the least you are going to need libraries for PDF and doc(x?) – jerryjvl Jun 19 '09 at 14:11
1

Here is a class that prints a Word doc without opening Word and showing the document. While I usually code in C#, I long ago learned that to code any Office automation with anything but VB.NET is downright silly (some of the upcoming features in C# 4.0 may change this).

This is only for Word, but Excel docs would be done in a similar fashion. For text documents, you can use the System.Drawing.Printing stuff pretty easily.

Imports System.IO 
Imports System.Windows.Forms 
Imports System.Drawing

Namespace rp.OfficeHelpers

    Public Enum PrintStatus
        Success
        FileNotFound
        FailedToOpenDocument
        FailedToPrintDocument
    End Enum

    Public Class Word

        Public Shared Function PrintDocument( DocumentName As String,_ 
                               PrinterName As String ) As PrintStatus 
            Dim wordApp As Microsoft.Office.Interop.Word.Application = _ 
                           new Microsoft.Office.Interop.Word.Application()
            Dim wordDoc As Microsoft.Office.Interop.Word.Document
            Dim copies  As Object = 1
            Dim CurrentPrinter As String = wordApp.ActivePrinter

            If ( Not File.Exists( DocumentName ) )
                Return PrintStatus.FileNotFound    
            End If

            wordApp.Visible = false

            wordApp.ActivePrinter = PrinterName

            ' Document name must be provided as an object, not a string.
            Try 
                wordDoc = wordApp.Documents.Open( CType( DocumentName, Object ) )
            Catch WordError as System.Exception 
                Return PrintStatus.FailedToOpenDocument
            End Try  

            Try 
                wordDoc.PrintOut( Copies := copies, Background:= false )
            Catch WordError as System.Exception 
                Return PrintStatus.FailedToPrintDocument
            End Try  

            wordApp.ActivePrinter = CurrentPrinter

            wordApp.Quit( SaveChanges := false )

            Return PrintStatus.Success        
        End Function

    End Class

End Namespace
rp.
  • 17,483
  • 12
  • 63
  • 79
  • This may not _show_ the document, but it certainly opens it. It also won't work in a multithreaded environment, as the Office APIs are meant for desktop automation (let alone licensing issues). Don't know if that matters to the OP, but just to get it on the record. – John Saunders Jun 19 '09 at 18:21
  • That's a good distinction, John. My use of the word "open" was sloppy. – rp. Jun 19 '09 at 21:20
  • That's a good distinction but, in Excel file case, how I can do something same like that. – h0cc0i Jul 13 '16 at 02:09
1

I have to agree with other answers in that you can't print it without opening it.

The only way I think you may be able to get around this is if you had a straight-up postscript file, and a directly-attached postscript-compatible printer.

In that case you could just dump the .ps file to the LPT port and the printer would process it correctly.

Yoopergeek
  • 5,592
  • 3
  • 24
  • 29
0

Some printers support sending a file to the printer using command line LPR. We have a copier that does this. This truly doesn't open the file on the computer at all. It sends the file to the printer and the printer interprets it and prints it.

LPR -S <Server Name> -P <Printer Name> -o l "C:\Temp\Sample.PDF"

We do it with PostScript and PDF files with great success. Don't know if it works for other file types; check the printer documentation.

For recent versions of Windows, you need to enable the "LPR Port Monitor". Control Panel > Programs and Features > Turn Windows features on or off > Print and Document Services/LPR Port Monitor

TechNet LPR Command Line

D_Bester
  • 5,723
  • 5
  • 35
  • 77