0

I've tried my damnedest to get this to work, but all info on the web regarding iTextSharp seems to be out dated. (I'm using version 5.5.3)

I have a simple string variable containing the HTML that I want rendered to a PDF.

There are a few examples using XMLWorker / XMLWorkerHelper, but these classes no longer seem to be part of iTextSharp. HTMLWorker is still available, but the moans about being obsolete. (And also doesn't work)

This is what I have so far:

public byte[] RenderPdf() 
{
   MemoryStream file = new MemoryStream();
   Document document = new Document(this.PageSize);
   PdfWriter writer = PdfWriter.GetInstance(document, file);

   string HTML = GetHTMLFromActionResult();

   document.Open();

   //Missing code here to inject HTML into document variable.

   document.Close();
   return file.ToArray();
}

Extra info:
This is a C# MVC5 Web Application running on .Net Framework 4.5.2

Swifty
  • 1,422
  • 2
  • 18
  • 38
  • Whilst not a solution to this problem when I was working with this library I found the following book extremely useful - http://www.amazon.co.uk/iText-Action-Creating-Manipulating-PDF/dp/1932394796. It's for the JAVA version but most of the code samples are very easily ported. – Jon Malcolm Nov 04 '14 at 15:32

2 Answers2

1

I see two separate questions in your post:

(1) Where is XML Worker for iTextSharp?

If you want to use XML Worker, you need an extra DLL. I'm not sure where you usually get iTextSharp, but if you go to SourceForge, you can clearly see both projects: http://sourceforge.net/projects/itextsharp/files/

Start by downloading itextsharp as well as xmlworker and make sure the version numbers correspond.

(2) Where can I find the documentation for XML Worker?

The last time I updated the examples was as long ago as last weekend, so it's not fair to say that they are outdated. Maybe you didn't search the official web site: http://itextpdf.com/sandbox/xmlworker

I recently added some examples in answer to the following questions:

Sure, the examples are iText examples in Java, but if you browse the examples, you'll find a link to the corresponding question on StackOverflow and many of those questions are about iTextSharp in Java. It's fairly easy to port a Java example to C#.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    I'm honored to have you reply to my query Bruno! I got iTextSharp of Nuget, I assumed that it would pull down everything I needed. I did not pull down the XMLWorker. Thanks for your response, I was not aware that it was a separate assembly. I got the worker off Nuget too now. I should be able to figure rest from here. – Swifty Nov 05 '14 at 06:23
  • Hi Bruno. The XMLWorker object complains about there being more than one head tag. I'm assuming this is because I am passing a full HTML page to the parser. Is there a way to not have the document object include its own html/head/body tags? – Swifty Nov 05 '14 at 13:29
  • 1
    The document doesn't have its own html/head/body tags. It just looks at the HTML you are passing to the parsers. Does that HTML have two head tags? – Bruno Lowagie Nov 05 '14 at 13:35
  • There was a malformed meta tag which made the parser think that there was something wrong with the head tag. The PDF now gets generated, but none of my controls are being rendered, only labels. It seems that iText is ignoring all tags. Note: I'm using bootstrap 3.0, I have not yet figured out how to include the CSS, not sure if that is part of the cause. – Swifty Nov 05 '14 at 13:51
  • Indeed: forms in PDF are technically very different from forms in HTML (for instance: in PDF, you can only have one `form` tag; in HTML you can have several), hence XML Worker does not support them. – Bruno Lowagie Nov 05 '14 at 13:54
  • I don't have any form tags in my HTML. – Swifty Nov 05 '14 at 14:11
  • Is there any way import bootstrap css (less) into the PDF? – Swifty Nov 06 '14 at 12:21
  • I have no idea what you mean when you write *bootstrap css (less)*. – Bruno Lowagie Nov 06 '14 at 12:32
-1

You need to call writer.close().

Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81