无法将ConvertHTMLToPDF.htm转换为PDF;在asp / c中使用itextsharp#

时间:2014-05-14 07:08:24

标签: html asp.net c#-4.0 itextsharp

我正在使用Itextsharp将html转换为pdf。我的html使用的是框架和CSS。主页面是ConvertHTMLToPDF.htm,它使用的是frame1.htm,frame2.htm和一个文件style.css。我正在使用Itextsharp将此html页面转换为pdf。虽然我能够将html转换为pdf,但它只将我的ConvertHTMLToPDF.htm转换为pdf。相关的frame1.htm,frame2.htm和style.css未嵌入pdf文件中。

   ////////////////File name: ConvertHTMLToPDF.htm
        <html>
        <head><title>Simple Web Page Using  2 Frames (Main Page)</title>
        </head>

        <frameset cols="20%, *">
           <frame src="frame1.htm" name="contents" noresize>
           <frame src="frame2.htm" name="pageinfo" noresize>

           <noframes>
               <body>
                       This web page is constructed using frames.
                       Your web browser does not support frames.
                       Seek, nonframes version.
               </body>
           </noframes>
        </frameset>

        </html>
        -----------------------------------------------------------------
        ////////////////File name: frame1.htm
        <html>
        <head><title>Left Pane (frame1.htm)</title>
        </head>
        <body bgcolor="#FFFFFF" text="#000000" link="#000000" vlink="#FF0000">

        <h3>Page Contents</h3>

        <br>
        <h5>
            Link 1<br><br>
            Link 2<br><br>
            Link 3<br><br>
            Link 4<br><br>
        </h5>

        </body>
        </html>
        -----------------------------------------------------------------
        ////////////////File name: frame2.htm
        <html>
        <head><title>Right Pane (frame2.htm)</title>
        </head>
        <body bgcolor="#FFFFFF" text="#000000" link="#000000" vlink="#FF0000">

        <h3 align="center">Information Page</h3>

        <br>
        <h5>Welcome. Here you can find....</h5>

        </body>
        </html>
        /////////////////////////File name: frame1.htm
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title></title>
        </head>
        <body>
        <H1>Hi this is ANIKET !!!<H1>
        <H2>This example is for converting pdf to word </H2>

        </body>
        </html>
    /////////////////file name: style.css

p{ line-height: 1em; }
    h1, h2, h3, h4{
        color: orange;
        font-weight: normal;
        line-height: 1.1em;
        margin: 0 0 .5em 0;
    }
    h1{ font-size: 1.7em; }
    h2{ font-size: 1.5em; }
    a{
        color: black;
        text-decoration: none;
    }
        a:hover,
        a:active{ text-decoration: underline; }

    /* you can structure your code's white space so that it is as readable for when you come back in the future or for other people to read and edit quickly */

    body{
        font-family: arial; font-size: 80%; line-height: 1.2em; width: 100%; margin: 0; background: #eee;
    }
    /* you can put your code all in one line like above */
    #page{ margin: 20px; }

    /* or on different lines like below */
    #logo{
        width: 35%;
        margin-top: 5px;
        font-family: georgia;
        display: inline-block;
    }
    /* but try and be as concise as possible */
    #nav{
        width: 60%;
        display: inline-block;
        text-align: right;
        float: right;
    }
        #nav ul{}
            #nav ul li{
                display: inline-block;
                height: 62px;
            }
                #nav ul li a{
                    padding: 20px;
                    background: orange;
                    color: white;
                }
                #nav ul li a:hover{
                    background-color: #ffb424;
                    box-shadow: 0px 1px 1px #666;
                }
                #nav ul li a:active{ background-color: #ff8f00; }

    #content{
        margin: 30px 0;
        background: white;
        padding: 20px;
        clear: both;
    }
    #footer{
        border-bottom: 1px #ccc solid;
        margin-bottom: 10px;
    }
        #footer p{
            text-align: right;
            text-transform: uppercase;
            font-size: 80%;
            color: grey;
        }

    /* multiple styles seperated by a , */
    #content,
    ul li a{ box-shadow: 0px 1px 1px #999; 
}

1 个答案:

答案 0 :(得分:0)

///这是我将html转换为pdf的代码,其中ltrlReeport是包含html的文字。

 public void CreatePDFFromHTMLFile()
        {
            try
            {

                string a = WebUtility.HtmlEncode(ltrlReeport.Text);
                a = a.Replace("\r\n", "");
                a = a.Replace("\0", "");
                StreamReader strmRdr = new StreamReader(a);

                Document doc = new Document(iTextSharp.text.PageSize.A4);   

                PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("F:\\aTestFile.pdf", FileMode.Create));

                doc.Open();

                HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
                htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

                ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);

                IPipeline pipeline = new CssResolverPipeline(cssResolver, new  HtmlPipeline(htmlContext, new PdfWriterPipeline(doc, wri)));

                XMLWorker worker = new XMLWorker(pipeline, true);
                XMLParser p = new XMLParser(worker);

                TextReader tr = new StringReader(ltrlReeport.Text);
                p.Parse(tr);
                doc.Add(new Paragraph("hello"));
                doc.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }