[英]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?现在我的问题是如何将存储在
MemoryStream
或PdfDocument
或PdfPage
的 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.