2013-03-22 95 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(); 
        } 
       } 
      } 
     } 
    } 
} 
相關問題