2012-11-13 275 views
6

我試圖打印RichTextBox的內容,如果我打印到打印機上的問題太多。 但是,當我打印到XPS文件(通過Windows中的XPS打印機),然後將此文件打印到打印機一切正常。打印成XPS文件,然後將其打印到打印機

所以我可以通過編程來完成所有這些事情嗎?

這裏是我的打印方法:

public int PrintRotate(bool rotate, PrintPageEventArgs e, int charFrom, int charTo) 
    { 
     //Calculate the area to render and print 
     RECT rectToPrint; 
     rectToPrint.Top = (int)(e.MarginBounds.Top * anInch); 
     rectToPrint.Bottom = (int)(e.MarginBounds.Bottom * anInch); 
     rectToPrint.Left = (int)(e.MarginBounds.Left * anInch); 
     rectToPrint.Right = (int)(e.MarginBounds.Right * anInch); 

     //Calculate the size of the page 
     RECT rectPage; 
     rectPage.Top = (int)(e.PageBounds.Top * anInch); 
     rectPage.Bottom = (int)(e.PageBounds.Bottom * anInch); 
     rectPage.Left = (int)(e.PageBounds.Left * anInch); 
     rectPage.Right = (int)(e.PageBounds.Right * anInch); 

     IntPtr hdc = e.Graphics.GetHdc(); 

     FORMATRANGE fmtRange; 
     fmtRange.chrg.cpMax = charTo;    //Indicate character from to character to 
     fmtRange.chrg.cpMin = charFrom; 
     fmtRange.hdc = hdc;     //Use the same DC for measuring and rendering 
     fmtRange.hdcTarget = hdc;    //Point at printer hDC 
     fmtRange.rc = rectToPrint;    //Indicate the area on page to print 
     fmtRange.rcPage = rectPage;   //Indicate size of page 


     SetGraphicsMode(fmtRange.hdc, GM_ADVANCED); 

     XFORM par = new XFORM(); 

     par = new XFORM(); 
     par.eM11 = 1; 
     par.eM12 = 0; 
     par.eM21 = 0; 
     par.eM22 = 1; 
     par.eDx = -e.PageSettings.Margins.Left/100 * e.PageSettings.PrinterResolution.X; 
     par.eDy = -e.PageSettings.Margins.Top/100 * e.PageSettings.PrinterResolution.Y; 
     ModifyWorldTransform(fmtRange.hdc, ref par, MWT_LEFTMULTIPLY); 

     IntPtr res = IntPtr.Zero; 

     IntPtr wparam = IntPtr.Zero; 
     wparam = new IntPtr(1); 

     //Get the pointer to the FORMATRANGE structure in memory 
     IntPtr lparam = IntPtr.Zero; 
     lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange)); 
     Marshal.StructureToPtr(fmtRange, lparam, false); 

     //Send the rendered data for printing 
     res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam); 

     //Free the block of memory allocated 
     Marshal.FreeCoTaskMem(lparam); 

     //Release the device context handle obtained by a previous call 
     e.Graphics.ReleaseHdc(hdc); 

     //Return last + 1 character printer 
     return res.ToInt32(); 
    } 
+1

您是否曾經爲此找到過解決方案?我正在做同樣的事情。 –

+0

不幸的是沒有。這項任務現在對我來說優先級低。 –

+0

我也想要一個解決方案。 – Jeff

回答

1

我有這樣的問題,並最終作出了.XPS文件,然後發送該打印機。

從您的問題中,聽起來您已經有了「打印」到xps文件的過程,這很好,因爲我不知道將富文本框打印到xps文件的過程。在我的場景中,我需要在不使用ms office的情況下打印一個文件夾,所以我最終制作了一個XPS文件,用代碼編輯它,然後將其發送到打印機。

這是我使用的XPS文件直接發送到打印機代碼:

LocalPrintServer localPrintServer = new LocalPrintServer(); 
var queue = localPrintServer.GetPrintQueue("NameOfPrinter"); 
PrintSystemJobInfo xpsPrintJob = queue.AddJob("name of print job", "my/xps/path.xps",false); 

還記得這個代碼的工作,你需要將引用添加到System.Printing AND「ReachFramework」。花了我更多的時間,比我想要記住找出爲什麼我無法訪問打印作業。

大多數打印機應該以我的經驗支持這一點。常見的,它甚至可以在存儲部門的奇數「條碼打印機」上工作。

相關問題