日期:2014-05-18  浏览次数:20957 次

如何保留格式生成pdf
问题是这样的 我准备了一个网页,网页上有一个编辑器,这个编辑器中插入图片文字连接都是以html的形式调整的格式,这个编辑器在一个form中,之后有一个按钮submit,提交后,后台得到提交的结果html的 例如:
HTML code
<div class="texte-libre-rose" style="text-align: right; margin-right: -25px;">
    <h1 style="color: rgb(34, 34, 34); text-transform: none; line-height: normal; text-indent: 0px; letter-spacing: normal; font-family: Arial, Verdana, sans-serif; font-style: normal; font-variant: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">
        <img alt="" src="http://a.cksource.com/c/1/inc/img/demo-little-red.jpg" style="width: 120px; height: 168px; margin-right: 10px; margin-left: 10px; float: left; cursor: default;" />Little Red Riding Hood</h1>
    <table align="right" border="1" cellpadding="1" cellspacing="1" style="font: 12px/normal Arial, Verdana, sans-serif; width: 200px; color: rgb(34, 34, 34); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; font-size-adjust: none; font-stretch: normal; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">
        <caption>
            <strong>International Names</strong></caption>
        <tbody>
            <tr>
                <td>
                    Chinese</td>
                <td>
                    <i>小紅帽</i></td>
            </tr>
            <tr>
                <td>
                    Italian</td>
                <td>
                    <i>Cappuccetto Rosso</i></td>
            </tr>
            <tr>
                <td>
                    Spanish</td>
                <td>
                    <i>Caperucita Roja</i></td>
            </tr>
        </tbody>
    </table>
    <p style="font: 12px/normal Arial, Verdana, sans-serif; color: rgb(34, 34, 34); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; font-size-adjust: none; font-stretch: normal; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">
        &nbsp;</p>
</div>
<p>
    &nbsp;</p>


这段html中有一个图片,有一个表格,图片在左边,表格在右边,标题在右边,现在点击按钮生成pdf,后台代码:
C# code
public static void Generate(string htmlText)
        {


            Document pdfDocument = new Document(PageSize.A4, 50, 25, 15, 10);
            PdfWriter wri = PdfWriter.GetInstance(pdfDocument, new FileStream("test.pdf", FileMode.Create));
            //wri.PageEvent = new PageEvent("");
            pdfDocument.Open();
            HTMLWorker htmlWorker = new HTMLWorker(pdfDocument);
            TextReader htmlReader = new StringReader(htmlText);
            htmlWorker.Parse(htmlReader);
            pdfDocument.Close();
            
            System.Diagnostics.Process.Start("test.pdf");
        }


但生成的pdf的格式跟之前编辑的格式不一样了,图片按原始的像素值显示,标题居中了,表格也不在右边,占了一整行:


不知道大家有什么方法保持原有的html格式生成pdf,或者我的这个生成pdf的代码有问题????

------解决方案--------------------
http://ufo-crackerx.blog.163.com/blog/static/11307877820121228810807/

http://ufo-crackerx.blog.163.com/blog/static/11307877820122178619921/

看看這些控件,或許能滿足你
------解决方案--------------------
找开源的就够用了吧
------解