用C#打印现有的PDF(或其他文件)
本文关键字:其他 文件 PDF 打印 | 更新日期: 2023-09-27 17:48:50
从我正在构建的应用程序中,我需要打印现有的PDF(由另一个应用程序创建)。我如何在C#中做到这一点,并提供一种机制,让用户可以选择不同的打印机或其他属性。
我看过PrintDialog,但不确定它试图打印什么文件(如果有的话),b/c输出总是一个空白页。也许我只是错过了什么。
我是否需要使用"iTextSharp"(如其他地方建议的那样)?这对我来说似乎很奇怪,因为我可以"将文件发送到打印机"。我只是在手动设置打印机等之前没有任何好的对话框,我也不想从头开始写打印对话框,但我通过搜索找到的很多例子似乎都是这样做的。
任何建议、示例或示例代码都将非常棒!
此外,如果PDF是问题所在,那么其他应用程序可以以位图或png等不同格式创建文件,如果这样做更容易的话。
显示一个带有组合框的小对话框,其中Items设置为PrinterSettings.InstalledPrinters
返回的字符串集合。
如果您可以要求在机器上安装GSView,那么您可以静默打印PDF。它有点缓慢和迂回,但至少你不必弹出Acrobat。
以下是我用来打印从UPS Web服务返回的一些PDF的一些代码:
private void PrintFormPdfData(byte[] formPdfData)
{
string tempFile;
tempFile = Path.GetTempFileName();
using (FileStream fs = new FileStream(tempFile, FileMode.Create))
{
fs.Write(formPdfData, 0, formPdfData.Length);
fs.Flush();
}
try
{
string gsArguments;
string gsLocation;
ProcessStartInfo gsProcessInfo;
Process gsProcess;
gsArguments = string.Format("-grey -noquery -printer '"HP LaserJet 5M'" '"{0}'"", tempFile);
gsLocation = @"C:'Program Files'Ghostgum'gsview'gsprint.exe";
gsProcessInfo = new ProcessStartInfo();
gsProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
gsProcessInfo.FileName = gsLocation;
gsProcessInfo.Arguments = gsArguments;
gsProcess = Process.Start(gsProcessInfo);
gsProcess.WaitForExit();
}
finally
{
File.Delete(tempFile);
}
}
正如您所看到的,它将PDF数据作为一个字节数组,将其写入一个临时文件,然后启动gsprint.exe将文件静默打印到指定的打印机("HP Laserjet 5M")。您可以将打印机名称替换为用户在对话框中选择的任何名称。
打印PNG或GIF会容易得多——只需扩展PrintDocument类并使用Windows窗体提供的普通打印对话框即可。
祝你好运!
虽然这是VB,但你可以很容易地翻译它。顺便说一句,Adobe不会弹出,它只打印pdf,然后就消失了。
''' <summary>
''' Start Adobe Process to print document
''' </summary>
''' <param name="p"></param>
''' <remarks></remarks>
Private Function printDoc(ByVal p As PrintObj) As PrintObj
Dim myProcess As New Process()
Dim myProcessStartInfo As New ProcessStartInfo(adobePath)
Dim errMsg As String = String.Empty
Dim outFile As String = String.Empty
myProcessStartInfo.UseShellExecute = False
myProcessStartInfo.RedirectStandardOutput = True
myProcessStartInfo.RedirectStandardError = True
Try
If canIprintFile(p.sourceFolder & p.sourceFileName) Then
isAdobeRunning(p)'Make sure Adobe is not running; wait till it's done
Try
myProcessStartInfo.Arguments = " /t " & """" & p.sourceFolder & p.sourceFileName & """" & " " & """" & p.destination & """"
myProcess.StartInfo = myProcessStartInfo
myProcess.Start()
myProcess.CloseMainWindow()
isAdobeRunning(p)
myProcess.Dispose()
Catch ex As Exception
End Try
p.result = "OK"
Else
p.result = "The file that the Document Printer is tryng to print is missing."
sendMailNotification("The file that the Document Printer is tryng to print" & vbCrLf & _
"is missing. The file in question is: " & vbCrLf & _
p.sourceFolder & p.sourceFileName, p)
End If
Catch ex As Exception
p.result = ex.Message
sendMailNotification(ex.Message, p)
Finally
myProcess.Dispose()
End Try
Return p
End Function
您需要Acrobat或其他可以打印PDF的应用程序。从那里您可以P/Invoke到ShellExecute来打印文档。
您也可以使用PDFsharp-这是一个用于创建和操作PDF的开源库。http://www.pdfsharp.net/
我正在为我的项目做同样的事情,它对我起到了作用
看看它是否能帮助你。。。
Process p = new Process();
p.EnableRaisingEvents = true; //Important line of code
p.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
Verb = "print",
FileName = file,
Arguments = "/d:"+printDialog1.PrinterSettings.PrinterName
};
try
{
p.Start();
}
catch
{
/* your fallback code */
}
你也可以玩不同的窗口选项
PRINT命令以获得所需输出。。。参考链接
经过对这项任务的大量研究和谷歌搜索,微软发布了一个很棒的KB,可以在不需要任何其他应用程序的情况下打印pdf。无需调用adobe或ghostprint。它可以在不将文件保存到磁盘的情况下打印,这让生活变得非常轻松。
http://support2.microsoft.com/?kbid=322091