从c# /。net打印的最佳方式?

时间:2022-10-30 21:31:43

What is the best way to print stuff from c#/.net?

从c#/。net打印东西的最好方法是什么?

The question is in regard to single pages as well as to reports containing lots of pages.

问题是关于单个页面以及包含大量页面的报告。

It would be great to get a list of the most common printing libs containing the main features and gotchas of each of them.

最好能得到一个最常见的打印列表,其中包含每一个的主要特性和问题。

[Update] for standard windows clients (or servers), not for web apps, please.

[更新]对于标准的windows客户端(或服务器),而不是web应用程序。

6 个解决方案

#1


9  

For reports, I use the RDLC control.

对于报告,我使用RDLC控件。

For everything else, I use the inherent printing objects within .NET.

对于其他的东西,我使用。net中固有的打印对象。

Edit The inherent printing objects are all found in the System.Drawing.Printing namespace. When you use the PrintDialog or the PrintPreviewDialog in a WinForms (or WPF) application, it is to these objects that you're turning over control.

编辑固有的打印对象都可以在System.Drawing中找到。打印名称空间。当您在WinForms(或WPF)应用程序中使用PrintDialog或PrintPreviewDialog时,您要将控制权交给这些对象。

The fundamental concept is that you're drawing to the printer. The simplest form of this is:

最基本的概念是你要画到打印机上。最简单的形式是:

Sub MyMethod()
     Dim x as New PrintDocument
     AddHandler x.PrintPage, AddressOf printDoc_PrintPage
     x.Print
End Sub
Sub printDoc_PrintPage( sender as Object,  e as PrintPageEventArgs)
      Dim textToPrint as String= ".NET Printing is easy"
      dim printFont as new Font("Courier New", 12)
      dim leftMargin as int= e.MarginBounds.Left
      dim topMargin as int = e.MarginBounds.Top
      e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, leftMargin, topMargin)
End Sub

What's happening here is that when my object (x) is sent the print command, it raises the "PRINT PAGE" event (which is designed to print 1 page at a time). This event then uses the Graphics attribute of the PrintPageEventArgs to draw the relevant string directly to the print spooler.

这里发生的事情是,当我的对象(x)被发送打印命令时,它会引发“打印页面”事件(每次打印1页)。然后,该事件使用PrintPageEventArgs的图形属性将相关字符串直接绘制到打印假脱机程序。

Here's one tutorial, and a quick Google search for ".NET printing tutorial" returns a bit over 200K results.

这里有一个教程,以及一个快速的谷歌搜索“。NET打印教程“返回超过200K的结果。

#2


2  

» Sample Code to show basics of Printing in Windows Forms Applications:

在Windows窗体应用程序中显示打印基础的示例代码:

    using System.Drawing.Printing;

    PrintDocument printDoc = new PrintDocument();
    printDoc.DefaultPageSettings.Landscape = true;
    printDoc.DefaultPageSettings.Margins.Left = 100; //100 = 1 inch = 2.54 cm
    printDoc.DocumentName = "My Document Name"; //this can affect name of output PDF file if printer is a PDF printer
    //printDoc.PrinterSettings.PrinterName = "CutePDF";
    printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);

    PrintDialog printDialog = new PrintDialog();
    printDialog.Document = printDoc; //Document property must be set before ShowDialog()

    DialogResult dialogResult = printDialog.ShowDialog();
    if (dialogResult == DialogResult.OK)
    {
        printDoc.Print(); //start the print
    }   

    void printDoc_PrintPage(object sender, PrintPageEventArgs e)
    {
        Graphics g = e.Graphics;
        string textToPrint = ".NET Printing is easy";
        Font font = new Font("Courier New", 12);
        // e.PageBounds is total page size (does not consider margins)
        // e.MarginBounds is the portion of page inside margins
        int x1 = e.MarginBounds.Left;
        int y1 = e.MarginBounds.Top;
        int w = e.MarginBounds.Width;
        int h = e.MarginBounds.Height;

        g.DrawRectangle(Pens.Red, x1, y1, w, h); //draw a rectangle around the margins of the page, also we can use: g.DrawRectangle(Pens.Red, e.MarginBounds)
        g.DrawString(textToPrint, font, Brushes.Black, x1, y1);

        e.HasMorePages = false; //set to true to continue printing next page
    }

#3


1  

We used a set of third party DLLs from PDFSharp who in turn use DLLs from MigraDoc. I'm not privy to all the reasons that we went that direction (the decision was made by a senior developer), but I can tell you that:

