我有一个托管在 AWS 上的 asp.net 网站。
从这个网站上,我需要能够从存储在 AWS 盒子上的列表中选择多个 PDF 文件,然后打印它们。
目前我的过程只允许一次打印一个 PDF,方法是将它们重定向到浏览器中的 PDF 文件路径,然后用户从那里手动打印,但如果他们有很多要打印,这个过程将变得乏味。
有人对我如何实现这一目标有任何想法吗?
最佳答案
在我看来,一个非常快速简单的方法就是将文件复制到打印机路径。
这适用于 word 文档和 pdf。
然而,这假设了一些事情:
1.) 文档存储在您可以从中复制的位置。
2.) 您已在 Web 服务器上安装了打印机驱动程序。
3.) 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;
}
}
这是如何使用助手。就是这样。
var printer = PrinterHelper.GetAllPrinters().FirstOrDefault(p => p.Default);
PrinterHelper.SendFileToPrinter(printer.Name, "C:\\Users\\Public\\Documents\\Form - Career Advancement Request.pdf");
关于c# - 从网站批量打印存储在服务器上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12096545/