在.NET中生成,显示和打印简单文档

时间:2011-01-08 17:32:26

标签: c# .net wpf winforms printing

我想在C#中做很常见的任务,但我无法弄清楚:我的应用程序将生成包含大量文本和一些图片的文档,让用户预览结果然后让他打印出来。 最简单的方法是什么?我把我从文件库中放入文档的文本。

说明:

3 个答案:

答案 0 :(得分:1)

也许这个例子会对你有所帮助。这实际上是基于WindowsForms,部分来自MSDN。使用以下代码:

       using (Printer p = new Printer(this.richTextBox.Text, 1)) { }

这里需要富文本框中的文本,但你可以在那里添加任何字符串。

在您的应用程序中创建一个新表单并添加以下代码:

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;

namespace PrinterExample
{
    public partial class Printer : Form
    {
        private string textToDisplay;
        private Font printFont;
        private StreamReader streamToPrint;
        private int mode;
       //mode 1 - Preview, 2 - Print
        public Printer(string textToDisplay,int mode)
        {
            this.textToDisplay = textToDisplay;
            this.mode = mode;
            InitializeComponent();
            PreviewPage();

        }

        internal void PreviewPage()
        {
            try
            {
                streamToPrint = new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(textToDisplay)));
                printFont = DefaultFont;
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler
                   (this.pd_PrintPage);

                var ppd = new PrintPreviewDialog();
                ppd.Document = pd;

                if (mode == 1) ppd.Show();
                if (mode == 2) pd.Print();
            }
            catch
            {
                MessageBox.Show("Exception occured", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float rightMargin = ev.MarginBounds.Right;
            float topMargin = ev.MarginBounds.Top;
            string line = null;

            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height /
               printFont.GetHeight(ev.Graphics);

            float charsPerLine = (rightMargin - leftMargin) / (printFont.GetHeight(ev.Graphics)*0.65f);

            // Print each line of the file.
            while (count < linesPerPage &&
               ((line = streamToPrint.ReadLine()) != null))
            {
                string newLine = null;
                int newLineCounter = 0;
                for (int i = 0; i < line.Length; i++)
                {
                    if (i % (int)charsPerLine == 0)
                    {
                        newLine = line.Substring((int)charsPerLine * newLineCounter, (int)charsPerLine > (line.Length - (int)charsPerLine * newLineCounter) ? (line.Length - (int)charsPerLine * newLineCounter) : (int)charsPerLine); 


                        yPos = topMargin + (count *
                           printFont.GetHeight(ev.Graphics));
                        ev.Graphics.DrawString(newLine, printFont, Brushes.Black,
                           leftMargin, yPos, new StringFormat());

                        count++;
                        newLineCounter++;
                    }

                }
                newLineCounter = 0;
            }

            // If more lines exist, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }

        private void Printer_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.streamToPrint.Close();
        }

    }

请注意,对于专业打印,大多数人都使用Crystal Reports等外部工具。我不确定你是否可以修改这个例子来打印图像。

答案 1 :(得分:1)

一种选择是为您执行内置的.rdlc报告

http://msdn.microsoft.com/en-us/library/ms252067(v=VS.90).aspx

答案 2 :(得分:1)

Drasto,

我创建了一个相当复杂的自定义打印工具,可能很有用。

PrintPage PrintPageEventHandler Is Printing Too Many Copies

随意窃取我想要的代码。