简体   繁体   English

c#打印格式化数据的简便方法

[英]c# easy way to print formatted data

I'm looking for a simple way to print DIN-A4 paper with data from a database on it. 我正在寻找一种简单的方法来打印DIN-A4纸,上面有数据库中的数据。 The data should be filled into multiple tables with borders. 应将数据填充到带边框的多个表中。 Some of the data should have a different text format pe bold or underlined. 某些数据应具有不同的文本格式,如粗体或带下划线。 I should also be able to print multiple images onto that sheet. 我还应该能够在该表上打印多个图像。

The program should be a Windows Forms Application or a console application written in C#. 该程序应该是Windows窗体应用程序或用C#编写的控制台应用程序。

Which is the easiest / most common way to format data and print it like that? 哪种格式化数据并将其打印出来的最简单/最常用的方法是什么?

Any suggestions apreciated :) 任何建议apreciated :)

EDIT: 编辑:

this is my current code with almost no success. 这是我目前的代码几乎没有成功。 It actually prints but what I get is simply the xml file printed. 它实际上打印但我得到的只是打印的xml文件。

        private void printButton_Click(object sender, EventArgs e)
    {

        string printPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        fileToPrint = new System.IO.StreamReader(printPath + @"\test.xml");
        printFont = new System.Drawing.Font("Arial", 10);

        PrintDocument printDocument1 = new PrintDocument();
        printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
        printDocument1.Print();
        fileToPrint.Close();
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        float yPos = 0f;
        int count = 0;
        float leftMargin = e.MarginBounds.Left;
        float topMargin = e.MarginBounds.Top;
        string line = null;
        float linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
        while (count < linesPerPage)
        {
            line = fileToPrint.ReadLine();
            if (line == null)
            {
                break;
            }
            yPos = topMargin + count * printFont.GetHeight(e.Graphics);
            e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
            count++;
        }
        if (line != null)
        {
            e.HasMorePages = true;
        }
    }

Printing in .NET is quite hard without additional tools. 没有其他工具,在.NET中打印非常困难。

  1. Visual Studio 2008 and Report Wizard Visual Studio 2008和报表向导

    Its my favorite tool. 它是我最喜欢的工具。 It's based on Microsoft Reporting Services which is available in Visual Studio 2008. It works similar to MS Access reports functionality. 它基于Microsoft Reporting Services,可在Visual Studio 2008中使用。它的工作方式类似于MS Access报告功能。

    Unfortunatelly i was not able to run it and design reports in other Visual Studio versions. 不幸的是,我无法在其他Visual Studio版本中运行它并设计报告。 Reporting Services are available in .NET Framework and you can use them with any Visual Studio, but there is no Report Designer/Wizard tool in versions other than 2008). Reporting Services在.NET Framework中可用,您可以将它们与任何Visual Studio一起使用,但在2008以外的版本中没有报表设计器/向导工具。

  2. Crystal Reports (expensive but really good). 水晶报告(昂贵,但非常好)。

  3. If you have some time you may fight with pixels like this: 如果你有一些时间,你可能会像这样的像素战斗:

    Simplified .NET printing in C# Dave Brighton at codeproject.com 在codeproject.com上使用C#Dave Brighton简化.NET打印

  4. WebBrowser control, as @colosso wrote in another answer, but this depends on Internet Explorer version and i personally don't like this method. WebBrowser控件,正如@colosso写的另一个答案,但这取决于Internet Explorer版本,我个人不喜欢这种方法。

I found two possibilities: 我找到了两种可能性:

1) In my opinion this is the worse one. 1)在我看来,这是更糟糕的一个。 I found a third-party software called "Antena house formatter". 我找到了一个名为“Antena house formatter”的第三方软件。 You can find it on www.antennahouse.com but it's unfortunately neither open source nor free software. 您可以在www.antennahouse.com上找到它,但遗憾的是它既不是开源也不是免费软件。 This software allows you to convert xml, xsl or xsl-fo data into pdf and other formats. 该软件允许您将xml,xsl或xsl-fo数据转换为pdf和其他格式。 From there you could print it with standard c#. 从那里你可以用标准的c#打印它。 I didn't chose this way for some reasons: To be hooked on a third-party software is no good solution in my opinion, although this Antennahouse formatter is a quite nice, reliable and fast software. 我没有选择这种方式出于某些原因:在我看来,迷上第三方软件并不是一个好的解决方案,尽管这个Antennahouse格式化程序是一个非常好,可靠和快速的软件。

2) This is the solution I have chosen. 2)这是我选择的解决方案。 You can create a simple WebBrowser control and fill it either with a saved html file or you can just fill it with a dynamically created string. 您可以创建一个简单的WebBrowser控件,并使用保存的html文件填充它,或者您可以使用动态创建的字符串填充它。 I now generate a string witch contains the whole html document an load it into the Webbrowser control: 我现在生成一个包含整个html文档的字符串,并将其加载到Webbrowser控件中:

webBrowser1.Document.OpenNew(true);
string strHtml = "<html><head></head><body></body></html>";
webBrowser1.Document.Write(strHtml);

When loading the form I open a new "tab" with: 加载表单时,我打开一个新的“选项卡”:

webBrowser1.Navigate("about:blank");

You can either show the Webbrowser control to have a "preview" of the site that is getting printed or just hide it. 您可以显示Webbrowser控件,以便对要打印的网站进行“预览”,或者只是隐藏它。 Finally, when you loaded you html file into the control you can print it with: 最后,当您将html文件加载到控件中时,您可以使用以下命令打印它:

webBrowser1.Print();

It will ow print the document with you default printer. 它将使用您的默认打印机打印文档。 I know, using a html file to print a site like that feels some sort of "hacky" but it's the easiest way I found to do something like that. 我知道,使用html文件打印这样的网站感觉某种“hacky”,但这是我发现做这样的事情最简单的方法。 Especially if you net to print very complex Sites with many different things on it. 特别是如果你打印非常复杂的网站上有很多不同的东西。

Nice to know: 很高兴知道:

  • I had to print DIN-A4 sites. 我不得不打印DIN-A4网站。 I set my content with to 670px and that fits nice. 我将内容设置为670px,这很合适。
  • You are printing with a Webbrowser control. 您正在使用Webbrowser控件进行打印。 He will take the printer settings from your local installed Internet Explorer (or the registry if we want to be exactly). 他将从您当地安装的Internet Explorer(或注册表中获取打印机设置,如果我们想要完全)。 If you want to change it go into your IE > Printing > Page settings... and set you desired options or just change it in your registry. 如果要更改它,请进入IE>打印>页面设置...并设置所需的选项或只需在注册表中进行更改。

Hope this helps someone :) 希望这有助于某人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM