问题
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:
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:
- iText partial HTML rendering
- how can I add cellspacing to pdftable when parsing html using XMLWorker and itext
- RowSpan does not work in iTextSharp?
- ...
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#.
回答2:
You need to call writer.close()
.
来源:https://stackoverflow.com/questions/26738289/itextsharp-write-html-to-pdf-document