Tobias
Tobias

Reputation: 5108

.NET: How to print files w/o opening them

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

Upvotes: 7

Views: 19819

Answers (6)

D_Bester
D_Bester

Reputation: 5931

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

Upvotes: 0

Yoopergeek
Yoopergeek

Reputation: 5642

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.

Upvotes: 1

joshua.ewer
joshua.ewer

Reputation: 3962

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);
    }
}

Upvotes: 7

rp.
rp.

Reputation: 17683

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

Upvotes: 1

C-Pound Guru
C-Pound Guru

Reputation: 16368

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();

Upvotes: 7

jerryjvl
jerryjvl

Reputation: 20151

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.

Upvotes: 2

Related Questions