4

我有一个托管在 AWS 上的 asp.net 网站。

在这个网站上,我需要能够从存储在 AWS 框中的列表中选择多个 PDF 文件,然后打印它们。

目前,我的过程只允许一次打印一个 PDF,方法是将它们重定向到浏览器中的 PDF 文件路径,然后用户从那里手动打印,但如果他们要打印很多,这个过程将变得乏味。

有人对我如何实现这一目标有任何想法吗?

4

2 回答 2

0

在我看来,一个非常快速简单的方法就是将文件复制到打印机路径。
这适用于 word 文档和 pdf。
然而,这假设了几件事:

1.) 文档存储在您可以从中复制它们的某个位置。

2.) 您在 Web 服务器上安装了打印机驱动程序。

3.) 必须在网络服务器上安装 Adob​​e 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");
于 2012-08-23T17:31:14.287 回答
0

你犯了一个小错误,如何使用助手的参数颠倒了。首先传递文件名,然后传递打印机名称。

于 2012-11-22T07:46:35.287 回答