通過幾天的尋找經測試後發現以下三種方法可以實現用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的介面,但是只能實現列印功能,不能另存。