打印 RichTextBox

4

我正在制作一个简单的WinForms应用程序,并希望允许用户打印RichTextBox中的文本。

我按照MSDN链接的指示进行操作... 并且它可以在真实的打印机上正常工作(我指的是那些可以触摸的打印机)。

但是,如果我想使用某种类型的PDF打印机呢? 那么,我必须说仅当打印一页时它才有效。 每个下一页都会打印在同一页上,这意味着文本将被覆盖。 这是显而易见的,但我该怎么做才能强制PDF打印机创建新页面呢?

这是我的代码:

private PrintDocument docToPrint; 
private string stringToPrint;

public mainForm()
        {
            InitializeComponent();
            CenterToScreen();
            this.docToPrint = new PrintDocument();
            (...)
        }

private void tsBtnPrint_Click(object sender, EventArgs e)
        {
            PrintDialog myPrintDialog = new PrintDialog();
            myPrintDialog.AllowCurrentPage = true;
            myPrintDialog.AllowSelection = true;
            myPrintDialog.AllowSomePages = true;
            myPrintDialog.Document = docToPrint;
            if(myPrintDialog.ShowDialog()==DialogResult.OK)
            {
                StringReader reader = new StringReader(this.richTextBox.Text);
                stringToPrint = reader.ReadToEnd();
                this.docToPrint.PrintPage += new PrintPageEventHandler(this.docToPrintCustom);
                this.docToPrint.Print();
            }
        }

 private void docToPrintCustom(object sender, PrintPageEventArgs e)
        {
            Font PrintFont = this.richTextBox.Font;
            SolidBrush PrintBrush = new SolidBrush(Color.Black); 

            int LinesPerPage = 0;
            int charactersOnPage = 0;

            e.Graphics.MeasureString(stringToPrint, PrintFont, e.MarginBounds.Size, StringFormat.GenericTypographic,
                out charactersOnPage, out LinesPerPage);

            e.Graphics.DrawString(stringToPrint, PrintFont, PrintBrush, e.MarginBounds, StringFormat.GenericTypographic);

            stringToPrint = stringToPrint.Substring(charactersOnPage);

            MessageBox.Show(stringToPrint.Length.ToString());
            e.HasMorePages = (stringToPrint.Length > 0);

            PrintBrush.Dispose();
        }

我应该怎么做才能正确打印每一页?

你能否检查一下你的代码中是否只设置了一次 PrintPage 事件?如果你设置了多次,这种情况可能会发生(如果你基于MSDN示例编写代码,则构造函数中的“(...)”可能有另一个赋值)。 - vesan
嗨:) 是的,我只在代码中设置了一次PrintPage事件。没有其他分配。 - Paweł Poręba
奇怪,它在一些PDF打印机上可以工作,在另一些上却不行。我不知道是否有什么办法可以解决这个问题。 - Paweł Poręba
2
只需执行一次:this.docToPrint.PrintPage += new PrintPageEventHandler 来设置打印处理程序。你每次点击按钮时都在添加它,所以你的代码会在每次按钮点击时运行多次。你不需要那个StringReader,只需要 stringToPrint = this.richTextBox.Text;。学会使用调试器并逐步执行代码以查看发生了什么是一个好时机。MessageBox 是穷人的调试器,不要再使用它了。 - LarsTech
@LarsTech 感谢您的好建议,我非常感激 :) - Paweł Poręba
@RezaAghaei 目前还没有,因为这不是主要目标,而且似乎很复杂,但我会在适当的时间找出解决方法并告诉您 :) - Paweł Poręba
1个回答

7
您可以通过向RuchTextBox发送EM_FORMATRANGE消息来打印其内容。

此消息通常用于为输出设备(如打印机)格式化rich edit控件的内容。

要实现此解决方案,您可以执行以下步骤:
  • 首先创建一个从RichTextBox继承并实现FormatRange方法的RichtextBoxEx类,它的工作是打印控件内容的每个页面。完整代码如下所示。
  • 然后创建一个Form,放置RichTextBoxExPrintDocument,并处理PrintDocumentBeginPrintPrintPageEndPrint事件,使用以下代码执行打印。
请注意
  1. 使用此方法,您可以打印应用于文本的所有格式设置的控件内容,这比使用黑色字体和一个字体大小打印所有文本要好。还可以将内容的字体、大小和颜色设置为一种字体、大小和颜色。

  2. 创建RichtextBoxEx仅用于封装,完全是可选的,如果您想使用现有控件,只需使用我提供的FormatRange方法的内容,并将控件的属性传递给它以执行打印。

支持打印的RichTextBoxEx:

这是我上面描述的完整工作代码。该代码摘自Martin Müller的msdn文章“从.NET RichTextBox获取所见即所得的打印结果”

