views:

3231

answers:

16

Does anyone have experience using the following libraries (or others) to build PDF files in C#? Which have you found to be the easiest and most straightforward? What I would like to do is push some HTML into a library and have it spit out a PDF. Any help would be appreciated.

+16  A: 

We've used iTextSharp and found it very user-friendly and super-fast.

John M Gant
I'm working with iTextSharp now in web pages, works great! +1
SomeMiscGuy
Another vote of iText, it's a powerful library
Steve Claridge
+2  A: 

If you are translating HTML to PDF, I would look into using Apache FOP, especially if its well-formed HTML (XHTML). You could use a XSLT to transform the HTML to XSL-FO and then use Apache FOP to generate the PDF. I generate a PDF from XML this way, and it's pretty easy once you know XSLT. Also, if you are generating the HTML from an XML, you could just perform the XSLT on that XML

I've heard good things about TallPDF from a former boss. He swore by it.

phsr
To avoid confusion: the link is for Apache FOP, not Adobe FOP.
DOK
My bad, I should have realized that, since I've used it for so long
phsr
A: 

Dynamic PDF really worked well for the reports we built.

CodeToGlory
+3  A: 

I have used and enjoyed iTextSharp. It worked well for the more complicated requirements. However, I would like to add that the VisualStudio Report Designer comes with a PDF generator. So if all you need is to pipe out a report to a PDF then I think this will meet most of your needs.

northpole
+2  A: 

I could recommend ABCpdf. Good options, easy to understand and a good manual. I think it's fairly cheap too.

sshow
A: 

I have used itextsharp before and it was pretty good

If you would like i can dig up an example or two but they have very good info online.

Crash893
+3  A: 

I have used DynamicPDF and even when I was a newbie I found it easy to use. Here is one case where I generate a 1000 page report sent to a sponsor combining multiple PDF files into one large one (That's how they want it, that's how they get it)...
(Superfluous items deleted...)

// Create output file

ceTe.DynamicPDF.Merger.MergeDocument docCombinedPDF = new ceTe.DynamicPDF.Merger.MergeDocument();
docCombinedPDF.Append(strFilePath);

//Read all data from the content table and put in a dataset

int iDocumentCount = dsPDFInfo.Tables[0].Rows.Count;
if (iDocumentCount > 0)
{
 for (int docs = 0; docs < dsPDFInfo.Tables[0].Rows.Count; docs++)
 {
  byte[] bytePDFArray = (byte[])dsPDFInfo.Tables[0].Rows[docs]["Content"];
  int iContentSize = Convert.ToInt32(dsPDFInfo.Tables[0].Rows[docs]["ContentSize"]);

  MemoryStream ms = new MemoryStream(bytePDFArray, 0, iContentSize);
  ceTe.DynamicPDF.Merger.PdfDocument pdfdoc = new ceTe.DynamicPDF.Merger.PdfDocument(ms);
  ceTe.DynamicPDF.Merger.MergeDocument mergedoc = new ceTe.DynamicPDF.Merger.MergeDocument(pdfdoc);

  docCombinedPDF.Append(mergedoc);
 }   

 // Insert Page Number
 int iPageNumber = 1;
 int iPageCount = docCombinedPDF.Pages.Count;
 float fPageNumberWidth = 150;
 float fPageNumberHeight = 15;
 float fPageNumberFontSize = 12;
 while (iPageNumber <= iPageCount)
 {
  ceTe.DynamicPDF.Page page = docCombinedPDF.Pages[(iPageNumber-1)];

  // All Page dimensions can be accessed therefore determine location dynamically
  float fPageNumberX = page.Dimensions.Width - (fPageNumberWidth + 20);
  float fPageNumberY = page.Dimensions.Height - (fPageNumberHeight + 20);

  page.Elements.Add(new PageNumberingLabel("Page %%CP%% of %%TP%%",
            fPageNumberX,
            fPageNumberY,
            fPageNumberWidth,
            fPageNumberHeight,
            ceTe.DynamicPDF.Font.TimesRoman,
            fPageNumberFontSize,
            ceTe.DynamicPDF.TextAlign.Right
            )
  );

  iPageNumber++;
 }

 // Write combined doc to web
 docCombinedPDF.InitialPageZoom = PageZoom.FitWidth; 
 docCombinedPDF.DrawToWeb(this.Page, false, "InvoiceProjectReport", false);

 Response.End();
}
else
{
labelMessage.Text = "There are no entries for the Project/Date Selection";
}
Dining Philanderer
A: 

