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

1

2 Answers 2

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#.

8
  • 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 5, 2014 at 6: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 5, 2014 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? Nov 5, 2014 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 <input type="xxxxxx"... .../> 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 5, 2014 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. Nov 5, 2014 at 13:54
-1

You need to call writer.close().

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.