返回首页

请帮我实现打印PDF文件穿过窗户applicatoin。

回答

评论会员:游客 时间:2012/02/07
Prera​​k帕特尔
试用iText的

{A1}]
评论会员:nsk_saravana 时间:2012/02/07
进口System.IO
进口System.Drawing.Printing
进口MODI
的进口DocumentProcessing
"进口的Microsoft.Office.Interop.Word
进口Microsoft.Office.Core
进口System.Runtime.InteropServices

,进口PrinterQueueWatch
公共类Form1

私人printFont作为字体
私人streamToPrint作为StreamReader的
私人共享文件路径作为字符串
私人_MODIDocument作为MODI.Document没有
暗淡PROC作为新的System.Diagnostics.Process

私人小组中button1_Click(0 System.Object的,BYVAL e由于发件人)把手Button1.Click

文件路径="C:\个人\个人\ BackButtonDisable.txt"
  ; "印刷()

'PrintDialog1.PrinterSettings.PrintFileName =文件路径

& #160; 因素"PrintDialog1.Document = PrintDocument1
的"PrintDialog1.ShowDialog()
 0; PrintFile()
printDoc的()
的"PrintMyExcelFile()
&# 160; "PrintPDF(在"E:\个人\个人\信用卡\ PNR中IOHPTI \ frmViewPassportPage1.pdf")

 0; END SUB
"打印文件。
& #160; 公用Sub打印()
尝试
& #160; streamToPrint =新的StreamReader(路径)
尝试
  ; printFont =新字体("宋体",10)
 60;昏暗的PD作为新的PrintDocument()
&# 160; AddHandler的pd.PrintPage,,厂址pd_PrintPage
"打印文档。
pd.Print()
最后
& #160; streamToPrint.Close()
最终的结局
&# 160; 作为例外赶上前
 0;MessageBox.Show(ex.Message)
最终的结局
结束小组"印刷

私人小组pd_PrintPage(BYVAL发件人为对象,作为PrintPageEventArgs的BYVAL EV)
作为单一暗淡linesPerPage = 0
 0; 作为单暗淡yPos = 0
暗淡计数作为整数= 0
暗淡leftMargin为单= ev.MarginBounds.Left
暗淡topMargin为单= ev.MarginBounds.Top
 60; 昏暗线为String =的没什么

"计算每页的行数。
  ; linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

"打印文件的每一行。
而LT计数; linesPerPage
行= streamToPrint.ReadLine()
  ;如果线是Nothing,然后
 0; 虽然退出
结束如果
yPos = topMargin计数* printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(线,printFont,Brushes.Black,leftMargin,yPos,新的StringFormat())
&# 160; 计数= 1
年底虽然

"如果存在多行,打印另一页。
如果(线IsNot运算的Nothing)
 0; ev.HasMorePages = TRUE
&# 160; 其他
ev.HasMorePages = FALSE
结束如果
& #160; END SUB

"私人小组PrintDocument1_PrintPage(BYVAL作为对象,像System.Drawing.Printing.PrintPageEventArgs BYVALé发件人)把手PrintDocument1.PrintPage

'的暗淡linesPerPage作为单= 0
由于单= 0
'暗淡yPos 0; '昏暗的计数整数= 0
'的点心leftMargin为单= e.MarginBounds.Left
'的点心topMargin为单= e.MarginBounds.Top
'DIM行的String =没有

'''计算每页的行数。
'linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)

'''遍历文件,打印每一行。
'虽然LT计数; linesPerPage
  ; ""行= streamToPrint.ReadLine()
""如果行是Nothing
'出口虽然
&# 160; '末端如果
'yPos = topMargin计数* printFont.GetHeight(e.Graphics)
'e.Graphics.DrawString(printFont,线,Brushes.Black,leftMargin _
'yPos,新的StringFormat())
  ; '数= 1
'结束时

 0; ""如果存在多行,打印另一页。
&# 160; "如果没有(线为Nothing)
'e.HasMorePages =真
'
'e.HasMorePages =假
'末端如果
  ; "昏暗的影像System.Drawing.Image = pictureBox1.Image
&# 160; "e.Graphics.DrawImage(形象,新点(0,0))
"END SUB

私人小组PrintFile()
"_filename =文件名;
_MODIDocument =新MODI.Document()

