当前位置:编程学习 > C#/ASP.NET >>

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

我看了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);
}
}
}
}
除了注释掉的行,就只有两行代码呀……
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,