简体   繁体   English

如何将存储在 MemoryStream 或 PdfDocument 或 PdfPage 对象中的 PDF 文件打印到打印机?

[英]How to print a PDF file stored in MemoryStream or PdfDocument or PdfPage object to a printer?

I am using Essential Objects to convert an HTML file to PDF, it is working perfectly and I can save the PDF to Hard-drive by using the following method.我正在使用Essential Objects将 HTML 文件转换为 PDF,它运行良好,我可以使用以下方法将 PDF 保存到硬盘驱动器。

HtmlToPdf.ConvertUrl(htmlFileName, outputFileName);

In addition to convert it to a file, it can also convert it to any .NET Stream object.除了将其转换为文件之外,它还可以将其转换为任何 .NET Stream 对象。 For example, you can convert it to a MemoryStream object, then retrieve the raw bytes of the PDF file from the MemoryStream and attach it to an email or any other purpose, all without creating any physical file at all by using the following method.例如,您可以将其转换为MemoryStream对象,然后从 MemoryStream 检索 PDF 文件的原始字节并将其附加到电子邮件或任何其他目的,使用以下方法完全不创建任何物理文件。

HtmlToPdf.ConvertHtml(htmlCode, memoryStream or pdfDocument or pdfPage);

Now my problem is that how can I print the PDF file stored in MemoryStream or PdfDocument or PdfPage to a printer?现在我的问题是如何将存储在MemoryStreamPdfDocumentPdfPage的 PDF 文件打印到打印机?

Have a look at PrintDocument , its the way to go when printing in c#.看看PrintDocument ,它是在 c# 中打印时要走的路。 To quote from the msdn site从 msdn 站点引用

Typically, you create an instance of the PrintDocument class, set properties such as the DocumentNameand PrinterSettings, and call the Print method to start the printing process.通常,您创建 PrintDocument 类的一个实例,设置 DocumentName 和 PrinterSettings 等属性,然后调用 Print 方法来启动打印过程。 Handle the PrintPage event where you specify the output to print, by using the GraphicsGraphics property of the PrintPageEventArgs.通过使用 PrintPageEventArgs 的 GraphicsGraphics 属性,处理您指定要打印的输出的 PrintPage 事件。

Maybe this method wil be helpfull.也许这种方法会有所帮助。

    public void Print(string printerName, string fileName)
    {
        if (String.IsNullOrEmpty(fileName))
            return;

        var url = fileName;
        var filePath = String.Format(@"{0}\{1}.pdf", Application.StartupPath, Guid.NewGuid().ToString());

        using (var client = new WebClient())
        {
            client.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            client.DownloadFile(url, filePath);
        }

        if (String.IsNullOrEmpty(Form1.SelectedPrinter))
            return;

        PrintDocument pdoc = new PrintDocument();

        pdoc.DefaultPageSettings.PrinterSettings.PrinterName = printerName;
        pdoc.DefaultPageSettings.Landscape = true;
        pdoc.DefaultPageSettings.PaperSize.Height = 140;
        pdoc.DefaultPageSettings.PaperSize.Width = 104;


        try
        {
            ProcessStartInfo gsProcessInfo;
            Process gsProcess;

            gsProcessInfo = new ProcessStartInfo();
            gsProcessInfo.Verb = "PrintTo";
            gsProcessInfo.CreateNoWindow = true; //The default is false.
            gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
            gsProcessInfo.FileName = filePath;
            gsProcessInfo.Arguments = "\"" + printerName + "\"";
            gsProcess = Process.Start(gsProcessInfo);
            gsProcess.WaitForExit(4000);
            if (gsProcess.HasExited == false)
            {
                gsProcess.Kill();
            }
            gsProcess.EnableRaisingEvents = true;

            gsProcess.Close();
        }
        catch (Exception)
        {
        }
    }

You can print a PDF document to a printer using the PdfPrinter class from HiQPdf library :您可以使用HiQPdf 库中的 PdfPrinter 类将 PDF 文档打印到打印机:

PdfPrinter pdfPrinter = new HiQPdf.PdfPrinter();
pdfPrinter.PrintPdf(pdfStream);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何打印PDFsharp PdfDocument对象? - How to print PDFsharp PdfDocument object? 如何使用Windows 10附带的“Microsoft Print To PDF”打印机以编程方式在WPF中打印到PDF文件? - How to programmatically print to PDF file in WPF using the “Microsoft Print To PDF” printer that comes with Windows 10? 打印到.pdf打印机会创建损坏的文件 - Print to .pdf printer creates corrupted file 在Winforms中使用自定义打印机打印PDF文件 - Print PDF File using Custom Printer in Winforms 从MemoryStream打印pdf文档 - Print pdf document from MemoryStream 如何在特定打印机中打印文件? - How to print file in specific Printer? 如何将文件打印到特定打印机 - how to print file to a specific Printer 如何使用 Windows 10 附带的 Microsoft Print To PDF 打印机以编程方式打印到 PDF 文件而不提示在 C# 中输入文件名 - How to programmatically print to PDF file without prompting for filename in C# using the Microsoft Print To PDF printer that comes with Windows 10 VB.net-使用打印机打印具有纸张大小的pdf文件 - VB.net - print pdf file with printer with paper size 以编程方式提供文件路径作为“Microsoft Print to PDF”打印机的输入文件 - Programmatically provide a filepath as input file for “Microsoft Print to PDF” printer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM