如何打印 .rtf 文件? [英] How to print .rtf file?

查看:349
本文介绍了如何打印 .rtf 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 C# 打印 .rtf 文件?(WinForms/WPF 应用程序)

解决方案

来自 https://support.microsoft.com/en-us/kb/812425:

<块引用>

在 Visual C# .NET 或 Visual C# 2005 中,创建一个名为 RichTextBoxPrintCtrl 的新类库项目.默认情况下,创建 Class1.cs.将 Class1.cs 的名称更改为 RichTextBoxPrintCtrl.cs.在解决方案资源管理器中,右键单击引用",然后单击添加引用".在添加引用"对话框中,双击 System.Drawing.dll 和 System.Windows.Forms.dll,然后单击确定".

将 RichTextBoxPrintCtrl.cs 中的现有代码替换为以下代码:

使用系统;使用 System.Windows.Forms;使用 System.Drawing;使用 System.Runtime.InteropServices;使用 System.Drawing.Printing;命名空间 RichTextBoxPrintCtrl{公共类 RichTextBoxPrintCtrl:RichTextBox{//转换.NET框架使用的单位(1/100英寸)//和Win32 API调用使用的单位(缇1/1440英寸)private const double anInch = 14.4;[结构布局(LayoutKind.Sequential)]私有结构 RECT{公共 int 左;public int Top;public int 权利;公共 int 底部;}[结构布局(LayoutKind.Sequential)]私有结构CHARRANGE{公共 int cpMin;//范围的第一个字符(0表示文档开始)公共 int cpMax;//范围的最后一个字符(-1表示文档结束)}[结构布局(LayoutKind.Sequential)]私有结构格式{公共 IntPtr hdc;//要利用的实际DC公共 IntPtr hdcTarget;//用于确定文本格式的目标 DC公共 RECT rc;//要绘制到的 DC 区域(以缇为单位)公共 RECT rcPage;//整个DC的区域(页面大小)(以缇为单位)public CHARRANGE chrg;//要绘制的文本范围(参见前面的声明)}私有常量 int WM_USER = 0x0400;私有常量 int EM_FORMATRANGE = WM_USER + 57;[DllImport("USER32.dll")]私有静态外部 IntPtr SendMessage (IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);//渲染 RichTextBox 的内容进行打印//返回打印的最后一个字符 + 1(从这一点开始打印下一页)public int Print( int charFrom, int charTo,PrintPageEventArgs e){//计算要渲染和打印的区域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);//计算页面大小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;//表示字符从到字符到fmtRange.chrg.cpMin = charFrom;fmtRange.hdc = hdc;//使用相同的DC进行测量和渲染fmtRange.hdcTarget = hdc;//指向打印机hDCfmtRange.rc = rectToPrint;//指定要打印的页面区域fmtRange.rcPage = rectPage;//表示页面大小IntPtr res = IntPtr.Zero;IntPtr wparam = IntPtr.Zero;wparam = new IntPtr(1);//获取内存中FORMATRANGE结构的指针IntPtr lparam= IntPtr.Zero;lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));Marshal.StructureToPtr(fmtRange, lparam, false);//发送渲染数据进行打印res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);//释放分配的内存块Marshal.FreeCoTaskMem(lparam);//释放上一次调用获得的设备上下文句柄e.Graphics.ReleaseHdc(hdc);//返回最后+1个字符的打印机返回 res.ToInt32();}}}

<块引用>

在调试"菜单上,单击开始"以运行应用程序.显示 Form1.在 RichTextBoxPrintCtrl 中键入一些文本.单击页面设置以设置页面设​​置.单击打印预览以查看页面的打印预览.点击打印,打印 RichTextBoxPrintCtrl 的内容.

How to print .rtf file using C#? (WinForms/WPF application)

解决方案

From https://support.microsoft.com/en-us/kb/812425:

In Visual C# .NET or Visual C# 2005, create a new Class Library project that is named RichTextBoxPrintCtrl. By default, Class1.cs is created. Change the name of Class1.cs to RichTextBoxPrintCtrl.cs. In Solution Explorer, right-click References, and then click Add Reference. In the Add Reference dialog box, double-click System.Drawing.dll and System.Windows.Forms.dll, and then click OK.

Replace the existing code in RichTextBoxPrintCtrl.cs with the following code:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Drawing.Printing;

namespace RichTextBoxPrintCtrl
{
    public class RichTextBoxPrintCtrl:RichTextBox
    {
        //Convert the unit used by the .NET framework (1/100 inch) 
        //and the unit used by Win32 API calls (twips 1/1440 inch)
        private const double anInch = 14.4;

        [StructLayout(LayoutKind.Sequential)] 
            private struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }

        [StructLayout(LayoutKind.Sequential)]
            private struct CHARRANGE
        {
            public int cpMin;         //First character of range (0 for start of doc)
            public int cpMax;           //Last character of range (-1 for end of doc)
        }

        [StructLayout(LayoutKind.Sequential)]
            private struct FORMATRANGE
        {
            public IntPtr hdc;             //Actual DC to draw on
            public IntPtr hdcTarget;       //Target DC for determining text formatting
            public RECT rc;                //Region of the DC to draw to (in twips)
            public RECT rcPage;            //Region of the whole DC (page size) (in twips)
            public CHARRANGE chrg;         //Range of text to draw (see earlier declaration)
        }

        private const int WM_USER  = 0x0400;
        private const int EM_FORMATRANGE  = WM_USER + 57;

        [DllImport("USER32.dll")]
        private static extern IntPtr SendMessage (IntPtr hWnd , int msg , IntPtr wp, IntPtr lp); 

        // Render the contents of the RichTextBox for printing
        //  Return the last character printed + 1 (printing start from this point for next page)
        public int Print( int charFrom, int charTo,PrintPageEventArgs e)
        {
            //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

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

    }
}

On the Debug menu, click Start to run the application. Form1 is displayed. Type some text in RichTextBoxPrintCtrl. Click Page Setup to set the page settings. Click Print Preview to view the print preview of the page. Click Print to print the content of RichTextBoxPrintCtrl.

这篇关于如何打印 .rtf 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