0

设置图片

Image image = Image.FromFile(@"C:\a.jpg");
Clipboard.SetImage(image);
richTextBox1.Paste();

文字打印

PrintDialog printDialog = new PrintDialog();
PrintDocument documentToPrint = new PrintDocument();
printDialog.Document = documentToPrint;
if (printDialog.ShowDialog() == DialogResult.OK)
{
    StringReader reader = new StringReader(richTextBox1.Text);
    documentToPrint.PrintPage += new PrintPageEventHandler(DocumentToPrint_PrintPage);
    documentToPrint.Print();
}


private void DocumentToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    StringReader reader = new StringReader(richTextBox1.Text);
    float LinesPerPage = 0;
    float YPosition = 0;
    int Count = 0;
    float LeftMargin = e.MarginBounds.Left;
    float TopMargin = e.MarginBounds.Top;
    string Line = null;
    Font PrintFont = this.richTextBox1.Font;
    SolidBrush PrintBrush = new SolidBrush(Color.Black);

    LinesPerPage = e.MarginBounds.Height / PrintFont.GetHeight(e.Graphics);

    while (Count < LinesPerPage && ((Line = reader.ReadLine()) != null))
    {
        YPosition = TopMargin + (Count * PrintFont.GetHeight(e.Graphics));
        e.Graphics.DrawString(Line, PrintFont, PrintBrush, LeftMargin, YPosition, new StringFormat());
        Count++;
    }

    if (Line != null)
    {
        e.HasMorePages = true;
    }
    else
    {
        e.HasMorePages = false;
    }
    PrintBrush.Dispose();
}

此代码仅在richtextbox 中打印文本,但如何在richtextbox 中打印文本和图像?

4

1 回答 1

0

我认为,你需要重写函数DocumentToPrint_PrintPage来渲染 rtf 到e.Graphics文本和图像。现在,该函数只渲染来自RichTextBox.Text.

您需要读取RichTextBox.rtf、解析并绘制它,例如e.Graphics.DrawString( docs ) 用于文本和e.Graphics.DrawImage( docs ) 用于图像

于 2012-06-17T12:49:37.753 回答