RichTextBox旋转文本进行打印

时间:2012-11-09 08:10:33

标签: c# winforms printing rotation

美好的一天!

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

由于客户的打印机功能,我们可以通过这种方式将卡放入打印机:

example image

我尝试过两种方式设置PageSettings:

  1. PageSettings.Width = 10; PageSettings.Height = 14.
  2. PageSettings.Width = 14; PageSettings.Height = 10。
  3. 可打印区域如下所示:

    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();
        }
    

    唯一的问题是我们只能将卡片水平放入打印机。

2 个答案:

答案 0 :(得分:1)

如前所述,您应该只设置PageSettings.Landscape属性。您也可以使用PrintPageEventArgs.Graphics的图形上下文直接绘制。然后,您可以绘制任何旋转的元素或旋转的文本。 This gives a good example。然后,您不需要使用指针(IntPtr)或设备上下文(GetHDC)。

答案 1 :(得分:0)

我遇到过与专业打印机类似的问题。驱动程序可能会忽略您设置的某些设置。

我的解决方案是从文本中制作图像并仅使用默认的打印机设置,而不是试图使您的文档适合某些奇怪的打印机驱动程序。

Here is how you convert your text to image