1、引用System.Drawing.Printing.dll:
代码如下:
/// <summary>
/// 开始打印
/// </summary>
public void PrintReport()
{
private PrintDocument print = new PrintDocument();
if (PrinterSettings.InstalledPrinters == null)
{
//没有打印机
}
print.BeginPrint += Print_BeginPrint;
print.EndPrint += Print_EndPrint;
print.PrinterSettings.PrinterName = "Microsoft Print to PDF";//打印机名称
print.PrintPage += new PrintPageEventHandler(this.PageSetting);
print.Print();
}
/// <summary>
/// 打印设置
/// </summary>
private void PageSetting(object sender, PrintPageEventArgs e)
{
Image image = Image.FromFile(path);
if(IsScale) //缩放打印图片
{
int paperWidth = (int)(e.PageSettings.PaperSize.Height * 1080 / 1920);
int paperBlank = (e.PageSettings.PaperSize.Width - paperWidth) / 2;
e.Graphics.DrawImage(image, paperBlank, 0, paperWidth, 1169);
}
else
{
e.Graphics.DrawImage(image, 0, 0, 827, 1169);
}
}
private void Print_BeginPrint(object sender, PrintEventArgs e)
{
//开始
}
private void Print_EndPrint(object sender, PrintEventArgs e)
{
//完成
}
存在问题:改方法只能在编辑器模式下使用,build之后失效,原因是.net下的打印功能都存放在System.Drawing中,但是这个区域在unity中不可用。
2、GitHub上的一种方法,非静默打印,使用的是调出打印面板的方法,为了防止侵权,只写下效果和链接:
效果:
github链接:https://github.com/ElviraZ/unity–pc–print