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

前端 未结 6 837
长情又很酷
长情又很酷 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: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);
        }
    }
    

提交回复
热议问题