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

前端 未结 6 802
长情又很酷
长情又很酷 2021-01-05 07:53

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

相关标签:
6条回答
  • 2021-01-05 08:27

    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.

    0 讨论(0)
  • 2021-01-05 08:32

    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.

    0 讨论(0)
  • 2021-01-05 08:34

    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

    0 讨论(0)
  • 2021-01-05 08:38

    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();
    
    0 讨论(0)
  • 2021-01-05 08:48

    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
    
    0 讨论(0)
  • 2021-01-05 08:52

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题