简体   繁体   中英

iTextSharp - Write HTML to pdf document

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

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

您需要调用writer.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM