6

I'm looking for a simple way to print DIN-A4 paper with data from a database on it. The data should be filled into multiple tables with borders. Some of the data should have a different text format p.e. 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#.

Which is the easiest / most common way to format data and print it like that?

Any suggestions apreciated :)

EDIT:

this is my current code with almost no success. It actually prints but what I get is simply the xml file printed.

        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;
        }
    }
3
  • So, what have you tired so far? Commented Nov 12, 2012 at 8:32
  • so, for paper I suggest to use something alike html (here is good example codeproject.com/Articles/32376/…) but seriously you need to explain (draw) what you want to get, I can't provide better answer without this information. Commented Nov 12, 2012 at 8:33
  • I'd like to have something like this on my paper: jsfiddle.net/35uCK Commented Nov 12, 2012 at 8:53

2 Answers 2

2

Printing in .NET is quite hard without additional tools.

  1. Visual Studio 2008 and Report Wizard

    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.

    Unfortunatelly i was not able to run it and design reports in other Visual Studio versions. 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).

  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

  4. WebBrowser control, as @colosso wrote in another answer, but this depends on Internet Explorer version and i personally don't like this method.

Sign up to request clarification or add additional context in comments.

Comments

1

I found two possibilities:

1) In my opinion this is the worse one. I found a third-party software called "Antena house formatter". You can find it on www.antennahouse.com but it's unfortunately neither open source nor free software. This software allows you to convert xml, xsl or xsl-fo data into pdf and other formats. From there you could print it with standard 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.

2) This is the solution I have chosen. 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. I now generate a string witch contains the whole html document an load it into the Webbrowser control:

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. Finally, when you loaded you html file into the control you can print it with:

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. 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. I set my content with to 670px and that fits nice.
  • You are printing with a Webbrowser control. He will take the printer settings from your local installed Internet Explorer (or the registry if we want to be exactly). 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.

Hope this helps someone :)

1 Comment

Worked further on it: I'm now using Telerik reporting. It's very easy to use, many ways to bind data and all kind of formating is possible! Unfortunately its not free but it's worth it. For further informations visit telerik.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.