How to print PDF file and error while exporting to pdf while pdf that pdf file is open

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to print PDF file and error while exporting to pdf while pdf that pdf file is open

Elvin
Hello,

I want to print PDF file that exported from datagridview to PDF with button. And there is one error appears (unhandled exception has occurred in your application) when exporting to PDF file second time while exported pdf file is open. How to show a notification message to screen that PDF file is open. Code is as following:

private void pDFəÇıxartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount == 0)
            {
                DialogResult result = MessageBox.Show("Table is empty.", "Export to PDF", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            else
            {
                //Full path to the Unicode Arial file
                string ARIALUNI_TFF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");

                //Create a base font object making sure to specify IDENTITY-H
                BaseFont bf = BaseFont.CreateFont(ARIALUNI_TFF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

                //Create a specific font object
                iTextSharp.text.Font h = new iTextSharp.text.Font(bf, 20);

                //Create a specific font object
                iTextSharp.text.Font f = new iTextSharp.text.Font(bf, 12);

                //Create a specific font object
                iTextSharp.text.Font g = new iTextSharp.text.Font(bf, 5);

                //Creating iTextSharp Table from the DataTable data
                PdfPTable pdfTable = new PdfPTable(dataGridView1.ColumnCount);
                pdfTable.DefaultCell.Padding = 3;
                pdfTable.SetWidths(new int[6] { 35, 90, 70, 70, 70, 90 });
                pdfTable.TotalWidth = 425;
                pdfTable.LockedWidth = true;
                pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
                //pdfTable.DefaultCell.BorderWidth = 1;

                //Adding Header row
                foreach (DataGridViewColumn column in dataGridView1.Columns)
                {
                    PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, f));
                    cell.BackgroundColor = new iTextSharp.text.Color(188, 222, 248);
                    cell.FixedHeight = 18;
                    pdfTable.AddCell(cell);
                }

                //Adding DataRow
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    foreach (DataGridViewCell celli in row.Cells)
                    {
                        try
                        {
                            pdfTable.AddCell(celli.Value.ToString());
                        }
                        catch { }
                    }
                }

                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "PDF File |*.pdf";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    Document pdfDoc = new Document(PageSize.A4, 85f, 10f, 28f, 20f);
                    PdfWriter wri = PdfWriter.GetInstance(pdfDoc, new FileStream(sfd.FileName, FileMode.Create));
                    pdfDoc.Open();
                    Paragraph paragraph = new Paragraph("Kreditin ödəmə planı", h);
                    paragraph.Alignment = Element.ALIGN_LEFT;
                    pdfDoc.Add(paragraph);
                    pdfDoc.Add(new Paragraph(" "));
                    pdfDoc.Add(new Paragraph("Ödəniş metodu:" + " " + comboBox3.Text, f));
                    pdfDoc.Add(new Paragraph("Kreditin məbləği:" + " " + kreditmeblegi.ToString() + " " + comboBox1.Text, f));
                    pdfDoc.Add(new Paragraph("Faiz dərəcəsi:" + " " + illikfaiz.ToString() + "% illik", f));
                    pdfDoc.Add(new Paragraph("Kreditin müddəti:" + " " + Convert.ToDouble(textBox3.Text) + " " + comboBox2.Text, f));
                    pdfDoc.Add(new Paragraph("Verilmə tarixi:" + " " + dateTimePicker1.Text, f));
                    pdfDoc.Add(new Paragraph(" ", g));
                    pdfDoc.Add(new Paragraph("Aylıq ödəniş:" + " " + label9.Text, f));
                    pdfDoc.Add(new Paragraph("Cəmi məbləğ:" + " " + umumiodenis.ToString("N2") + " " + comboBox1.Text, f));
                    pdfDoc.Add(new Paragraph("Ödənilən faiz:" + " " + odenilenfaiz.ToString("N2") + " " + comboBox1.Text + "\n" + "\n", f));
                    pdfDoc.Add(pdfTable);
                    pdfDoc.Close();
                    MessageBox.Show("Table has been exported to PDF", "Export to PDF", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // How to print?
        }
Loading...