您的位置:程序门 -> .net技术 -> c#



求救啊。如何用itextsharp将html转成pdf啊??好几天了。


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求救啊。如何用itextsharp将html转成pdf啊??好几天了。
发表于:2007-09-07 14:03:26 楼主
memorystream   m   =   new   memorystream();
                document   document   =   new   document();
                htmlworker   worker   =   new   htmlworker(document);
               
                system.net.webclient   mywebclient   =   new   system.net.webclient();
                mywebclient.credentials   =   system.net.credentialcache.defaultcredentials;
                byte[]   pagedata   =   mywebclient.downloaddata( "http://localhost/web/products/itemshow.aspx?espno= "+baserequest.getstring( "espno "));
                string   pagehtml   =   system.text.encoding.default.getstring(pagedata);
                textreader   filereader   =   new   stringreader(pagehtml);
                //response.write(pagehtml);
                //response.end();
               
                try   {
                        response.contenttype   =   "application/pdf ";
                        response.addheader( "content-disposition ",   "attachment;       filename= "+baserequest.getstring( "espno ")+ ".pdf ");
                        //   filereader   filereader   =   new   filereader( "chinese.html ");
                        pdfwriter.getinstance(document,   m);
                        //basefont   bf   =   basefont.createfont( "c:\\windows\\fonts\\arial.ttf ",   basefont.identity_h,   basefont.not_embedded);
                        //font   font1   =   new   font(bf,   12,   font.normal);
                        document.open();
                        stylesheet   styles   =   new   stylesheet();

                        styles.loadtagstyle( "td ",   "face ",   "gulim ");
                        styles.loadtagstyle( "td ",   "encoding ",   "identity-h ");
                        styles.loadtagstyle( "td ",   "leading ",   "12,0 ");

                        styles.loadtagstyle( "body ",   "face ",   "simhei ");
                        styles.loadtagstyle( "body ",   "encoding ",   "identity-h ");
                        styles.loadtagstyle( "body ",   "leading ",   "12,0 ");
                       
                        hashtable   hashpars   =   new   hashtable();
                        hashpars.add( "img_baseurl ",   "http://localhost/web/products/ ");
                        worker.style   =   styles;
                        worker.interfaceprops   =   hashpars;
                        worker.parse(filereader);
                }
                catch   (documentexception   ex)
                {
                        console.error.writeline(ex.stacktrace);
                        console.error.writeline(ex.message);
                }
                //       step       5:       close       document          
                document.close();
                //response.end();          


                //       //       step       6:       write       pdf       bytes       to       outputstream          
                response.outputstream.write(m.getbuffer(),   0,   m.getbuffer().length);
                response.outputstream.flush();
                response.outputstream.close();
发表于:2007-09-07 14:04:221楼 得分:0
我这段代码生成不了啊。。。怎么办啊。。。。

求救啊。。。
发表于:2007-09-07 17:07:132楼 得分:0
救命啊。高手啊。。。
发表于:2007-09-07 19:04:563楼 得分:0
帮顶
发表于:2007-09-07 20:59:084楼 得分:0
谢谢,救命
发表于:2007-09-07 21:16:365楼 得分:0
呵呵
i   coming
发表于:2007-09-07 21:19:106楼 得分:0
不晓滴。。up。。。。。
发表于:2007-09-07 21:20:457楼 得分:0
提供思路
读取源html文件
byte[]   downloadbytes   =   somemethond(url);获取源文件


                system.web.httpresponse   response   =   system.web.httpcontext.current.response;
                response.clear();
                response.addheader( "content-type ",   "binary/octet-stream ");
                response.addheader( "content-disposition ",
                        "attachment;   filename= "   +   downloadname   +   ";   size= "   +   downloadbytes.length.tostring());
                response.flush();
                response.binarywrite(downloadbytes);
                response.flush();
                response.end();
发表于:2007-09-08 08:30:368楼 得分:0
可以不
发表于:2007-09-08 11:55:129楼 得分:0
关注...
发表于:2007-09-08 13:14:2710楼 得分:0
关注一下
发表于:2007-09-08 14:19:3911楼 得分:0
偶也不晓滴
发表于:2007-09-08 17:57:5312楼 得分:0
不可以。郁闷。

发表于:2007-09-08 17:58:0513楼 得分:0
还有没有人晓得啊。
发表于:2007-09-08 18:31:3714楼 得分:0
先把html转成word.   然后处理.


using       websupergoo.abcpdf4;      
    using       websupergoo.abcpdf4.objects;      
    using       websupergoo.abcpdf4.atoms;      
       
    //这个是要生成pdf文档的源文件的地址      
    string       strurl       =       "http://localhost/pr/tempfiles/printdetail.aspx ";      
    string       strfolder       =       "d:\test_pdf\ ";      
    string       strpagename       =       "test.pdf ";      
       
    doc       doc       =       new       doc();      
    doc.rect.inset(72,144);      
    l_inttemp       =       doc.addimageurl(strurl,true,0,true);      
       
    while       (true)          
    {      
    doc.framerect();      
    if       (doc.getinfo(l_inttemp,       "truncated ")       !=       "1 ")      
    break;      
    doc.page       =       doc.addpage();      
    l_inttemp       =       doc.addimagetochain(l_inttemp);      
    }          
       
    for       (int       i       =       1;       i       <=       doc.pagecount;       i++)          
    {      
    doc.pagenumber       =       i;      
    doc.flatten();      
    }      
       
       
    doc.save(strfolder       +       strpagename);      
    doc.clear();
发表于:2007-09-08 19:51:1115楼 得分:0
不能直接解析html到pdf么?
itextsharp里不是有解析的东西么?
itextsharp.text.html.simpleparser;
这里面不是有htmlworker类么?
他里面有parse解析功能吧?
发表于:2007-09-09 10:50:0316楼 得分:0
顶啊。。。。
发表于:2007-09-09 16:23:3917楼 得分:0
顶啊。。。。顶啊。。。。


快速检索

最新资讯
热门点击