iTextSharp创建损坏的PDF文件C#

时间:2016-10-21 08:24:13

标签: c# itext

我程序的这一部分负责读取我程序中较早的分数,该分数显示在几个文本框中,并创建该数据的.pdf文件。最初,这有效。但是,由于未知原因,它已经开始创建损坏的.pdf文件。

以下是我的计划部分:

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();
                }
            }
    }

提前谢谢。

2 个答案:

答案 0 :(得分:1)

窃取@ mkl的评论:

我认为这是因为你写了一个流:

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

并关闭另一个:

MyStream.Close();

我认为这应该有效,但您可能需要更改一些内容:

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();
            }
        }
}

答案 1 :(得分:0)

我似乎已经修复了程序:

MyStream

我认为这是因为我现在让用户选择保存目录,因此我不需要PDFDirectory变量,所以不需要告诉程序在那里创建一个文件,它正在做。 @mlk,你也没关系我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();
        }
    }

{

import pickle, requests
import selenium.webdriver 


driver = selenium.webdriver.Firefox()
driver.get("http://google.com")

# getting page title with js
print driver.execute_script("""
    return document.title;
    """)

# add a random cookie
driver.add_cookie({'name':'test', 'value': 'test'})

# save cookies on a pickle file
pickle.dump( driver.get_cookies() , open("cookies.pkl","wb"))

driver.close()

# then later open the cookies with
loaded_cookies = pickle.load(open("cookies.pkl", "rb"))

print loaded_cookies

}