我们使用了一组来自PDFSharp的第三方dll,而这些dll又是来自MigraDoc的dll。我不知道我们朝那个方向发展的所有原因(这个决定是由一位资深开发人员做出的),但我可以告诉你:

  • It seems to be in active development.
  • 它似乎在积极发展。
  • It had most of the features we needed.
  • 它有我们需要的大部分特性。
  • The source code is available. Although it used some patterns and conventions that I hadn't seen before, once I got on to them, it was fairly easy to make the changes. I added support for using the System.Drawing.Image directly rather than as saving files.
  • 源代码是可用的。虽然它使用了一些我以前从未见过的模式和惯例,但一旦我开始使用它们,就很容易做出改变。我添加了对使用System.Drawing的支持。直接映像而不是保存文件。
  • It is not documented well either internally or externally.
  • 在内部或外部都没有很好的文档记录。

#4


1  

Loads of stuff, you say. Hum, seems that you should use a solution with a designer, so you should look into Crystal Reports and RDLC. There's also the Reporting Services solution, but in that case you would need a server with SQL Server.

很多东西,你说。嗯,似乎您应该与设计人员一起使用解决方案,因此您应该查看Crystal报表和RDLC。还有报告服务解决方案,但是在这种情况下,您需要一个带有SQL server的服务器。

Crystal Reports seems to give you more choices, but needs a little more learning than RDLC.

Crystal报表似乎给了您更多的选择,但是比RDLC需要更多的学习。

I wouldn't recommend you create those in HTML + CSS, because of the limitations and the extra work you would have to throw at it.

我不建议您在HTML + CSS中创建它们,因为您必须对其进行限制和额外的工作。

#5


1  

If you can build your output as a FlowDocument, you can turn it into XPS easily to get an "electronic" version, and the print the XPS.

如果可以将输出构建为FlowDocument,那么可以轻松地将其转换为XPS,以获得“电子”版本,并打印XPS。

#6


0  

It depends a lot on the requirements of your application.

这在很大程度上取决于应用程序的需求。

Even though it isn't the perfect tool (really far from that), Crystal Reports tends to be a good choice. It gives you the option of getting data directly from a Database or, if you already have a list of objects you want to print, you can pass them to the document and bind the object properties to the labels of the report.

尽管它不是完美的工具(远远不是),Crystal report往往是一个不错的选择。它提供了直接从数据库获取数据的选项,或者,如果您已经有了想要打印的对象列表,您可以将它们传递到文档,并将对象属性绑定到报表的标签。

But give us some more information of what you're trying to do, so you can receive better proposals.

但请给我们更多的信息,你正在做什么,以便你能收到更好的建议。

#1


9  

For reports, I use the RDLC control.

对于报告,我使用RDLC控件。

For everything else, I use the inherent printing objects within .NET.

对于其他的东西,我使用。net中固有的打印对象。

Edit The inherent printing objects are all found in the System.Drawing.Printing namespace. When you use the PrintDialog or the PrintPreviewDialog in a WinForms (or WPF) application, it is to these objects that you're turning over control.

编辑固有的打印对象都可以在System.Drawing中找到。打印名称空间。当您在WinForms(或WPF)应用程序中使用PrintDialog或PrintPreviewDialog时,您要将控制权交给这些对象。

The fundamental concept is that you're drawing to the printer. The simplest form of this is:

最基本的概念是你要画到打印机上。最简单的形式是:

Sub MyMethod()
     Dim x as New PrintDocument
     AddHandler x.PrintPage, AddressOf printDoc_PrintPage
     x.Print
End Sub
Sub printDoc_PrintPage( sender as Object,  e as PrintPageEventArgs)
      Dim textToPrint as String= ".NET Printing is easy"
      dim printFont as new Font("Courier New", 12)
      dim leftMargin as int= e.MarginBounds.Left
      dim topMargin as int = e.MarginBounds.Top
      e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, leftMargin, topMargin)
End Sub

What's happening here is that when my object (x) is sent the print command, it raises the "PRINT PAGE" event (which is designed to print 1 page at a time). This event then uses the Graphics attribute of the PrintPageEventArgs to draw the relevant string directly to the print spooler.

这里发生的事情是,当我的对象(x)被发送打印命令时,它会引发“打印页面”事件(每次打印1页)。然后,该事件使用PrintPageEventArgs的图形属性将相关字符串直接绘制到打印假脱机程序。

Here's one tutorial, and a quick Google search for ".NET printing tutorial" returns a bit over 200K results.

这里有一个教程,以及一个快速的谷歌搜索“。NET打印教程“返回超过200K的结果。

#2


2  

