pcconfuser
pcconfuser

Reputation: 25

iTextSharp creating corrupt PDF files C#

This section of my program is in charge of reading the scores from earlier in my program, which is displayed in a couple of text boxes, and creating a .pdf file of this data. Originally, this worked. However, for an unknown reason it has started to create corrupt .pdf files.

Here is the section of my program:

private void SaveToPDF_Click(object sender, EventArgs e)
    {

        SaveFileDialog SavePDFDialog = new SaveFileDialog();
        Stream MyStream;
        SavePDFDialog.Filter = "PDF File (*.pdf)|*.pdf|All Files(*.*)|*.*";
        SavePDFDialog.FilterIndex = 1;
        SavePDFDialog.RestoreDirectory = true;
        SavePDFDialog.FileName = ("Report");


            if (SavePDFDialog.ShowDialog() == DialogResult.OK)
            {
                if ((MyStream = SavePDFDialog.OpenFile()) != null)
                {

                    try
                    {
                        Document document = new Document();
                        PdfWriter.GetInstance(document, new FileStream(PDFDirectory, FileMode.Create));
                        document.Open();
                        //Paragraph h = new Paragraph("Results from: " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLocalTime());
                        Paragraph h = new Paragraph("Results from: " + DateTime.Now.ToLocalTime());
                        Paragraph p1 = new Paragraph("The Top Scoring student is:" + TopStudentBox.Text);
                        Paragraph p2 = new Paragraph("The Question answer wrong the most is: " + MissedQuestionBox.Text);
                        document.Add(h);
                        document.Add(p1);
                        document.Add(p2);
                        document.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    MyStream.Close();
                }
            }
    }

Thank you in advance.

Upvotes: 0

Views: 595

Answers (2)

pcconfuser
pcconfuser

Reputation: 25

I seemed to have fixed the program by removing:

MyStream = new FileStream(PDFDirectory, FileMode.Create);

I figured this as I am now letting the user choose the save directory, therefore I do not need the PDFDirectory variable and so do not need to tell the program to create a file there, which it was doing. @mlk, you were also right about me not using the MyStream Here is the fixed code:

private void SaveToPDF_Click(object sender, EventArgs e)

{

SaveFileDialog SavePDFDialog = new SaveFileDialog();
Stream MyStream;
SavePDFDialog.Filter = "PDF File (*.pdf)|*.pdf|All Files(*.*)|*.*";
SavePDFDialog.FilterIndex = 1;
SavePDFDialog.RestoreDirectory = true;
SavePDFDialog.FileName = ("Report");


    if (SavePDFDialog.ShowDialog() == DialogResult.OK)
    {
        if ((MyStream = SavePDFDialog.OpenFile()) != null)
        {

            try
            {
                Document document = new Document();
                PdfWriter.GetInstance(document, MyStream);
                document.Open();
                //Paragraph h = new Paragraph("Results from: " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLocalTime());
                Paragraph h = new Paragraph("Results from: " + DateTime.Now.ToLocalTime());
                Paragraph p1 = new Paragraph("The Top Scoring student is:" + TopStudentBox.Text);
                Paragraph p2 = new Paragraph("The Question answer wrong the most is: " + MissedQuestionBox.Text);
                document.Add(h);
                document.Add(p1);
                document.Add(p2);
                document.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            MyStream.Close();
        }
    }

}

Upvotes: 0

user5226582
user5226582

Reputation: 1986

Stealing @mkl's comment:

I think it's because you write to one stream:

PdfWriter.GetInstance(document, new FileStream(PDFDirectory, FileMode.Create));

and close another:

MyStream.Close();

I think this should work, but you may need to change a few things:

private void SaveToPDF_Click(object sender, EventArgs e)
{

    SaveFileDialog SavePDFDialog = new SaveFileDialog();
    Stream MyStream;
    SavePDFDialog.Filter = "PDF File (*.pdf)|*.pdf|All Files(*.*)|*.*";
    SavePDFDialog.FilterIndex = 1;
    SavePDFDialog.RestoreDirectory = true;
    SavePDFDialog.FileName = ("Report");


        if (SavePDFDialog.ShowDialog() == DialogResult.OK)
        {
            if ((MyStream = SavePDFDialog.OpenFile()) != null)
            {

                try
                {
                    MyStream = new FileStream(PDFDirectory, FileMode.Create);
                    Document document = new Document();
                    PdfWriter.GetInstance(document, MyStream);
                    document.Open();
                    //Paragraph h = new Paragraph("Results from: " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLocalTime());
                    Paragraph h = new Paragraph("Results from: " + DateTime.Now.ToLocalTime());
                    Paragraph p1 = new Paragraph("The Top Scoring student is:" + TopStudentBox.Text);
                    Paragraph p2 = new Paragraph("The Question answer wrong the most is: " + MissedQuestionBox.Text);
                    document.Add(h);
                    document.Add(p1);
                    document.Add(p2);
                    document.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                MyStream.Close();
            }
        }
}

Upvotes: 1

Related Questions