如何在导出的pdf文件打开时导出为pdf时打印PDF文件和错误

如何在导出的pdf文件打开时导出为pdf时打印PDF文件和错误

问题描述:

我想用按钮打印从datagridview导出到PDF的PDF文件。当导出的pdf文件打开时,第二次导出为PDF文件时,会出现一个错误(应用程序中出现未处理的异常)。如何显示通知消息以屏幕打开PDF文件。代码如下:



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?
        } 





我的尝试:



没什么。



What I have tried:

Nothing.

你应该将你的代码包装在try / catch块中并以合理的方式处理异常给用户的错误信息。
You should wrap your code in a try/catch block and properly handle the exception in such a way as to present a reasonable error message to the user.


非常感谢。它工作得很好。我用try catch。但是我如何打印上面提到的?



Thank you very much. It worked nice. I used try catch. But how can I print that I mentioned above?

SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "PDF File |*.pdf";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        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);
                    }

                    catch
                    {
                        MessageBox.Show("PDF file is open."Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }