C# 如何将存储在MemoryStream、PdfDocument或PdfPage对象中的PDF文件打印到打印机?
我正在使用将HTML文件转换为PDF,它工作正常,我可以使用以下方法将PDF保存到硬盘C# 如何将存储在MemoryStream、PdfDocument或PdfPage对象中的PDF文件打印到打印机?,c#,pdf,printing,C#,Pdf,Printing,我正在使用将HTML文件转换为PDF,它工作正常,我可以使用以下方法将PDF保存到硬盘 HtmlToPdf.ConvertUrl(htmlFileName, outputFileName); HtmlToPdf.ConvertHtml(htmlCode, memoryStream or pdfDocument or pdfPage); 除了将其转换为文件外,它还可以将其转换为任何.NET流对象。例如,您可以将其转换为MemoryStream对象,然后从MemoryStream中检索PDF文件
HtmlToPdf.ConvertUrl(htmlFileName, outputFileName);
HtmlToPdf.ConvertHtml(htmlCode, memoryStream or pdfDocument or pdfPage);
除了将其转换为文件外,它还可以将其转换为任何.NET流对象。例如,您可以将其转换为MemoryStream
对象,然后从MemoryStream中检索PDF文件的原始字节,并将其附加到电子邮件或任何其他用途,而无需使用以下方法创建任何物理文件
HtmlToPdf.ConvertUrl(htmlFileName, outputFileName);
HtmlToPdf.ConvertHtml(htmlCode, memoryStream or pdfDocument or pdfPage);
现在我的问题是,如何将存储在MemoryStream
或PdfDocument
或PdfPage
中的PDF文件打印到打印机上?看看,这是用c#打印时的方法。从msdn站点引用
通常,创建PrintDocument类的实例,设置DocumentName和PrinterSettings等属性,并调用Print方法来启动打印过程。使用PrintPageEventArgs的GraphicsGraphics属性处理指定要打印的输出的PrintPage事件
看一看,这是用c#打印的方法。从msdn站点引用
通常,创建PrintDocument类的实例,设置DocumentName和PrinterSettings等属性,并调用Print方法来启动打印过程。使用PrintPageEventArgs的GraphicsGraphics属性处理指定要打印的输出的PrintPage事件
也许这个方法会有帮助
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)
{
}
}
也许这个方法会有帮助
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)
{
}
}
您可以从以下位置使用PdfPrinter类将PDF文档打印到打印机:
您可以从以下位置使用PdfPrinter类将PDF文档打印到打印机:
我已经在内存中创建了一个pdf文件,因此无法使用PrintDocument提供的图形对象打印它!图形对象仅用于绘制图形和字符串,而不是pdf文件。我已经在内存中创建了一个pdf文件,因此无法使用PrintDocument提供的图形对象打印它!图形对象仅用于绘制图形和字符串,而不是pdf文件。