Unity 使用打印机打印

2021-09-14  本文已影响0人  虫小白

1,原文地址
只能打印图片,需引用System.Drawing.dll

    /// <summary>
    /// 打印
    /// </summary>
    public void PrintFile()
    {
        PrintDocument pri = new PrintDocument();
        pri.PrintPage += Printpagetest;
        pri.Print();
    }

    private void Printpagetest(object sender, PrintPageEventArgs e)
    {
        try
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(printPath);
            System.Drawing.Graphics g = e.Graphics;
            g.TranslateTransform(_4AHeight, 0);
            g.RotateTransform(90);
            g.DrawImage(image, 0, 0, _4AWidth, _4AHeight);
        }
        catch (Exception ee)
        {
            Debug.LogError(ee.Message);
        }
    }

2,原文地址
可打印word、excel。

    public void PrintFile(string path)
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process(); //系统进程
        process.StartInfo.CreateNoWindow = true; //不显示调用程序窗口
        process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//
        process.StartInfo.UseShellExecute = true; //采用操作系统自动识别模式
        process.StartInfo.FileName = path; //要打印的文件路径
        process.StartInfo.Verb = "print"; //指定执行的动作,打印:print 打开:open …………
        process.Start(); //开始打印    
    }

补充:在公司内网测试时,无法打印,原因是没有默认关联程序,怀疑是word版本有问题,安装wps并设为默认程序后,可正常打印。

上一篇 下一篇

猜你喜欢

热点阅读