2012-11-13 52 views
6

リッチテキストボックスの内容を印刷しようとしましたが、プリンタに印刷するとバグが多すぎます。 しかし、私はXPSファイル(WindowsのXPSプリンタを介して)に印刷してから、このファイルをプリンタに印刷するとすべてがOKです。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ファイルに「印刷」するプロセスが既にあるようです。私のscenariaでは、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」への参照を追加する必要が動作することを覚えておいてください。なぜ私が印刷ジョブにアクセスできないのかを覚えておくことを心配するよりも長くかかりました。

私の経験上、ほとんどのプリンタでこれをサポートしています。一般的なもので、ストレージ部門の奇妙な「バーコードプリンタ」でも動作します。

関連する問題