John Alexiou
John Alexiou

Reputation: 29244

What options do I have to produce a PDF report from code in .NET for scientific data (winforms)

I have a "legacy" VB.NET application (winforms) written for .NET 1.1, and re-compiled under 2.0 that produces a report in HTML via a custom XmlTextWriter wrapper that is suited for HTML. The user then would print the report into pdf if they wanted to.

That was 2003, and now technology has changed a bit, especially within the C#/VB.NET world, and customers want to skip the HTML part, and go to PDF directly. What are my options for open source, or low cost PDF libraries that work well with .NET and must support tables with pictures (generated bitmaps from code) and text.

Here is screen shot of the resulting html rendering

HTML Report

Obviously this needs some cleaning up, tidying it and stuff, but I am interested on known which technology to pursue in this project.

This related question might be what I need, or it may be out of date by now. I don't have any data sources that will provide all the information I want displayed. Currently it is collected from various classes within the application in order to be displayed as html.

anybody have direct experience with iTextSharp or SharpPDF ?

Thanks for any advice.

Update 1: found possible duplicate here.

Upvotes: 4

Views: 5232

Answers (7)

Joezer
Joezer

Reputation: 665

I just wrote a TIP in CodeProject on how to do this without using any external DLL in a couple of lines.

Here's the short code copied:

// ----------------------------------------------------------------------------------------------
// If you run this on Windows 10 (having it's default printer "Microsoft Print to PDF" installed)
// This should print a PDF file named "CreatedByCSharp.PDF" in your "MyDocuments" folder
// containing the string "When nothing goes right, go left"
// ----------------------------------------------------------------------------------------------

// If not present, you will need to add a reference to System.Drawing in your project References
using System.Drawing;
using System.Drawing.Printing;


void PrintPDF()
{
    // Set the output dir and file name
    string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    string file = "CreatedByCSharp.pdf";

    PrintDocument pDoc = new PrintDocument()
    {
        PrinterSettings = new PrinterSettings()
        {
             PrinterName = "Microsoft Print to PDF", 
             PrintToFile = true, 
             PrintFileName = System.IO.Path.Combine(directory, file),
        }
    };

    pDoc.PrintPage += new PrintPageEventHandler(Print_Page);
    pDoc.Print();
}

void Print_Page(object sender, PrintPageEventArgs e)
{    
    // Here you can play with the font style (and much much more, this is just an ultra-basic example)
    Font fnt = new Font("Courier New", 12);

    // Insert the desired text into the PDF file
    e.Graphics.DrawString("When nothing goes right, go left", fnt, System.Drawing.Brushes.Black, 0, 0);
}

Upvotes: 1

Dawid Kowalski
Dawid Kowalski

Reputation: 1247

I was researching this topic two months ago and basically you have two ways:

Dlls

  • open source iTextSharp - well, don't expect too much from it, it can generate PDFs from simple web pages, your table seems quite complicated though some my I don't think you will succeed with it without some tweaking of it's source code
  • paid options - I've used ABCPdf, works pretty smoothly, not everything is rendered as good as in browser but does it's job, I believe there are way more libraries like this

Command line tools

If you are lucky enough to have full control over server I think it will be your best option

I had not tried does two though, I used hosting so they were not an option to me

Upvotes: 1

Aaron
Aaron

Reputation: 1

I'd also suggest to take a look at PD4ML html to pdf converting library. It has quite a modest price for paid product, but it supports lots of features and is instantly updated.

Upvotes: 0

Doc Brown
Doc Brown

Reputation: 20044

Report.NET is a .NET PDF library specific for report generation supporting the features you asked for. It is smaller than iTextPdf, but perhaps sufficient for your needs:

http://sourceforge.net/projects/report/

(license is LGPL).

Upvotes: 1

codes_occasionally
codes_occasionally

Reputation: 9566

I have used iTextSharp to produce PDF reports before. Although you have to get used to the library (and it is an extensive library), once you get the hang of it it isn't so bad. I found the book iText In Action to be very helpful. Even though the book is about the original Java library, not the .NET port, most of the methods and classes are named the same so it wasn't really a problem.

My #1 piece of advice when working with iTextSharp is that you'll be writing a lot of the same code, over and over again. (i.e. creating a table cell, setting the fonts, sizes, colors, and borders for that table cell, setting text...). Do yourself a favor and make your own little Utility class that will do all of your gruntwork for you -- otherwise you'll end up with 2000 lines of code that just create a few tables with some special formatting.

In addition, this site has a series of brief articles that I found useful when I was first learning iTextSharp.

Edit:

If you're interested in an XHTML-->PDF converter, I just found this blog post by Darin Dimitriov that shows how to port the open-source Java flying-saucer library to .NET. He makes it look easy!

Interestingly enough, it seems that flying-saucer uses iText under the hood to perform the conversion.

Upvotes: 4

Laramie
Laramie

Reputation: 5587

I ended up using iTextSharp to produce image flashcards with some tricky formatting after becoming deeply frustrated with other libraries.

Where it really paid off was the competent documentation compared to the other options. I believe there is also an option to automatically parse HTML/XML.

Upvotes: 0

Steve Wellens
Steve Wellens

Reputation: 20620

You can use this free print driver:

http://www.dopdf.com/

When you print to it, it outputs PDF.

Upvotes: 1

Related Questions