0

My need is: if the last PDF file created is open then a MessageBox must pop up on the screen warning user to close it before creating a new one.

This is my code to create the PDF file using iTextSharp, dataGridView and MySQL datas:

private void button2_Click(object sender, EventArgs e)
{
    if ((comboBox1.Text != "") && (comboBox2.Text != ""))
    {
        Document document = new Document();

        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("D:\\sample.pdf", FileMode.Create));

        document.Open();
        iTextSharp.text.Font font5 = FontFactory.GetFont(FontFactory.HELVETICA, 10);
        iTextSharp.text.Font font10 = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 30);

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

        Paragraph themeWorkshop;
        themeWorkshop = new Paragraph(comboBox1.SelectedItem.ToString(), font10);
        themeWorkshop.Alignment = Element.ALIGN_CENTER;
        themeWorkshop.SpacingAfter = 15f;
        document.Add(themeWorkshop);

        Paragraph turma;
        turma = new Paragraph("Turma: " + comboBox2.SelectedItem.ToString(), font5);
        turma.Alignment = Element.ALIGN_CENTER;
        turma.SpacingAfter = 15f;
        document.Add(turma);

        Paragraph dateWorkshop;
        dateWorkshop = new Paragraph("Date: " + dateWorkshop, font5);
        dateWorkshop.Alignment = Element.ALIGN_CENTER;
        dateWorkshop.SpacingAfter = 15f;
        document.Add(dateWorkshop);

        Paragraph hourWorkshop;
        hourWorkshop = new Paragraph("Hour: " + hourWorkshop, font5);
        hourWorkshop.Alignment = Element.ALIGN_CENTER;
        hourWorkshop.SpacingAfter = 15f;
        document.Add(hourWorkshop);

        table.DefaultCell.Padding = 4;

        table.TotalWidth = 200;
        table.WidthPercentage = 100;
        int iCol = 0;
        string colname = "";

        foreach (DataGridViewColumn c in dataGridView1.Columns)
        {
            if (c.HeaderText.ToString().Equals("nStudents"))
            {
                table.AddCell(new Phrase(c.HeaderText.Replace("nStudents", "Number"), font5));
            }
            else
            {
                table.AddCell(new Phrase(c.HeaderText.Replace("nameStudents", "NAME"), font5));
            }
        }

        foreach (DataGridViewRow r in dataGridView1.Rows)
        {
            foreach (DataGridViewCell cell in r.Cells)
            {
                var cells = cell.Value;
                if (cells != null)
                {
                    table.AddCell(cells.ToString());
                }
            }
        }

        document.Add(table);
        document.Close();
        writer.Close();
        MessageBox.Show("PDF Created");
    }
    else
    {
        MessageBox.Show("Make sure you fill out all required fields","Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    
}

I tried the follow code, but it didn`t work:

Protected virtual bool IsFileInUse(FileInfo file)
{
    try
    {
        FileStream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
        stream.Dispose();
    }
    catch (IOException)
    {
        //Could not access because file is in use.
        return true;
    }

    //file is not in use
    return false;
}
Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101

0 Answers0