public class RichTextBoxEx : RichTextBox
{
    [StructLayout(LayoutKind.Sequential)]
    private struct STRUCT_RECT
    {
        public Int32 left;
        public Int32 top;
        public Int32 right;
        public Int32 bottom;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct STRUCT_CHARRANGE
    {
        public Int32 cpMin;
        public Int32 cpMax;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct STRUCT_FORMATRANGE
    {
        public IntPtr hdc;
        public IntPtr hdcTarget;
        public STRUCT_RECT rc;
        public STRUCT_RECT rcPage;
        public STRUCT_CHARRANGE chrg;
    }

    [DllImport("user32.dll")]
    private static extern Int32 SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, IntPtr lParam);

    private const Int32 WM_USER = 0x400;
    private const Int32 EM_FORMATRANGE = WM_USER + 57;
    private const Int32 EM_GETCHARFORMAT = WM_USER + 58;
    private const Int32 EM_SETCHARFORMAT = WM_USER + 68;

    /// <summary>
    /// Calculate or render the contents of our RichTextBox for printing
    /// </summary>
    /// <param name="measureOnly">If true, only the calculation is performed, otherwise the text is rendered as well</param>
    /// <param name="e">The PrintPageEventArgs object from the PrintPage event</param>
    /// <param name="charFrom">Index of first character to be printed</param>
    /// <param name="charTo">Index of last character to be printed</param>
    /// <returns> (Index of last character that fitted on the page) + 1</returns>
    public int FormatRange(bool measureOnly, PrintPageEventArgs e, int charFrom, int charTo)
    {
        // Specify which characters to print
        STRUCT_CHARRANGE cr = default(STRUCT_CHARRANGE);
        cr.cpMin = charFrom;
        cr.cpMax = charTo;

        // Specify the area inside page margins
        STRUCT_RECT rc = default(STRUCT_RECT);
        rc.top = HundredthInchToTwips(e.MarginBounds.Top);
        rc.bottom = HundredthInchToTwips(e.MarginBounds.Bottom);
        rc.left = HundredthInchToTwips(e.MarginBounds.Left);
        rc.right = HundredthInchToTwips(e.MarginBounds.Right);

        // Specify the page area
        STRUCT_RECT rcPage = default(STRUCT_RECT);
        rcPage.top = HundredthInchToTwips(e.PageBounds.Top);
        rcPage.bottom = HundredthInchToTwips(e.PageBounds.Bottom);
        rcPage.left = HundredthInchToTwips(e.PageBounds.Left);
        rcPage.right = HundredthInchToTwips(e.PageBounds.Right);

        // Get device context of output device
        IntPtr hdc = default(IntPtr);
        hdc = e.Graphics.GetHdc();

        // Fill in the FORMATRANGE structure
        STRUCT_FORMATRANGE fr = default(STRUCT_FORMATRANGE);
        fr.chrg = cr;
        fr.hdc = hdc;
        fr.hdcTarget = hdc;
        fr.rc = rc;
        fr.rcPage = rcPage;

        // Non-Zero wParam means render, Zero means measure
        Int32 wParam = default(Int32);
        if (measureOnly)
        {
            wParam = 0;
        }
        else
        {
            wParam = 1;
        }

        // Allocate memory for the FORMATRANGE struct and
        // copy the contents of our struct to this memory
        IntPtr lParam = default(IntPtr);
        lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fr));
        Marshal.StructureToPtr(fr, lParam, false);

        // Send the actual Win32 message
        int res = 0;
        res = SendMessage(Handle, EM_FORMATRANGE, wParam, lParam);

        // Free allocated memory
        Marshal.FreeCoTaskMem(lParam);

        // and release the device context
        e.Graphics.ReleaseHdc(hdc);

        return res;
    }

    /// <summary>
    /// Convert between 1/100 inch (unit used by the .NET framework)
    /// and twips (1/1440 inch, used by Win32 API calls)
    /// </summary>
    /// <param name="n">Value in 1/100 inch</param>
    /// <returns>Value in twips</returns>
    private Int32 HundredthInchToTwips(int n)
    {
        return Convert.ToInt32(n * 14.4);
    }

    /// <summary>
    /// Free cached data from rich edit control after printing
    /// </summary>
    public void FormatRangeDone()
    {
        IntPtr lParam = new IntPtr(0);
        SendMessage(Handle, EM_FORMATRANGE, 0, lParam);
    }
}

使用示例:

private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    // Start at the beginning of the text
    firstCharOnPage = 0;
}

private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
    // Clean up cached information
    richTextBoxEx1.FormatRangeDone();
}

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    firstCharOnPage = richTextBoxEx1.FormatRange(false, e, firstCharOnPage, richTextBoxEx1.TextLength);
    // check if there are more pages to print
    if (firstCharOnPage < richTextBoxEx1.TextLength)
        e.HasMorePages = true;
    else
        e.HasMorePages = false;
}

private void printToolStripButton_Click(object sender, EventArgs e)
{
    //Print the contents here
    printDocument1.Print();
}

1
代码看起来像是从从.NET RichTextBox获取WYSIWYG打印结果中提取的。你的答案应该反映出代码的来源。 - LarsTech
@LarsTech 谢谢您指向原始源代码 :) 这个答案是基于一个源代码的,我在10年前使用过它在一个旧应用程序中,我不记得源代码的起源在哪里了。 - Reza Aghaei

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接