【问题标题】:Generate, display and print simple document in .NET在 .NET 中生成、显示和打印简单文档
【发布时间】:2011-01-08 17:32:25
【问题描述】:

我想在 C# 中执行非常常见的任务,但我不知道如何:我的应用程序将生成包含大量文本和一些图片的文档,让用户预览结果然后让他打印。 最简单的方法是什么?我从数据库中获取我放入文档的文本。

备注:

【问题讨论】:

  • 在我看来,我应该使用DocumentViewer 控件(WPF)或PrintPreviewControl(Windows 窗体)或ReportViewer(Windows 窗体)。但我不知道如何为他们创建文档。
  • 刚刚谷歌搜索并找到了这个例子。 nbdtech.com/Blog/archive/2009/04/20/… 但它看起来不像有预览版。我想你应该需要 PrintPreviewDialog 或 PrintPreviewControl 来进行预览。请参阅这篇 MSDN 文章 msdn.microsoft.com/en-us/magazine/cc188767.aspx

标签: c# .net wpf winforms printing


【解决方案1】:

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

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

这里它从richTextBox中获取文本,但是你可以在里面放任何字符串。

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

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 等外部工具。我不确定您是否可以修改此示例以打印图像。

【讨论】:

  • 我会附议您不希望将其用于任何类型的数据驱动报告或文档。通过其他方式来实现它更简单更好。
  • 我还需要图片,无法以任何方式修改它以呈现它们。所以没有简单的方法可以用 C# 中的文本和一些图片创建 Document 吗?!那水晶报表呢?有教程或文档吗?
  • 致 msarchet:那么请告诉我那是什么。我找不到任何解决方案。
  • @msarchet 完全同意,这只是一个“临时”解决方案。 @drasto 有许多强大的打印组件,但其中大多数都不是免费的。我不完全确定,但在我看来 Visual Studio 的 Crystal Reports 插件是免费的。
  • @drasto,Crystal Reports,Dev Express 的报告,组件一报告,Telerik 的报告。大多数 .NET 组件提供商都将内置某种报告解决方案。我在工作中使用 Dev Express,我真的很喜欢他们的报告解决方案
【解决方案2】:

一种选择是为您的任务使用内置的 .rdlc 报告

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

【讨论】:

    【解决方案3】:

    德拉斯托,

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

    PrintPage PrintPageEventHandler Is Printing Too Many Copies

    您可以随意窃取我的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-21
      • 2016-01-03
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 2014-07-02
      • 1970-01-01
      • 2012-04-01
      相关资源
      最近更新 更多