找了无数资料,发现iTextSharp将 现有的Html文件 转换成 PDF 后样子完全变了,很丑其中就这个方法 iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList()是来解析Html但是解析后的呈现的结果很难看我现在想知道的是 iTextSharp 是不是 不能直接把 html 转换成 pdf?????? 尤其是那种有表格 有图片的html。。那这肯定不能自己写代码往pdf加,因为html风格都不一样问题:我现在想知道的是 iTextSharp 是不是 不能直接把 html 转换成 pdf?????? 麻烦了。

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Text;
    using iTextSharp.text;
    using iTextSharp.text.pdf;namespace Pbreak.PDf{    class MyPdf
        {        #region Fields        /// <summary>
            /// pdf文档
            /// </summary>
            private Document _pdfDocument;        /// <summary>
            /// 基本字体
            /// </summary>
            private BaseFont _bfSun =
                BaseFont.CreateFont(@"c:\Windows\fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);        #endregion        #region Constructors        /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="fileName"></param>
            public MyPdf(string fileName,ListView lvi)
            {
                Font font = new Font(_bfSun,21,1);
                _pdfDocument = new Document(PageSize.A4,10,10,25,25);
                PdfWriter writer = PdfWriter.GetInstance(_pdfDocument,new FileStream(fileName,FileMode.Create));            //添加页脚
                HeaderFooter footer = new HeaderFooter(new Phrase(Program._resourceManager.GetString("page")), true);
                footer.Alignment = 1;
                footer.Border = Rectangle.ALIGN_CENTER;
                _pdfDocument.Footer = footer;            _pdfDocument.Open();            //添加标题
                Paragraph Header = new Paragraph(new Paragraph(Program._resourceManager.GetString("weldTable"), font));
                Header.Alignment = 1;
                _pdfDocument.Add(Header);            //添加数据表格
                _pdfDocument.NewPage();
                _pdfDocument.Add(AddTable(lvi));            //添加图片
                _pdfDocument.NewPage();
                if(PrintHeads.ImageAddress != null && PrintHeads.ImageAddress.Length != 0)
                {
                    foreach(string str in PrintHeads.ImageAddress)
                    {
                        iTextSharp.text.Image image =
                            iTextSharp.text.Image.GetInstance(str);
                        image.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
                        _pdfDocument.Add(image);
                    }
                }            _pdfDocument.Close();
            }        #endregion        #region Methods        /// <summary>
            /// 添加数据表格
             /// </summary>
            /// <returns></returns>
            private Table AddTable(ListView lvi)
            {
                Font font = new Font(_bfSun,12,1);
                Table myTable = new Table(4);
                myTable.BorderWidth = 0;
                myTable.BorderColor = new Color(0, 0, 255);
                myTable.Cellpadding = 1;
                myTable.TableFitsPage = true;            for(int columNum = 0; columNum != lvi.Columns.Count; columNum++)
                {
                    Cell myCell = new Cell(new Phrase(lvi.Columns[columNum].Text, font));
                    myCell.BackgroundColor = iTextSharp.text.Color.LIGHT_GRAY;
                    myTable.AddCell(myCell);
                    myTable.EndHeaders();
                }            for(int rowNum = 0; rowNum != lvi.Items.Count; rowNum++)
                {
                    for(int columNum = 0; columNum != lvi.Columns.Count; columNum++)
                    {
                        myTable.AddCell(new Phrase(lvi.Items[rowNum].SubItems[columNum].Text,font));
                    }
                }
                return myTable;
            }        #endregion
    }}
    iTextSharp
    参考
    http://www.codeproject.com/KB/cs/pdfizer.aspx
    http://topic.csdn.net/u/20090504/15/b1c3019c-2cd0-4357-8edd-e309eed59661.html