2012-11-09 124 views
9

美好的一天!RichTextBox旋轉文字印刷

我需要從RichTextBox打印短卡。 卡的大小是10x14釐米。

由於客戶的打印機功能,我們可以把卡在打印機只能這樣說:

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

打印機支持以橫向和縱向模式打印,PageSettings.Landscape屬性。比自己旋轉輸出更容易。 –

+0

我也試過這種方法。但是,橫向允許僅旋轉打印內容,但不打印區域。所以紙張在打印機內的位置不會改變。 –

回答

0

我有專門的打印機有類似的問題。驅動程序可能會忽略您設置的一些設置。

我的解決方案是從文本中製作圖像,並使用默認的打印機設置,而不是試圖使您的dokument適合一些奇怪的打印機驅動程序。

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 –