最近整理代码,发现了以前开发的基于richtextbox控件打印的控件,决定拿出来了大家分享,共同进步。
本控件支持Form或Pag中的RichTextBox对象或/及其的内容进行纸张和页面设置功能,继而能以打印预览方式预览打印的页面效果,并能在打印预览界面供用户通过可视化方式操作指定打印内容的位置和范围,最后能够将预览所见页面和指定打印范围的内容打印在所选纸相应位置和范围,并且支持Windows操作系统中设定过的多个打印机执行此性能的打印任务。
具体功能如下:
1
、
纸张和页面设置功能;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
2
、
打印预览及;可视化方式操作指定打印内容的位置和范围
3
、对选定内容和纸张页面提供拉帘式、“即见所得”式的打印效果
4
、 能读取rtf外部文档,并通过缓存和本控件进行数据交换
部分应用代码
form2为控件所在窗体
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.Clear();
String readText = System.IO.File.ReadAllText(openFileDialog1.FileName);
richTextBox1.Rtf = readText;
richTextBox1.SaveFile(userInput,
RichTextBoxStreamType.RichText);
userInput.WriteByte(13);
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.Clear();
String readText = System.IO.File.ReadAllText(openFileDialog1.FileName);
richTextBox1.Rtf = readText;
richTextBox1.SaveFile(userInput,
RichTextBoxStreamType.RichText);
userInput.WriteByte(13);
//实际存在窗体
Form2 Form2 = new Form2();//userInput
Form2.printControl11.PrintPreview(userInput, ref s);
Form2.ShowDialog();
s= Form2.printControl11.CustomValue;
int i_temp = 0;
XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
// SetPageSetUpDialog(Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageSizeW").InnerText), Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageSizeH").InnerText), Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginL").InnerText), Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginR").InnerText), Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginT").InnerText), Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginB").InnerText), Convert.ToBoolean(doc.SelectSingleNode("/Pageset/PageScape").InnerText));
int i_width = Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageSizeDpiW").InnerText);
int i_height = Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageSizeDpiH").InnerText);
Form2 Form2 = new Form2();//userInput
Form2.printControl11.PrintPreview(userInput, ref s);
Form2.ShowDialog();
s= Form2.printControl11.CustomValue;
int i_temp = 0;
XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
// SetPageSetUpDialog(Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageSizeW").InnerText), Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageSizeH").InnerText), Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginL").InnerText), Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginR").InnerText), Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginT").InnerText), Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginB").InnerText), Convert.ToBoolean(doc.SelectSingleNode("/Pageset/PageScape").InnerText));
int i_width = Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageSizeDpiW").InnerText);
int i_height = Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageSizeDpiH").InnerText);
int i_MarginLeft = Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginHundredthsOfInchL").InnerText);
int i_MarginRight = Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginHundredthsOfInchR").InnerText);
int i_MarginTop = Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginHundredthsOfInchT").InnerText);
int i_MarginBottom = Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginHundredthsOfInchB").InnerText);
int i_MarginRight = Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginHundredthsOfInchR").InnerText);
int i_MarginTop = Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginHundredthsOfInchT").InnerText);
int i_MarginBottom = Convert.ToInt32(doc.SelectSingleNode("/Pageset/PageMarginHundredthsOfInchB").InnerText);
i_MarginLeft = Form2.printControl11.ConvertUnit_HundredthsOfInchToDPi(i_MarginLeft);
i_MarginRight = Form2.printControl11.ConvertUnit_HundredthsOfInchToDPi(i_MarginRight);
i_MarginTop = Form2.printControl11.ConvertUnit_HundredthsOfInchToDPi(i_MarginTop);
i_MarginBottom = Form2.printControl11.ConvertUnit_HundredthsOfInchToDPi(i_MarginBottom);
i_MarginRight = Form2.printControl11.ConvertUnit_HundredthsOfInchToDPi(i_MarginRight);
i_MarginTop = Form2.printControl11.ConvertUnit_HundredthsOfInchToDPi(i_MarginTop);
i_MarginBottom = Form2.printControl11.ConvertUnit_HundredthsOfInchToDPi(i_MarginBottom);
// panel1.Width = i_width;
//panel1.Height = i_height;
//int i_panelLeft = panel1.Left;
richTextBox2.Width = i_width - i_MarginLeft - i_MarginRight;
richTextBox2.Height = i_height - i_MarginTop - i_MarginBottom;
richTextBox2.Left = i_MarginLeft;
richTextBox2.Top = i_MarginTop;
//panel1.Height = i_height;
//int i_panelLeft = panel1.Left;
richTextBox2.Width = i_width - i_MarginLeft - i_MarginRight;
richTextBox2.Height = i_height - i_MarginTop - i_MarginBottom;
richTextBox2.Left = i_MarginLeft;
richTextBox2.Top = i_MarginTop;
Form2.Dispose();
userInput.Dispose();
}
}
userInput.Dispose();
}
}
转载于:https://blog.51cto.com/yaya123/343801