iTextSharp HtmlParser为什么我html转换的pdf是0KB呢?

lovingness 2011-12-09 02:44:07
我看了iTextSharp 的示例代码,HTML转PDF,就两行代码呀,为什么我转出来的pdf打不开(报错:不支持文件类型或文件损坏),而且是0KB呢?下面是代码,对了,我是winform程序C#代码,直接把html文件转成pdf,不是把网页转成pdf。

private void button1_Click(object sender, EventArgs e)
{
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
try
{
PdfWriter.GetInstance(document, new FileStream("aaa.pdf", FileMode.Create));
HtmlParser.Parse(document, "aaa.htm");
MessageBox.Show("完成");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
结果也显示完成了,但就是pdf文件是0KB,打开的时候还报错,这是为什么呢?还请高手赐教!!!
我的QQ 450414688

下面是iTextSharp 里面第七章的示例代码:

using System;
using System.IO;
using System.util;

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using System.Collections;
using iTextSharp.text.html;

namespace iTextSharp.tutorial.Chap07
{
/// <summary>
/// Chap0706 的摘要说明。
/// </summary>
public class Chap0707
{
public Chap0707()
{
Console.WriteLine("Chapter 7 example 7: parsing the HTML from example 2");

// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 80, 50, 30, 65);

try
{

// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter.GetInstance(document, new FileStream("Chap0707.pdf", FileMode.Create));

// // step 3: we create a parser and set the document handler
// Parser parser = ParserFactory.makeParser(PARSER);
// parser.setDocumentHandler(new SAXmyHtmlHandler(document));
// step 3: we create a parser

// ITextHandler h = new ITextHandler(document);
HtmlParser.Parse(document,"Chap0702.htm");

// step 4: we parse the document _Document;
// h.Parse("Chap0702.htm");
// step 4: we parse the document
// parser.parse("Chap0702.html");

}
catch(Exception e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
}
}
}
}
除了注释掉的行,就只有两行代码呀……
...全文
263 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

111,078

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