2012-11-09 22 views
9

おはよう!RichTextBox印刷用の回転テキスト

リッチテキストボックスから短いカードを印刷する必要があります。 カードのサイズは10x14 cmです。我々は、プリンタにのみ、このようにカードを置くことができますので、お客様のプリンタ機能の

example image

私はPageSettingsに二つの方法を設定しようとしました:

  1. PageSettings.Width = 10 ; PageSettings.Height = 14.
  2. PageSettings.Width = 14; PageSettings.Height = 10

そして、印刷可能領域のようになります。

enter image description here

ここで印刷が解放されたかのコードがあります:

btnRotate.CheckedChanged += (s, e) => InitPaperSize(); 

private void InitPaperSize() 
    { 
     string name = btnRotate.Checked ? "ShortCard (rotate)" : "ShortCard"; 
     int width = Centimeters(btnRotate.Checked ? 14 : 10); 
     int height = Centimeters(btnRotate.Checked ? 10 : 14); 

     System.Drawing.Printing.PaperSize ps = new System.Drawing.Printing.PaperSize(name, width, height); 
     printDocument.DefaultPageSettings.PaperSize = ps; 
    } 

private int Centimeters(int centimeters) 
    { 
     return (int)((centimeters * 100)/2.54); 
    } 

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;//делим на 100 так как границы указываются в сотых долях дюйма 
     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(); 
    } 

唯一の問題は、私たちですカードはプリンタに水平にしか置くことができません。

+3

プリンタは、LandscapeおよびPortraitモード、PageSettings.Landscapeプロパティでの印刷をサポートしています。あなた自身が出力を回転させるよりも、全体のチェックが簡単です。 –

+0

私もこの方法を試しました。しかし、ランドスケープでは、印刷コンテンツだけを回転できますが、印刷可能な領域は回転できません。したがって、プリンタ内の紙の位置は変わりません。 –

答えて

0

特殊なプリンタでも同様の問題がありました。ドライバは設定した設定の一部を無視することがあります。

私の解決策は、テキストからイメージを作成し、あなたのドキュメントをいくつかの奇妙なプリンタドライバに合わせようとするのではなく、デフォルトのプリンタ設定を使用することでした。

Here is how you convert your text to image

1

誰かが前に言ったように、あなたは単にPageSettings.Landscapeプロパティを設定する必要があります。 PrintPageEventArgs.Graphicsのグラフィックスコンテキストを使用して直接描画することもできます。回転した要素や回転したテキストを描画できます。 This gives a good example。次に、ポインタ(IntPtr)またはデバイスコンテキスト(GetHDC)を使用する必要はありません。

+0

これはどうやってやるのですか:RichTextBoxの内容を取得し、それが必要な方法で 'PrintDocument'で"描画 "します。 http://msdn.microsoft.com/fr-fr/library/system.drawing.printing.printdocument(v=vs.80).aspx –

関連する問題