使用 PDFSharp 打印 PDF
我有以下代码:
using System;
using System.Diagnostics;
using System.IO;
using PdfSharp.Pdf.Printing;
namespace PrintPdfFile
{
class Program
{
[STAThread]
static void Main(string[] args)
{
// Set Acrobat Reader EXE, e.g.:
PdfFilePrinter.AdobeReaderPath = @"C:\\Documents and Settings\\mike.smith\\Desktop\\Adobe Reader 9.0.exe";
// -or-
//PdfPrinter.AdobeReaderPath = @"C:\Program Files\Adobe\[...]\AcroRd32.exe";
//// Ony my computer (running a German version of Windows XP) it is here:
//PdfFilePrinter.AdobeReaderPath = @"C:\\Documents and Settings\\mike.smith\\Desktop\\Adobe Reader 9.0.exe";
// Set the file to print and the Windows name of the printer.
// At my home office I have an old Laserjet 6L under my desk.
PdfFilePrinter printer = new PdfFilePrinter(@"C:\Documents and Settings\mike.smith\Desktop\Stuff\ReleaseNotesAndFolderList.pdf", " \\ny-dc-03\\IT-01");
try
{
printer.Print();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
在我的一生中,我无法让它工作并打印出单个 PDF。 每当我去打印时,我都会收到错误“找不到指定的文件”。 有人知道我的代码是否有问题吗? 我在这里使用 PDFSharp...
I have the following code:
using System;
using System.Diagnostics;
using System.IO;
using PdfSharp.Pdf.Printing;
namespace PrintPdfFile
{
class Program
{
[STAThread]
static void Main(string[] args)
{
// Set Acrobat Reader EXE, e.g.:
PdfFilePrinter.AdobeReaderPath = @"C:\\Documents and Settings\\mike.smith\\Desktop\\Adobe Reader 9.0.exe";
// -or-
//PdfPrinter.AdobeReaderPath = @"C:\Program Files\Adobe\[...]\AcroRd32.exe";
//// Ony my computer (running a German version of Windows XP) it is here:
//PdfFilePrinter.AdobeReaderPath = @"C:\\Documents and Settings\\mike.smith\\Desktop\\Adobe Reader 9.0.exe";
// Set the file to print and the Windows name of the printer.
// At my home office I have an old Laserjet 6L under my desk.
PdfFilePrinter printer = new PdfFilePrinter(@"C:\Documents and Settings\mike.smith\Desktop\Stuff\ReleaseNotesAndFolderList.pdf", " \\ny-dc-03\\IT-01");
try
{
printer.Print();
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
For the life of me i cannot get this to work and print out a single PDF. Anytime i go to print, i get the error "Cannot find the file specified". Does anybody have any idea if something is wrong with my code?? I'm using PDFSharp here...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在下面一行中观察到:
您正在使用“@”来转义字符串并转义反斜杠。 删除“@”或使用单个反斜杠。
还要确保这是您的 EXE 的正确路径。
更新:如果您确认 Acrobat Reader EXE 的路径正确,接下来要查看的是传递给 PdfFilePrinter 构造函数的“打印机名称”参数。
您正在传递
" \\ny-dc-03\\IT-01"
作为打印机名称。 这需要与 Windows 打印机列表中显示的打印机名称完全匹配,而不仅仅是任意 IP 打印机。如果这是正确的,请务必删除前导空格:
"\\ny-dc-03\\IT-01"
。One observation, in the following line:
You are using the "@" to escape the string and also escaping the backslashes. Either remove the "@" or use a single backslash.
Also make sure that is the correct path to your EXE.
UPDATE: If you have confirmed that you have the correct path to your Acrobat Reader EXE, the next thing to look at is the "Printer Name" parameter that you are passing to the PdfFilePrinter constructor.
You are passing
" \\ny-dc-03\\IT-01"
as the printer name. This needs to match the name of printer exactly as it appears in the list of Printers in Windows, not just an arbitrary IP printer.If this is correct, be sure to remove, the leading space:
"\\ny-dc-03\\IT-01"
.这可能是显而易见的,但却是杂技演员:
这只是您的用户名暗示您的名字不是 Mike smith。
This may be stating the obvious but is acrobat at:
It's just your user name implies that your name isn't Mike smith.
我认为这应该是
"\\\\ny-dc-03\\IT-01"
code> 或@"\\ny-dc-03\IT-01"
不确定
@"\\ny-dc-03\\IT-01"
是否会工作,但"\\ny-dc-03\\IT-01"
无法工作,因为 UNC 名称以双反斜杠开头。I think this should be
"\\\\ny-dc-03\\IT-01"
or@"\\ny-dc-03\IT-01"
Not sure if
@"\\ny-dc-03\\IT-01"
will work, but"\\ny-dc-03\\IT-01"
cannot work as UNC names start with a double backslash.