转换HTML文件使用iTextSharp的PDF文件 [英] Convert HTML file to PDF file using ITextSharp

查看:359
本文介绍了转换HTML文件使用iTextSharp的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下目标:

由于HTML文件的路径名,和PDF文件的路径所需的HTML文件转换为PDF使用iTextSharp的。我见过很多代码样本里面做接近这一点,但不是我所需要的东西的。我相信我的解决方案将需要使用iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList()函数,但我遇到了麻烦这与实际的HTML文件工作,并输出一个实际的PDF文件。

Given the path name of an html file, and the desired pathname of a pdf file, convert the HTML file to PDF using ITextSharp. I've seen plenty of code samples which do close to this but not exactly what I need. I believe my solution will need to use the iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList() function but I'm having trouble getting this to work with an actual HTML file and outputting an actual PDF file.

public void GeneratePDF(string htmlFileName, string outputPDFFileName)
{...}

是我真的很想得到正常工作的功能。

is the function I'd really like to get working properly.

在此先感谢

修改:下面是我的什么我已经试过一个例子:

Edit: Here's an example I've of what I've tried:

iTextSharp.text.Document doc = new Document();
        PdfWriter.GetInstance(doc, new FileStream(Path.GetFullPath("fromHTML.pdf"), FileMode.Create));

        doc.Open();

        try
        {
            List<IElement> list = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(File.ReadAllText(this.textBox1.Text)), null);
            foreach (IElement elm in list)
            {
                doc.Add(elm);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        doc.Close();



注意textBox1.Text包含HTML文件我想转换成的全路径名PDF格式,我想这让输出到fromHTML.pdf

Note that textBox1.Text contains the full path name of the html file I'm trying to convert to pdf and I want this to get output to "fromHTML.pdf"

谢谢!

推荐答案

我有同样的要求,被转移到该页面由谷歌,但找不到一个具体的答案。
但是一些头的打击和考验之后,我已经能够HTML代码转换成功使用iTextSharp的库5.1.1 PDF。
我已经在这里共享的代码也需要照顾的img标签的HTML中的相对路径。如果你的img标签没有绝对的SRC iTextSharp的库抛出一个错误。
你是在这里找到的代码:
http://am22tech.com/s/22/Blogs/post/2011/09/28/HTML-To-PDF-using-iTextSharp.aspx

I had the same requirement and was diverted to this page by Google but could not find a concrete answer. But after some head hitting and trials, i have been able to successfully convert the HTML code to PDF using iTextSharp library 5.1.1. The code that i have shared here also takes care of the img tags in HTML with relative paths. iTextSharp library throws an error if your img tags do not have absolute src. You an find the code here: http://am22tech.com/s/22/Blogs/post/2011/09/28/HTML-To-PDF-using-iTextSharp.aspx

让我知道如果你需要更多的信息。该代码是用C#。

Let me know if you need more information. The code is in c#.

这篇关于转换HTML文件使用iTextSharp的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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