C # an easy way to print formatted data

I am looking for an easy way to print a DIN-A4 document with data from a database on it. Data should be filled in several tables with borders. Some data must have a different text format pe bold or underlined. I should also be able to print multiple images on this sheet.

The program must be a Windows Forms application or a console application written in C #.

What is the easiest / most common way to format data and print it out?

Any suggestions :)

EDIT:

this is my current code with little success. It does print, but what I get is just an 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; } } 
+6
source share
2 answers

Printing in .NET is quite complicated without additional tools.

  • Visual Studio 2008 and Report Wizard

    Its my favorite tool. It is based on Microsoft Reporting Services, which is available in Visual Studio 2008. It works similarly to the MS Access reporting features.

    Unfortunately, I was not able to run it and create reports in other versions of Visual Studio. Reporting Services is available in the .NET Framework, and you can use it with any Visual Studio, but other versions than 2008 do not have a tool for creating a report designer / wizard).

  • Crystal Reports (expensive but really good).

  • If you have time, you can deal with these pixels:

    Simplified .NET printing in C # Dave Brighton at codeproject.com

  • WebBrowser management, as @colosso wrote in a different answer, but it depends on the version of Internet Explorer, and I personally donโ€™t like this method.

+2
source

I found two possibilities:

1) In my opinion, this is worse. I found a third-party software called "Antena house formatter". You can find it at www.antennahouse.com , but unfortunately neither open source nor free software. This software allows you to convert xml, xsl or xsl-fo data to PDF and other formats. From there you can print it with standard C #. I did not choose this path for some reason: to be connected to third-party software, in my opinion, is not a good solution, although this Antennahouse formatter is pretty nice, reliable and fast software.

2) This is the solution that I have chosen. You can create a simple WebBrowser control and fill it with either a saved html file, or simply fill it with a dynamically created line. Now I am creating a line containing the entire html document to load it into the Webbrowser control:

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

When loading a 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 being printed, or just hide it. Finally, when you have uploaded your html file to the control, you can print it using:

 webBrowser1.Print(); 

It will print the document with the default printer. I know using an html file to print such a site that feels something like โ€œhacked,โ€ but this is the easiest way to find something like that. Especially if you can print very complex Sites with many other things on it.

Nice to know:

  • I had to print DIN-A4 sites. I set my content to 670 pixels and it works.
  • You print using the Webbrowser control. It will take the printer settings from the locally installed Internet Explorer (or registry, if we want to be exact). If you want to change it, go to your IE> Print> Page Settings ... and set the necessary parameters or just change it in your registry.

Hope this helps someone :)

+1
source

All Articles