_MODIDocument.Create(在"E:\个人\个人\医疗\ Saravana_Nov_Activities.doc")
然后,如果_MODIDocument IsNot运算没有
& #160; 昏暗d由于新的PrintDialog()
&# 160; d.AllowSomePages = TRUE
 0; d.AllowSelection =假
 60; d.PrintToFile = FALSE
 60; 昏暗的PS为新PrinterSettings()
&# 160; ps.MinimumPage = 0
&# 160; ps.MaximumPage = _MODIDocument.Images.Count - 1
ps.FromPage = 0
ps.ToPage = _MODIDocument.Images.Count - 1

&# 160; d.PrinterSettings = PS
如果d.ShowDialog()= DialogResult.OK然后

  ; _MODIDocument.PrintOut(d.PrinterSettings.FromPage,d.PrinterSettings.ToPage,d.PrinterSettings.Copies,d.PrinterSettings.PrinterName","假,_
MODI.MiPRINT_FITMODES.miPRINT_ACTUALSIZE)
 0; 结束如果
结束如果
END SUB
公共printDoc的小组()

昏暗的交流作为新Microsoft.Office.Interop.Word.Application

昏暗的应用程序作为Microsoft.Office.Interop.Word.Application = ac.Application
作为字符串暗淡lsPriterName
  ; "我的警报设置都处于关闭状态,我试图让
"这在后台打印
Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone app.DisplayAlerts =

  ; "打开文件打印...
昏暗作为对象名="C:\个人\个人\医疗\ Saravana_Nov_Activities.docx"
暗淡missingValue对象= Type.Missing的
"OpenOld使用,以便与其他版本的Word兼容
昏暗的文件的Microsoft.Office.Interop.Word.Document = app.Documents.Open(文件名,missingValue,missingValue,missingValue,missingValue,missingValue,_
missingValue,missingValue,missingValue,missingValue)

 0; '设置活动打印机
昏暗d由于新的PrintDialog()
如果d.ShowDialog()= DialogResult.OK然后
  ; d.PrinterSettings.PrinterName lsPriterName =
& #160; 其他
退出小组
结束如果

&# 160; lsPriterName app.ActivePrinter =
 0; 昏暗myTrue为对象= TRUE
 60;"在后台打印
暗淡myFalse对象= FALSE
"PrintOutOld使用独立版本
app.ActiveDocument.PrintOut(myTrue,myFalse,missingValue,missingValue,missingValue,missingValue,_
missingValue,missingValue,missingValue,missingValue,myFalse,missingValue _
&# 160; missingValue,missingValue)

document.Close(missingValue,missingValue,missingValue)

确保所有的文件都是从队列中了
虽然app.BackgroundPrintingStatus GT; 0
 60; System.Threading.Thread.Sleep(250)
年底虽然

"清理":
所以GC.Collect()
 60; GC.WaitForPendingFinalizers()
 0; Marshal.FinalReleaseComObject(APP)
& #160;Marshal.FinalReleaseComObject(AC)
文件=的没什么

END SUB

  ; "私人小组InvokePrint(BYVAL发件人作为对象,如RoutedEventArgs BYVALé)
'创建打印对话框对象,并设置选项
",点心pDialog作为新的PrintDialog()
"pDialog.PageRangeSelection = PageRangeSelection.AllPages
"pDialog.UserPageRangeEnabled = TRUE
'显示对话框。如果用户按下"打印"按钮,返回true。
"点心打印可为空(作者:[布尔值])= pDialog.ShowDialog()
如果打印= true,那么
"点心xpsDocument作为新XpsDocument(的"C:\ FixedDocumentSequence.xps",FileAccess.ReadWrite)
"点心fixedDocSeq作为FixedDocumentSequence = xpsDocument.GetFixedDocumentSequence()
"pDialog.PrintDocument(fixedDocSeq.DocumentPaginator,"测试打印作业")
'结束,如果
"END SUB

私人小组PrintMyExcelFile()
& #160; 昏暗的excelApp作为新Microsoft.Office.Interop.Excel.Application()
"打开工作簿:
&# 160; 昏暗的WB作为Microsoft.Office.Interop.Excel.Workbook = excelApp.Workbooks.Open(在"E:\个人\个人\医疗\ Card.xlsx",Type.Missing的,Type.Missing的,Type.Missing的,Type.Missing的, Type.Missing的,_
Type.Missing的,Type.Missing的,Type.Missing的,Type.Missing的,Type.Missing的,Type.Missing的,_
Type.Missing的,Type.Missing的,Type.Missing的)
'获取第一个工作表。
"(Excel使用基地1个索引,而不是基地0)
昏暗的WS作为Microsoft.Office.Interop.Excel.Worksheet = DirectCast的(wb.Worksheets(1),Microsoft.Office.Interop.Excel.Worksheet)
& #160; "默认的打印机打印出1份:
"昏暗的应用程序作为Microsoft.Office.Interop.Excel.Application = excelApp.Application
"点心lsPriterName作为字符串
&# 160; "DIM d由于新的PrintDialog()
"如果d.ShowDialog()= DialogResult.OK然后
  ; 'lsPriterName = d.PrinterSettings.PrinterName
