2013-03-22 97 views
0

我在c#中有一个面板,其中包含像图片框和datagridview各种组件。 我想创建一个包含整个datagridview和图片框在一起的pdf。 现在,只有datagridview或图片框出现在pdf中。合并在一起是不可能的。我正在使用iTextSharp来创建PDF。 我的代码如下..合并使用itextsharp在c#中创建pdf的各种组件#

 string strFileName; 

     string FontPath = "C:\\WINDOWS\\Fonts\\simsun.ttc,1"; 

     int FontSize = 12; 

     /// 

     Boolean cc = false; 
     SaveFileDialog savFile = new SaveFileDialog(); 
     savFile.AddExtension = true; 
     savFile.DefaultExt = "pdf"; 
     savFile.Filter = "PDF Document|*.pdf|*.pdf|"; 

     savFile.ShowDialog(); 

     if (savFile.FileName != "") 
     { 
      strFileName = savFile.FileName; 
     } 
     else 
     { 
      MessageBox.Show("export stop", "export stop", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      return; 
     } 


     iTextSharp.text.Image jpg= iTextSharp.text.Image.GetInstance(Properties.Resources.templete3, System.Drawing.Imaging.ImageFormat.Png); 

     jpg.ScaleToFit(750, 850); 
     jpg.SetAbsolutePosition(0, 0); 

     // Page site and margin left, right, top, bottom is defined 
     Document pdfDoc = new Document(PageSize.A4);//, 10f, 10f, 10f, 0f); 


     //If you want to choose image as background then, 



     PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strFileName, FileMode.Create)); 

     BaseFont baseFont = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 

     iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, FontSize); 

     pdfDoc.Open(); 

     PdfPTable table = new PdfPTable(dataGridView1.Columns.Count); 


     for (int j = 0; j < dataGridView1.Columns.Count; j++) 
     { 
      table.AddCell(new Phrase(datagridview[j, 0].Value.ToString(), font)); 
     } 

     table.HeaderRows = 1; 

     for (int i = 0; i < dataGridView1.Rows.Count; i++) 
     { 
      for (int j = 0; j < dataGridView1.Columns.Count; j++) 
      { 
       try 
       { 
        table.AddCell(new Phrase(dataGridView1[j, i].Value.ToString(), font)); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
        cc = true; 
       } 
      } 
     } 


     pdfDoc.NewPage(); 

     pdfDoc.Add(jpg); 

     pdfDoc.Add(table); 
     pdfDoc.Close(); 

     Process.Start(strFileName); 

    } 


} 
+0

面板/数据网格/图像在asp.net应用程序中使用?如果是这样,您可能可以将它们呈现为HTML并将其转换。与这个问题无关,我觉得你很可爱如何将你的PNG命名为'jpg'。 – Nenotlep 2013-03-22 07:28:21

+0

@Nenotlep:不幸的是,我们使用的是c#语言。有没有其他的方式来使用c# – 2013-03-22 10:08:15

回答

0

这不是一个答案(还)。您的代码似乎是大型项目的一部分,并且有许多无关的代码与特定问题无关。诊断这类事情的第一步是删除所有不重要的事情,以便我们能够真正重现您的问题。以下是试图做到这一点。

该代码首先根据您告诉我们的内容创建示例环境。首先它创建一些样本数据并将其添加到DataGridView。然后它将现有图像加载到PictureBox中。然后它将这两个控件添加到新创建的Panel。完成这些步骤后,它会创建一个PDF,从PictureBox获取图像,从表中获取数据并将所有这些数据添加到PDF中。有关更多详细信息,请参阅代码中的注释。当我运行这段代码时,我在我的PDF中同时获得一张表格和一张图片。

如果你运行这个代码,开始一个全新的项目 - 不要使用你现有的项目。我无法强调这一点。不要选择部分代码运行,启动一个新项目并使用它,只有这个用于你的代码。如果这可行,那么希望你可以开始比较工作代码和非工作代码之间的差异。

此代码在VS Express 2012 For Windows桌面对iTextSharp 5.4.0进行了测试。

using iTextSharp.text; 
using iTextSharp.text.pdf; 
using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.IO; 
using System.Windows.Forms; 

namespace WindowsFormsApplication20 { 
    public partial class Form1 : Form { 
     public Form1() { 
      InitializeComponent(); 
     } 
     //Used for our sample data 
     public class Person { 
      public string FirstName { get; set; } 
      public string LastName { get; set; } 
     } 

     private void Form1_Load(object sender, EventArgs e) { 
      //Sample image, set to a PNG 
      var sampleImagePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "sample.png"); 
      //Full path to the PDF to export 
      var exportFilePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf"); 

      //Next we're going to create all of the basic controls per the OP's scenario 

      //Create some sample data to put into our DGV 
      var P1 = new Person() { FirstName = "Alice", LastName = "Cooper" }; 
      var P2 = new Person() { FirstName = "Bob", LastName = "Dole" }; 
      var People = new List<Person>(new Person[] { P1, P2 }); 

      //Create our sample DataGridView 
      var dataGridView1 = new DataGridView(); 
      dataGridView1.AutoGenerateColumns = true; 
      dataGridView1.DataSource = People; 
      dataGridView1.Location = new Point(0, 0); 

      //Create our sample PictureBox 
      var PB = new PictureBox(); 
      PB.Load(sampleImagePath); 
      PB.Location = new Point(400, 0); 
      PB.SizeMode = PictureBoxSizeMode.AutoSize; 


      //Create our sample panel and give it room to show everything 
      var panel = new Panel(); 
      panel.AutoSize = true; 
      panel.Dock = DockStyle.Fill; 

      //Add the above controls to our DGV 
      panel.Controls.Add(dataGridView1); 
      panel.Controls.Add(PB); 
      //Add the DGV to the form 
      this.Controls.Add(panel); 

      //Basic PDF creation here, nothing special 
      using (var fs = new FileStream(exportFilePath, FileMode.Create, FileAccess.Write, FileShare.None)) { 
       using (var pdfDoc = new Document(PageSize.A4)) { 
        using (var writer = PdfWriter.GetInstance(pdfDoc, fs)) { 
         pdfDoc.Open(); 

         //Get our image (Code is mostly the OP's) 
         iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(PB.Image, System.Drawing.Imaging.ImageFormat.Png); 
         jpg.ScaleToFit(750, 850); 
         jpg.SetAbsolutePosition(0, 0); 

         //Create our table 
         var table = new PdfPTable(dataGridView1.Columns.Count); 

         //Add the headers from the DGV to the table 
         for (int j = 0; j < dataGridView1.Columns.Count; j++) { 
          table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText)); 
         } 

         //Flag the first row as a header 
         table.HeaderRows = 1; 

         //Add the actual rows from the DGV to the table 
         for (int i = 0; i < dataGridView1.Rows.Count; i++) { 
          for (int j = 0; j < dataGridView1.Columns.Count; j++) { 
           table.AddCell(new Phrase(dataGridView1[j, i].Value.ToString())); 
          } 
         } 

         //Add our image 
         pdfDoc.Add(jpg); 
         //Add out table 
         pdfDoc.Add(table); 
         pdfDoc.Close(); 
        } 
       } 
      } 
     } 
    } 
} 
相关问题