I noticed you added ExpertPDF to the list. We used that as well and found it quick and easy for small file sizes, but it scaled terribly once we got over a few dozen pages. We were also unimpressed with their customer service response when we we reported the scalability problem.

John M Gant
+1  A: 

Another vote for iTextSharp. I used it to pull out a page from a huge PDF and stream it out to a web-browser (oStream)

PdfReader oPDFReader = new PdfReader(sourcePdf);
Document oPDFDoc = new Document(oPDFReader.GetPageSizeWithRotation(1));
PdfCopy oPDFCopy = new PdfCopy(oPDFDoc, oStream);
oPDFDoc.Open();
PdfImportedPage oPDFPage = oPDFCopy.GetImportedPage(oPDFReader, pageNumberToExtract);
oPDFCopy.AddPage(oPDFPage);
oPDFDoc.Close();
oPDFReader.Close();

Job done

Gordon Carpenter-Thompson
+2  A: 

Creating a PDF and using HTML as the content is pretty easy to do with Quick PDF Library and its DrawHTMLText function. This C# example uses the ActiveX edition of the library.

// Setup the parameters
string fileName = "C:\\example.pdf";
string licenseKey = "...";

// Create the library
QuickPDFAX0712.PDFLibrary qp = new QuickPDFAX0712.PDFLibrary();

// Unlock the library
qp.UnlockKey(licenseKey);
qp.DrawHTMLText(40, 760, 200, "Here is some <b>bold</b>, <i>italic</i> and <u>underlined</u> text");
qp.DrawHTMLText(40, 730, 200, "<ul><li>This</li><li>is</li><li>a</li><li>bullet</li><li>list</li></ul>");

// Save the new PDF that you've created.
qp.SaveToFile(fileName);
Rowan
A: 

I have used PDFNet SDK for manipulating PDF documents. It is probably not the best tool for reports generation, but to get back to editable content from PDF, I haven't yet found anything that works better.

IanGilham
+3  A: 

I've done well with ABCpdf. Their image components (ImageGlue) are helpful too.

Matt Sherman
A: 

I am facing a similar situation at work. However, I am also looking towards providing other formats (word, excel). For this, I am looking at OpenOffice, as it can arbitrarily target many formats. Plus it's open source (I tend to take this into account).

You can provide it with a HTML document (in a zip), and send a server instance instructions to convert etc. There are also bindings for .net, but this appears to be very heavyweight on code. Also you can send it ODF, if this is an option, to more accurately control the look.

A: 

I used PDFSharp / MigraDoc on several occasions, and was pretty happy with it. I needed to generate various charts and reports, and was quite satisfied with it.

It does need some getting used to though, however when I got stuck I always received answers on their official forms.

All in all it is a good library and I feel comfortable recommending it!

Rekreativc
+1  A: 

ABCpdf offers a few methods for working with HTML...

  • AddHtml - for adding a block of HTML styled text.
  • AddImageHtml - renders a page specified as HTML.
  • AddImageUrl - renders a web page specified by a URL.

It's a powerful library but simple to use. The task you described could possibly be achieved with a minimal example like the one below:

Doc theDoc = new Doc();
theDoc.AddImageHtml("<html>... your HTML code here ...</html>");
theDoc.Save("output.pdf");
theDoc.Clear();

There's also an HtmlOptions object which provides sophisticated control over the way AddImageHtml and AddImageUrl work, documented here on the webSupergoo website.

AffineMesh94464
A: 

Since you are a .NET developer I suggest using standard .NET (WPF) API to generate dynamic Flow documents (which as based on XAML). This is similar to HTML but is more powerful. For more info you can search for "FlowDocument" in MSDN.

You can then serailize Flow documents or XAML to PDF using PDFTron's PDFNet SDK (or XPS2PDF). See Xaml2PDF sample in PDFNet SDK: http://www.pdftron.com/pdfnet/samplecode.html#XAML2PDF

Lary