以下1,2種方法是可以正常進行pdf直接打印,其他方法為網頁的參攷資料,沒有具體實踐過。
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
Process processInstance = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.Verb = "Print";
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = @"/p /h \" + pdfPath + "\" \"" + pd.PrinterSettings.PrinterName + " \"";//pd.PrinterSettings.PrinterName;
2)調用本地命令行打印view plaincopy to clipboardprint?
//印刷
public void printPDF(Dictionary<string, string> dt)
{
if(File.Exists(pdfPath))
{
File.Delete(pdfPath);
}
createPDF(dt);
System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument();
Process processInstance = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.Verb = "Print";
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = @"/p /h \" + pdfPath + "\" \"" + pd.PrinterSettings.PrinterName + " \"";//pd.PrinterSettings.PrinterName;
startInfo.FileName = pdfPath;
processInstance.StartInfo = startInfo;
processInstance.Start();
//processInstance.CloseMainWindow();
}
//印刷
public void printPDF(Dictionary<string, string> dt)
{
if(File.Exists(pdfPath))
{
File.Delete(pdfPath);
}
createPDF(dt);
4)c#自帶打印類庫
processInstance.Start();
//processInstance,LV M41436 Monogram Etoile手包 老花系列 LV皮夾手袋價格、目錄、型錄、新款 官方網站旗艦店.CloseMainWindow();
}
startInfo.FileName = pdfPath;
processInstance.StartInfo = startInfo;
view plaincopy to clipboardprint?
/// <summary>
/// 實現PDF復制
/// </summary>
/// <param name="filePath">源PDF文件</param>
/// <param name="toPath">目標PDF文件</param>
/// <param name="print">是否實現自動打印</param>
private static 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中寫入內容
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();
}
/// <summary>
/// 實現PDF復制
/// </summary>
/// <param name="filePath">源PDF文件</param>
/// <param name="toPath">目標PDF文件</param>
/// <param name="print">是否實現自動打印</param>
private static 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中寫入內容
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();
}
3)通過itextsharp類
System.Drawing.Printing.PrintDocument
1)確認本機安裝adobeAcrobat 軟件,用 Acrobat.exel /h /p c:\test2.pdf
相关的主题文章: