C# winform экспорт в PDF
Я создал небольшое приложение, чтобы попытаться понять, как экспортировать изображения в pdf. Приведенный ниже код экспортируется, но PDF-файл остается пустым.
У меня есть 2 столбца ID и Image.
<pre>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using iTextSharp.text; using iTextSharp.text.pdf; namespace TestImage { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //browse image into pictureBox1 private void BtnBrowseImage_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif"; if (ofd.ShowDialog() == DialogResult.OK) { pictureBox1.Image = System.Drawing.Image.FromFile(ofd.FileName); } } //button add data to dataGridView //insert image from pictureBox to dataGridView private void BtnAdd_Click(object sender, EventArgs e) { try { MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat); byte[] img = ms.ToArray(); dataGridView1.Rows.Add(textBoxId.Text, img); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void Form1_Load(object sender, EventArgs e) { //create a DataGridView Image Column DataGridViewImageColumn dgvImage = new DataGridViewImageColumn(); //set a header test to DataGridView Image Column dgvImage.HeaderText = "Image"; dgvImage.ImageLayout = DataGridViewImageCellLayout.Stretch; DataGridViewTextBoxColumn dgvId = new DataGridViewTextBoxColumn(); dgvId.HeaderText = "Id"; dataGridView1.Columns.Add(dgvId); dataGridView1.Columns.Add(dgvImage); dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView1.RowTemplate.Height = 120; dataGridView1.AllowUserToAddRows = false; } private void btn_Export_Click(object sender, EventArgs e) { if (dataGridView1.Rows.Count > 0) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "PDF (*.pdf)|*.pdf"; sfd.FileName = "Output.pdf"; bool fileError = false; if (sfd.ShowDialog() == DialogResult.OK) { if (File.Exists(sfd.FileName)) { try { File.Delete(sfd.FileName); } catch (IOException ex) { fileError = true; MessageBox.Show("It wasn't possible to write the data to the disk." + ex.Message); } } if (!fileError) { try { PdfPTable pdfTable = new PdfPTable(dataGridView1.Columns.Count); pdfTable.DefaultCell.Padding = 3; pdfTable.WidthPercentage = 100; pdfTable.HorizontalAlignment = Element.ALIGN_LEFT; foreach (DataGridViewColumn column in dataGridView1.Columns) { PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText)); pdfTable.AddCell(cell); } ////Option 2 foreach (DataGridViewRow row in dataGridView1.Rows) { string Id = row.Cells[0].Value.ToString(); // Now your image cell byte[] imageByte = (byte[])row.Cells[1].Value; iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance(imageByte); pdfTable.AddCell(myImage); } //Stop Option 2 using (FileStream stream = new FileStream(sfd.FileName, FileMode.Create)) { Document pdfDoc = new Document(PageSize.A4, 10f, 20f, 20f, 10f); PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); pdfDoc.Add(pdfTable); pdfDoc.Close(); stream.Close(); } MessageBox.Show("Data Exported Successfully !!!", "Info"); } catch (Exception ex) { MessageBox.Show("Error :" + ex.Message); } } } } else { MessageBox.Show("No Record To Export !!!", "Info"); } } } }
Что я уже пробовал:
////Option 1 //foreach (DataGridViewRow row in dataGridView1.Rows) //{ // foreach (DataGridViewCell cell in row.Cells) // { // pdfTable.AddCell(cell.Value.ToString()); ////Stop Option 1