从我正在构建的应用程序中,我需要打印现有的PDF(由另一个应用程序创建)。 如何在C#中执行此操作并提供一种机制,以便用户可以选择其他打印机或其他属性。
我查看了PrintDialog,但不确定它尝试打印的文件是什么,如果有的话,b / c输出始终是空白页面。也许我只是错过了那里的东西。
我是否需要使用“iTextSharp”(如其他地方所建议的那样)?这对我来说似乎很奇怪,因为我可以“将文件发送到打印机”我手边没有任何好的对话框来设置打印机等。我真的不想从头开始编写打印对话框但似乎我通过搜索找到的很多例子就是这样。
任何建议,示例或示例代码都会很棒!
此外,如果PDF是问题,那么其他应用程序可以使用差异格式(如位图或png)创建文件,如果这样可以使事情变得更容易。
答案 0 :(得分:23)
显示一个带有组合框的小对话框,其组合项设置为PrinterSettings.InstalledPrinters
返回的字符串集合。
如果您可以要求在机器上安装GSView,则可以静默打印PDF。这有点慢和迂回,但至少你不必弹出Acrobat。
以下是我用来打印一些从UPS网络服务中获取的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窗体提供的普通打印对话框。
祝你好运!答案 1 :(得分:2)
虽然这是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
答案 2 :(得分:1)
您需要Acrobat或其他可以打印PDF的应用程序。从那里你P / Invoke到ShellExecute来打印文档。
答案 3 :(得分:1)
您还可以使用PDFsharp - 它是一个用于创建和操作PDF的开源库。 http://www.pdfsharp.net/
答案 4 :(得分:1)
我正在为我的项目做同样的事情,它对我有用
看看它是否可以帮助你......
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 */
}
您还可以使用不同的Windows选项
PRINT命令获得所需的输出... Reference link
答案 5 :(得分:1)
经过大量研究和谷歌搜索这项任务,微软已经发布了一个很棒的KB来打印PDF文件,而不需要任何其他应用程序。无需调用adobe或ghostprint。它可以在不将文件保存到磁盘的情况下进行打印,使生活变得非常轻松。