c# 利用GDI+和itextsharp 生成pdf

 public static void Main()
        {  
            
            const int width = 530, height = 350;
            Bitmap myPalette = new Bitmap(width, height);
            Graphics myGraphics1 = Graphics.FromImage(myPalette);
            // 绘制灰色背景
            myGraphics1.FillRectangle(new SolidBrush(System.Drawing.Color.LightGray), 0, 0, width, height);
            // 设定显示数据

            string[] DataName = { "2011-9", "2011-8", "2011-7", "2011-6", "2011-5", "2011-4", "2011-3", "2011-2", "2011-1", "2010-12", "2010-11", "2010-10","2010-9" };
            int[] Data = { 20, 100, 50, 60, 20, 240, 20, 100, 50, 60, 20, 240,23 };
            //设定显示颜色
            System.Drawing.Color[] myColors = {
                                System.Drawing.Color.Blue,
                                System.Drawing.Color.Red,
                                System.Drawing.Color.Yellow,
                                System.Drawing.Color.Purple,
                                System.Drawing.Color.Orange,
                                System.Drawing.Color.Brown,
                                System.Drawing.Color.AliceBlue,
                                System.Drawing.Color.Beige,
                                System.Drawing.Color.Brown,
                                System.Drawing.Color.LawnGreen,
                                System.Drawing.Color.LightBlue,
                                System.Drawing.Color.Lavender,
                                System.Drawing.Color.LightYellow,
                                System.Drawing.Color.Olive,
                                };
            //绘制柱图
            for (int i = 0; i < DataName.Length; i++)
            {
                //填充柱图
               myGraphics1.FillRectangle(new SolidBrush(myColors[i]),(i * 40) + 15, 300 - Data[i]/2, 20, Data[i]/2+ 5);             
                //绘制柱图边界           
             
               myGraphics1.DrawRectangle(new Pen(System.Drawing.Color.Black),(i * 40)+15,300 - Data[i]/2,20,Data[i]/2+5);
                //绘制柱图上方数据
               myGraphics1.DrawString(Data[i].ToString(), new System.Drawing.Font("宋体", 9), Brushes.Black, new PointF((i * 40) + 5, 300 - Data[i]/2 - 20));
                //绘制柱图下标
               myGraphics1.DrawString(DataName[i], new System.Drawing.Font("宋体", 9), Brushes.Black, new PointF((i * 40) + 3,320));
            }
            //输出图像
          
            myPalette.Save("aaa.jpg", ImageFormat.Jpeg);
          

            myGraphics1.Dispose();
            myPalette.Dispose();
           

          
            Document document = new Document();
            try
            {
                // step 2:
                // we create a writer that listens to the document
                // and directs a PDF-stream to a file
                PdfWriter.getInstance(document, new FileStream("Chap0504.pdf", FileMode.Create));
                // step 3: we open the document
               // iTextSharp.text.Image favicon = iTextSharp.text.Image.getInstance("favicon.jpg");
                BaseFont bfSun = BaseFont.createFont(@"c:\WINDOWS\fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                iTextSharp.text.Font font = new iTextSharp.text.Font(bfSun, 10);
                string text = "                                       资料来源:";
                //document.Add(new Paragraph(text, font));

                HeaderFooter header = new HeaderFooter(new Phrase(text, font),true);
                document.Header = header;
                Watermark watermark = new Watermark(iTextSharp.text.Image.getInstance("tata.jpg"), 16,790);
                document.Add(watermark);
                document.Open();
                string chinacode = "中文指标:";
                string codeunit = "指标单位:";
                string datasource = "数据来源:";
                string dataclassify = "数据分类:";
                string newdate = "最新日期:";
                string datachart = "数据图表:";
              
                document.Add(new Paragraph(chinacode,font));
                document.Add(new Paragraph(codeunit,font));
                document.Add(new Paragraph(datasource,font));
                document.Add(new Paragraph(dataclassify, font));
                document.Add(new Paragraph(newdate,font));
                document.Add(new Paragraph(datachart, font));
                iTextSharp.text.Image jpg = iTextSharp.text.Image.getInstance("aaa.jpg");
                // step 4: we create a table and add it to the document
               
                Table aTable = new Table(13, 2);    // 2 rows, 2 columns
                //aTable.BorderWidth = 3;
              
                aTable.AutoFillEmptyCells = true;
               
           
                              
                //aTable.addCell(DataName[0].ToString());
                //aTable.addCell(DataName[1].ToString());
                //aTable.addCell(DataName[2].ToString());
                //aTable.addCell(DataName[3].ToString());
                //aTable.addCell(DataName[4].ToString());
                //aTable.addCell(DataName[5].ToString());
                //aTable.addCell(DataName[6].ToString());
                //aTable.addCell(DataName[7].ToString());
                //aTable.addCell(DataName[8].ToString());
                //aTable.addCell(DataName[9].ToString());
                //aTable.addCell(DataName[10].ToString());
                //aTable.addCell(DataName[11].ToString());
                //aTable.addCell(DataName[12].ToString());
                for (int i = 0; i < DataName.Length ; i++)
                {
                    Cell cell = new Cell(DataName[i].ToString());
                    cell.Rowspan =190;
                    //cell.Colspan = 3;
                  
                    aTable.addCell(cell);
                }
                for (int i = 0; i < Data.Length ; i++)
                {

                    Cell cell = new Cell(Data[i].ToString());
                    cell.Rowspan = 190;
                  //  cell.Colspan = 3;
                    aTable.addCell(cell);
                }
               
              

              
                document.Add(jpg);
                document.Add(new Paragraph("Date:"));
                document.Add(aTable);
                string one = "注:1.如需历史数据或更多定制数据,请在数据圈论坛、资料交换区/悬赏-求助-找资料板块(http://www.witland.com.cn)提交需求";
    string two = "     2.提交需求请注明所需要的详细指标列表 时间 地区.";
                string three = "  3.更多帮助请联系客户服务qq:128";
                string four = "                           MSN:wit";
                string five = "                            Gtalk:j"; 
                string sex = "                            qq群:";
                string seven = "                           MSN群:加gro为好友";
              //  string footer = "塔塔信息咨询有限公司版权所有,未经允许,严禁转载";
                document.Add(new Paragraph(" "));
                document.Add(new Paragraph(" "));
             
              
                document.Add(new Paragraph(one, font));
                document.Add(new Paragraph(two, font));
                document.Add(new Paragraph(three, font));
                document.Add(new Paragraph(four, font));
                document.Add(new Paragraph(five, font));
                document.Add(new Paragraph(sex, font));
                document.Add(new Paragraph(seven, font));
                document.Add(new Paragraph(" "));
               

              //  document.Add(new Paragraph(footer, font));

             
            }
            catch (DocumentException de)
            {
                Console.Error.WriteLine(de.Message);
            }
            catch (IOException ioe)
            {
                Console.Error.WriteLine(ioe.Message);
            }
            // step 5: we close the document
            document.Close();
           //Console.ReadKey();
           
      
    }

posted on 2011-03-16 10:48  树欲静兮  阅读(556)  评论(0编辑  收藏  举报