C# 从网站批量打印存储在服务器上的文件
我有一个asp.net网站,它托管在AWS上 从这个网站上,我需要能够选择多个PDF文件从一个存储在AWS框列表,然后打印它们 目前,我的过程只允许一次打印一个PDF,方法是将它们重定向到浏览器中的PDF文件路径,然后用户从那里手动打印,但如果他们有许多PDF文件要打印,则此过程将变得非常繁琐C# 从网站批量打印存储在服务器上的文件,c#,asp.net,C#,Asp.net,我有一个asp.net网站,它托管在AWS上 从这个网站上,我需要能够选择多个PDF文件从一个存储在AWS框列表,然后打印它们 目前,我的过程只允许一次打印一个PDF,方法是将它们重定向到浏览器中的PDF文件路径,然后用户从那里手动打印,但如果他们有许多PDF文件要打印,则此过程将变得非常繁琐 有人对我如何实现这一点有什么想法吗?在我看来,一个非常简单的方法就是将文件复制到打印机路径。 这适用于word文档和pdf文档。 然而,这假设了两件事: 1.)文件存储在您可以复制的地方 2.)您的Web
有人对我如何实现这一点有什么想法吗?在我看来,一个非常简单的方法就是将文件复制到打印机路径。
这适用于word文档和pdf文档。
然而,这假设了两件事: 1.)文件存储在您可以复制的地方 2.)您的Web服务器上安装了打印机驱动程序 3.)必须在web服务器上安装Adobe reader。(否则打印机有时无法识别PDF。) 因此,我要做的是通过ManagementObjectSearcher查询服务器上的打印机,找到默认打印机或您想要的任何打印机,获取打印机路径,然后将文件复制到该路径。 就是这样。 代码非常简单
public static class PrinterHelper
{
public class PrinterSettings
{
public string Name { get; set; }
public string ServerName { get; set; }
public string DeviceId { get; set; }
public string ShareName { get; set; }
public string Comment { get; set; }
public bool Default { get; set; }
}
/// <summary>
/// Sends the file to printer.
/// </summary>
/// <param name="filePathAndName">Name of the file path and Name of File.</param>
/// <param name="printerName">Name of the printer with Path. E.I. \\SFDPRINT2.raven.ravenind.net\P14401</param>
public static void SendFileToPrinter(string filePathAndName, string printerName)
{
FileInfo file = new FileInfo(filePathAndName);
file.CopyTo(printerName);
}
/// <summary>
/// Gets all printers that have drivers installed on the calling machine.
/// </summary>
/// <returns></returns>
public static List<PrinterSettings> GetAllPrinters()
{
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Printer");
ManagementObjectSearcher mos = new ManagementObjectSearcher(query);
List<PrinterSettings> printers = new List<PrinterSettings>();
foreach (ManagementObject mo in mos.Get())
{
PrinterSettings printer = new PrinterSettings();
foreach (PropertyData property in mo.Properties)
{
if (property.Name == "Name")
printer.Name = property.Value == null ? "" : property.Value.ToString();
if (property.Name == "ServerName")
printer.ServerName = property.Value == null ? "" : property.Value.ToString();
if (property.Name == "DeviceId")
printer.DeviceId = property.Value == null ? "" : property.Value.ToString();
if (property.Name == "ShareName")
printer.ShareName = property.Value == null ? "" : property.Value.ToString();
if (property.Name == "Comment")
printer.Comment = property.Value == null ? "" : property.Value.ToString();
if (property.Name == "Default")
printer.Default = (bool)property.Value;
}
printers.Add(printer);
}
return printers;
}
}
您犯了一个小错误,如何使用助手的参数颠倒了。首先传递文件名,然后传递打印机名。我没有发现这是AWS。我不知道你一定要用它,但我会张贴我的代码很快,看看它是否有帮助。
var printer = PrinterHelper.GetAllPrinters().FirstOrDefault(p => p.Default);
PrinterHelper.SendFileToPrinter(printer.Name, "C:\\Users\\Public\\Documents\\Form - Career Advancement Request.pdf");