通过几天的查找经测试后发现以下三种方法可以实现用C#直接打印PDF文件,但是三种方法都不是我想要的,我想要的是一种在C#中单击打印按钮就直接打印PDF文件而不跳出任何窗口,还在寻找中,如有朋友知道如何实现请留言,不过以下三种方法供大家参考。
方法一:通过调用命令行:
using System.Drawing.Printing;
using System.Diagnostics;
using System.Collections.Specialized;
//打印方法
private void pdfPrint(string filePath)
{
PrintDocument pd = new PrintDocument();
Process p = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = true;
startInfo.FileName = filePath;
startInfo.Verb = "print";
startInfo.Arguments = @"/p /h \" + filePath + "\"\"" + pd.PrinterSettings.PrinterName + "\"";
p.StartInfo = startInfo;
p.Start();
p.WaitForExit();
}
protected void btn_print_Click(object sender, EventArgs e)
{
string filePath="C:\\Documents and Settings\\AuYeungCK\\My Documents\\myfile\\1.pdf";
pdfPrint(filePath);
}
总结:以上方法单击打印后会跳出一个adobe窗口,但是不会显示任何内容,打印机会自动打印,经测试现在一个问题,在打印我公司的通告时打印出来的内容是异常的。
方法二:通过调用其他的类库实现
//这6个DLL找到之后在工程中引用才写后面的代码
//O2S.Components.PDFView4NET.dll
//O2S.Components.PDFRender4NET.dll
//FontBox-0.1.0-dev.dll
//IKVM.GNU.Classpath.dll
//IKVM.Runtime.dll
//PDFBox-0.7.3.dll
//这是引用的3个命名空间
using O2S.Components.PDFRender4NET;
using System.Drawing.Printing;
using O2S.Components.PDFRender4NET.Printing;
/// <summary>
/// 打印的代码
/// </summary>
/// <param name="url">要打印的PDF路径</param>
private int printShow(string url)
{
int isOK = 0;
PDFFile file = PDFFile.Open(url);
PrinterSettings settings = new PrinterSettings();
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
settings.PrinterName = "hp LaserJet 1160 PCL 5e";
settings.PrintToFile = false ;
//设置纸张大小(可以不设置,取默认设置)3.90 in, 8.65 in
PaperSize ps = new PaperSize("test",4,9);
ps.RawKind = 9; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx)
O2S.Components.PDFRender4NET.Printing.PDFPrintSettings pdfPrintSettings = new O2S.Components.PDFRender4NET.Printing.PDFPrintSettings(settings);
pdfPrintSettings.PaperSize = ps;
pdfPrintSettings.PageScaling = O2S.Components.PDFRender4NET.Printing.PageScaling.FitToPrinterMarginsProportional;
pdfPrintSettings.PrinterSettings.Copies = 1;
try
{
file.Print(pdfPrintSettings);
isOK = 1;
}
catch (Exception)
{
isOK = -1;
throw;
}
finally
{
file.Dispose();
}
return isOK;
}
//单击一个打印按钮进行打印
protected void btnPrint_Click(object sender, EventArgs e)
{
string url = "这里是PDF档的路径如:C:\\Documents and Settings\\AuYeungCK\\My Documents\\myfile\\aa1.pdf";
int isOK =-1;
isOK=printShow(url);
if (isOK > 0)
{
Response.Write("打印成功!");
}
else {
Response.Write("打印失败!");
}
}
总结:以上方法单击打印后会直接打印,但是打印出来的纸上会显示“PDFView4.0.2.0 eveluation version”字样,并发现打印有文字和图片的内容时,只打印图片部分。
方法三:加载adobe的com组件
1、打开winform界面,然后在左边的在工具栏中右击->单击choose Items->单击COM Components在里面将Adobe PDF Reader勾上确定。
2、将刚加载进来的Adobe PDF Reader控件拖到winform界面。
3、然后在加载界面输入如下代码:
string fileName = "C:\\Documents and Settings\\AuYeungCK\\My Documents\\myfile\\aa1.pdf";
this.axAcroPDF1.LoadFile(fileName);
axAcroPDF.1setShowToolbar(false);
axAcroPDF1.LoadFile(fileName);
axAcroPDF1.printAll();
另:拖过来的axAcroPDF1也可以自己创建如下代码:
AxAcroPDFLib.AxAcroPDF axAcroPDF 1= new AxAcroPDFLib.AxAcroPDF();
axAcroPDF1.Location = new System.Drawing.Point(0, 24);
axAcroPDF1.Size = new System.Drawing.Size(292, 242);
axAcroPDF1.Dock = DockStyle.Fill;
Controls.Add(axAcroPDF1);
总结:以上方法完成了显示PDF档以及打印功能,但是这样运行后会先跳出一个提示窗口是否打印,不管你是否打印都会显示一个无任何内容的adobe窗口,然后也会在winform中显示PDF档。
方法四:用第三方控件iTextSharp复制PDF档打印
/// <summary>
/// 实现PDF复制
/// </summary>
/// <param name="filePath">源PDF档</param>
/// <param name="toPath">目标c1PDF档</param>
/// <param name="print">是否实现自动打印</param>
private void ConvertPDFToPDF(string filePath, string toPath, bool print)
{
PdfReader reader = new PdfReader(filePath);
Document document = new Document(reader.GetPageSizeWithRotation(1));
int n = reader.NumberOfPages;
FileStream baos = new FileStream(toPath, FileMode.Create, FileAccess.Write);
PdfCopy copy = new PdfCopy(document, baos);
copy.ViewerPreferences = PdfWriter.HideToolbar | PdfWriter.HideMenubar;
//往pdf中写20837 ¤J内23481 ®e
document.Open();
for (int i = 1; i <= n; i++)
{
PdfImportedPage page = copy.GetImportedPage(reader, i);
copy.AddPage(page);
}
if (print)
{
PdfAction.JavaScript("myOnMessage();", copy);
copy.AddJavaScript("this.print(true);function myOnMessage(aMessage) {app.alert('Test',2);} var msgHandlerObject = new Object();doc.onWillPrint = myOnMessage;this.hostContainer.messageHandler = msgHandlerObject;");
}
document.Close();
reader.Close();
}
protected void cmdPrint_Click(object sender, EventArgs e)
{
string filePath = @"D:\iTextSharpPDF\金牌服务打印机序列号.pdf";
string toPath = @"D:\iTextSharpPDF\copy\金牌服务打印机序列号.pdf";
ConvertPDFToPDF(filePath,toPath ,true);
Response.Write("复制成功");
}
总结:此方法是实现将PDF复制到另一个地方,然后用户去打开复制的PDF档后就会直接打印,此档也会跳出Adobe的界面,但是只能实现打印功能,不能另存。