iTextSharp - 将HTML写入pdf文档 [英] iTextSharp - Write HTML to pdf document

查看:182
本文介绍了iTextSharp - 将HTML写入pdf文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过最大的努力让这个工作,但网上关于iTextSharp的所有信息似乎都过时了。 (我正在使用版本5.5.3)

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)

我有一个简单的字符串变量,其中包含我想要呈现为PDF的HTML。

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

有一些使用XMLWorker / XMLWorkerHelper的例子,但这些类似乎不再是iTextSharp的一部分。 HTMLWorker仍然可用,但是关于过时的呻吟声。 (并且也不起作用)

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)

这是我到目前为止:

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();
}

额外信息:

这是一个C#MVC5在.Net Framework 4.5.2上运行的Web应用程序

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)iTextSharp的XML Worker在哪里?

如果你想使用XML Worker,你需要一个额外的DLL。我不确定你通常会在哪里获得iTextSharp,但如果你去SourceForge,你可以清楚地看到这两个项目: http://sourceforge.net/projects/itextsharp/files/

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/

首先下载 itextsharp 以及 xmlworker 并确保版本号对应。

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

(2)我在哪里可以找到XML Worker的文档?

上次更新示例的时间早在上周末,所以说它们已经过时是不公平的。也许您没有搜索官方网站: http://itextpdf.com/sandbox/xmlworker

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?
  • ...

当然,示例是Java中的iText示例,但是如果您浏览示例,您将在StackOverflow上找到相应问题的链接以及其中许多问题ns是关于Java中的iTextSharp。将Java示例移植到C#相当容易。

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

这篇关于iTextSharp - 将HTML写入pdf文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