&# 160; 'ELSE
"退出小组
'结束,如果

&# 160; 'ws.ActivePrinter = lsPriterName

'ws.ActiveDocument.PrintOutOld(Type.Missing的,Type.Missing的,Type.Missing的,Type.Missing的,Type.Missing的,Type.Missing的,_
"Type.Missing的,Type.Missing的)
ws.PrintOut(Type.Missing的,Type.Missing的,Type.Missing的,Type.Missing的,Type.Missing的,Type.Missing的,_
Type.Missing的,Type.Missing的)
"清理":
所以GC.Collect()
 0; GC.WaitForPendingFinalizers()
Marshal.FinalReleaseComObject(WS)
wb.Close(FALSE,Type.Missing的,Type.Missing的)
 60; Marshal.FinalReleaseComObject(WB)
&# 160;excelApp.Quit()
Marshal.FinalReleaseComObject(excelApp)
END SUB

 0; 公共功能PrintPDF(0弦乐,BYVAL PrinterName的字符串,将作为整数级超时PDFFile)作为整数

  ; 昏暗d由于新的PrintDialog()
如果d.ShowDialog()= DialogResult.OK然后
 0; d.PrinterSettings.PrinterName的PrinterName =
其他
exit函数
结束如果

 60; 如果PrinterName.Trim.Length = 0
 60; PrinterName的=(新System.Drawing.Printing.PrinterSettings)。PrinterName的
 0; 结束如果

"DIM的PROC作为新的System.Diagnostics.Process

&# 160;Proc.EnableRaisingEvents = TRUE
PDFFile Proc.StartInfo.FileName =
Proc.StartInfo.Arguments = CHR(34)的PrinterName CHR(34)
&# 160; Proc.StartInfo.Verb =的"PrintTo"
Proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
Proc.StartInfo.CreateNoWindow = TRUE

"现在开始监视打印机的队列。
昏暗监视器作为新PrinterQueueWatch.PrinterMonitorComponent
  ; Monitor.MonitorJobAddedEvent = FALSE
  ; Monitor.MonitorJobDeletedEvent = FALSE
& #160; Monitor.MonitorJobSetEvent = TRUE
& #160; Monitor.MonitorJobWrittenEvent = FALSE
 60; Monitor.MonitorPrinterChangeEvent = FALSE
  ; AddHandler的Monitor.JobSet,,厂址SetPrintJob
&# 160; PrinterName的Monitor.DeviceName =

"现在我看,启动Acrobat中。
Proc.Start()

 60; 这样做虽然超时GT; 0并且还没有Proc.HasExited
& #160; System.Threading.Thread.Sleep(1000)
超时 - = 1
 60; 循环

"停止监视。
RemoveHandler Monitor.JobSet,厂址SetPrintJob
监视器=的没什么

如果不Proc.HasExited,
Debug.Print("杀进程")
Proc.Kill()
结束如果

 60; Debug.WriteLine("交割过程")
& #160; Proc.Close()

返回0
结束函数
公共功能PrintPDF(0弦乐PDFFile)作为整数
返回PrintPDF(PDFFile,"",60)
结束函数
 60;公共功能PrintPDF(BYVAL作为字符串,如整数BYVAL超时PDFFile)作为整数
返回PrintPDF(PDFFile","超时)
结束函数

私人小组SetPrintJob(BYVAL发件人作为对象,BYVALé作为System.EventArgs)
打印作业打印。
System.Threading.Thread.Sleep(500)
Proc.Kill()
END SUB

尾类