» Sample Code to show basics of Printing in Windows Forms Applications:

在Windows窗体应用程序中显示打印基础的示例代码:

    using System.Drawing.Printing;

    PrintDocument printDoc = new PrintDocument();
    printDoc.DefaultPageSettings.Landscape = true;
    printDoc.DefaultPageSettings.Margins.Left = 100; //100 = 1 inch = 2.54 cm
    printDoc.DocumentName = "My Document Name"; //this can affect name of output PDF file if printer is a PDF printer
    //printDoc.PrinterSettings.PrinterName = "CutePDF";
    printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);

    PrintDialog printDialog = new PrintDialog();
    printDialog.Document = printDoc; //Document property must be set before ShowDialog()

    DialogResult dialogResult = printDialog.ShowDialog();
    if (dialogResult == DialogResult.OK)
    {
        printDoc.Print(); //start the print
    }   

    void printDoc_PrintPage(object sender, PrintPageEventArgs e)
    {
        Graphics g = e.Graphics;
        string textToPrint = ".NET Printing is easy";
        Font font = new Font("Courier New", 12);
        // e.PageBounds is total page size (does not consider margins)
        // e.MarginBounds is the portion of page inside margins
        int x1 = e.MarginBounds.Left;
        int y1 = e.MarginBounds.Top;
        int w = e.MarginBounds.Width;
        int h = e.MarginBounds.Height;

        g.DrawRectangle(Pens.Red, x1, y1, w, h); //draw a rectangle around the margins of the page, also we can use: g.DrawRectangle(Pens.Red, e.MarginBounds)
        g.DrawString(textToPrint, font, Brushes.Black, x1, y1);

        e.HasMorePages = false; //set to true to continue printing next page
    }

#3


1  

We used a set of third party DLLs from PDFSharp who in turn use DLLs from MigraDoc. I'm not privy to all the reasons that we went that direction (the decision was made by a senior developer), but I can tell you that:

我们使用了一组来自PDFSharp的第三方dll,而这些dll又是来自MigraDoc的dll。我不知道我们朝那个方向发展的所有原因(这个决定是由一位资深开发人员做出的),但我可以告诉你:

  • It seems to be in active development.
  • 它似乎在积极发展。
  • It had most of the features we needed.
  • 它有我们需要的大部分特性。
  • The source code is available. Although it used some patterns and conventions that I hadn't seen before, once I got on to them, it was fairly easy to make the changes. I added support for using the System.Drawing.Image directly rather than as saving files.
  • 源代码是可用的。虽然它使用了一些我以前从未见过的模式和惯例,但一旦我开始使用它们,就很容易做出改变。我添加了对使用System.Drawing的支持。直接映像而不是保存文件。
  • It is not documented well either internally or externally.
  • 在内部或外部都没有很好的文档记录。

#4


1  

Loads of stuff, you say. Hum, seems that you should use a solution with a designer, so you should look into Crystal Reports and RDLC. There's also the Reporting Services solution, but in that case you would need a server with SQL Server.

很多东西,你说。嗯,似乎您应该与设计人员一起使用解决方案,因此您应该查看Crystal报表和RDLC。还有报告服务解决方案,但是在这种情况下,您需要一个带有SQL server的服务器。

Crystal Reports seems to give you more choices, but needs a little more learning than RDLC.

Crystal报表似乎给了您更多的选择,但是比RDLC需要更多的学习。

I wouldn't recommend you create those in HTML + CSS, because of the limitations and the extra work you would have to throw at it.

我不建议您在HTML + CSS中创建它们,因为您必须对其进行限制和额外的工作。

#5


1  

If you can build your output as a FlowDocument, you can turn it into XPS easily to get an "electronic" version, and the print the XPS.

如果可以将输出构建为FlowDocument,那么可以轻松地将其转换为XPS,以获得“电子”版本,并打印XPS。

#6


0  

It depends a lot on the requirements of your application.

这在很大程度上取决于应用程序的需求。

Even though it isn't the perfect tool (really far from that), Crystal Reports tends to be a good choice. It gives you the option of getting data directly from a Database or, if you already have a list of objects you want to print, you can pass them to the document and bind the object properties to the labels of the report.

尽管它不是完美的工具(远远不是),Crystal report往往是一个不错的选择。它提供了直接从数据库获取数据的选项,或者,如果您已经有了想要打印的对象列表,您可以将它们传递到文档,并将对象属性绑定到报表的标签。

But give us some more information of what you're trying to do, so you can receive better proposals.

但请给我们更多的信息,你正在做什么,以便你能收到更好的建议。