C#打印(RichTextBox)
我想打印RichTextBox(eintragRichTextBox)的内容 我现在有以下代码:C#打印(RichTextBox),c#,printing,C#,Printing,我想打印RichTextBox(eintragRichTextBox)的内容 我现在有以下代码: private void druckenPictureBox_Click(object sender, EventArgs e) { PrintDialog printDialog = new PrintDialog(); PrintDocument documentToPrint = new PrintDocument(); printDialog.Document = d
private void druckenPictureBox_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
PrintDocument documentToPrint = new PrintDocument();
printDialog.Document = documentToPrint;
if (printDialog.ShowDialog() == DialogResult.OK)
{
StringReader reader = new StringReader(eintragRichTextBox.Text);
documentToPrint.Print();
documentToPrint.PrintPage += new PrintPageEventHandler(DocumentToPrint_PrintPage);
}
}
private void DocumentToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
StringReader reader = new StringReader(eintragRichTextBox.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.eintragRichTextBox.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();
}
private void druckenPictureBox\u单击(对象发送方,事件参数e)
{
PrintDialog PrintDialog=新建PrintDialog();
PrintDocument DocumentTopPrint=新的PrintDocument();
printDialog.Document=DocumentTopPrint;
if(printDialog.ShowDialog()==DialogResult.OK)
{
StringReader=新的StringReader(eintragRichTextBox.Text);
documentToPrint.Print();
DocumentTopPrint.PrintPage+=新的PrintPageEventHandler(DocumentTopPrint\u PrintPage);
}
}
私有void DocumentToPrint_PrintPage(对象发送者,System.Drawing.Printing.PrintPageEventArgs e)
{
StringReader=新的StringReader(eintragRichTextBox.Text);
float LinesPerPage=0;
浮动位置=0;
整数计数=0;
浮动左边距=e.MarginBounds.Left;
浮动顶部边距=e.MarginBounds.Top;
字符串行=null;
Font PrintFont=this.eintragRichTextBox.Font;
SolidBrush PrintBrush=新的SolidBrush(颜色为黑色);
LinesPerPage=e.MarginBounds.Height/PrintFont.GetHeight(e.Graphics);
而(Count
但它总是给我一个空白的网站:(…有人有想法,为什么它不起作用?
或者有没有更好的代码/想法来实现打印
编辑:
看答案,明白了
在这个地方:
if (printDialog.ShowDialog() == DialogResult.OK)
{
StringReader reader = new StringReader(eintragRichTextBox.Text);
documentToPrint.Print();
documentToPrint.PrintPage += new PrintPageEventHandler(DocumentToPrint_PrintPage);
}
我改为:
if (printDialog.ShowDialog() == DialogResult.OK)
{
StringReader reader = new StringReader(eintragRichTextBox.Text);
documentToPrint.PrintPage += new PrintPageEventHandler(DocumentToPrint_PrintPage);
documentToPrint.Print();
}
现在它很好用
另外,如果有人需要打印RichTextBox的内容,您可以使用我的代码。您的代码似乎有问题,它只会因为
StringReader reader = new StringReader(eintragRichTextBox.Text);
在您的文档toprint\u PrintPage()中使用它
StringReader reader;
int prov = 0;
private void DocumentToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (prov == 0)
{
prov = 1;
reader = new StringReader(eintragRichTextBox.Text);
}
float LinesPerPage = 0;
float YPosition = 0;
int Count = 0;
float LeftMargin = e.MarginBounds.Left;
float TopMargin = e.MarginBounds.Top;
string Line = null;
我试过了,效果很好
您只需创建一个自定义的RichTextBox
,然后它就可以快速打印整个文本,并保持纸张上的样式
用于创建自定义RichTextBox的代码
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();
}
}
}
主要的源代码包括PrintDialog、PrintPreviewDialog、PrintDocument和PageSetupDialog
,但是只需一个PrintDocument
控件就可以了。因此我删除了一些额外的代码,只是为了缩短有用的部分。但是如果您有兴趣全部使用它们,可以在链接页面中找到完整的代码
private PrintDocument _printDocument = new PrintDocument();
private int _checkPrint;
public Form1()
{
InitializeComponent();
_printDocument.BeginPrint += _printDocument_BeginPrint;
_printDocument.PrintPage += _printDocument_PrintPage;
}
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDialog printDialog=new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
_printDocument.Print();
}
private void _printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// Print the content of RichTextBox. Store the last character printed.
_checkPrint = rchEditor.Print(_checkPrint, rchEditor.TextLength, e);
// Check for more pages
e.HasMorePages = _checkPrint < rchEditor.TextLength;
}
private void _printDocument_BeginPrint(object sender, PrintEventArgs e)
{
_checkPrint = 0;
}
private PrintDocument\u PrintDocument=new PrintDocument();
私人内部检查打印;
公共表格1()
{
初始化组件();
_printDocument.BeginPrint+=\u printDocument\u BeginPrint;
_printDocument.PrintPage+=\u printDocument\u PrintPage;
}
私有void btnPrint\u单击(对象发送方,事件参数e)
{
PrintDialog PrintDialog=新建PrintDialog();
if(printDialog.ShowDialog()==DialogResult.OK)
_printDocument.Print();
}
私有void\u printDocument\u PrintPage(对象发送者,PrintPageEventArgs e)
{
//打印RichTextBox的内容。存储打印的最后一个字符。
_checkPrint=rchEditor.Print(_checkPrint,rchEditor.TextLength,e);
//查看更多页面
e、 HasMorePages=_checkPrint
为什么要在documentToPrint.Print()之前创建“StringReader阅读器…”?我想你不需要它。Aso,调试时会发生什么?我还没有发现你的问题,但有一件事是,如果你的RTB有足够的文本填充多个页面,你会发现它打印了无限多的页面。你需要声明你的读卡器、计数和行值超出了你的PrintPage e范围发泄…你还需要做一些调试,添加一些断点,看看什么值被设置为预期值,哪些是无效的。也许代码可以工作,但可能是,我没有足够的墨水。我会尽快检查。没有,有足够的墨水,我可以用文本打印word文档。我的RTB没有很多文本,有时只有一个短语s、 。调试不起作用,不知道为什么。。这不会以样式打印文档,而且打印长文本需要很长时间!我4年前编写了此代码,当时的目标不是以样式打印。无论如何,感谢您在回答中提供了新的变体。无法使用set-printerDocument.DefaultPageSettings.PaperSize和use这个方法。这就是我的问题所在。