none
微软c#有生成pdf的类库或控件吗? RRS feed

答案

全部回复

  • 目前没有。网上有开源的iText http://sourceforge.net/projects/itext/
    【孟子E章】
    2010年9月9日 9:27
    版主
  • 谢谢!

    2010年9月9日 10:39
  • 目前没有,可以参考孟版主提供的连接,也可以尝试以下做法:

    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
    }

    }

    2010年9月10日 2:09
    版主
  • 谢谢!用Reporting Services的PDF导出了!
    2010年10月12日 2:08