打开asp.net生成的PDF文件时出错

时间:2021-06-27 09:46:54

I'm trying to generate a PDF for my website. Currently, I'm attempting to get the data from the database and display it out on my PDF file, however my main priority was actually to get the value from the asp:label and export it into pdf format. Unfortunately, when i open the PDF file generated, i got this error.

我正在尝试为我的网站生成PDF。目前,我正在尝试从数据库中获取数据并将其显示在我的PDF文件中,但我的主要优先事项实际上是从asp:标签获取值并将其导出为pdf格式。不幸的是,当我打开生成的PDF文件时,我收到了这个错误。

Error : There was an error opening this document. This file is already open or in use by another application

错误:打开此文档时出错。此文件已被其他应用程序打开或使用

protected void btnPDF_Click(object sender, EventArgs e)
    {

        var doc1 = new Document();
        var filename = DDLCase.SelectedItem.Text + ".pdf";
        var output = new FileStream(Path.Combine("C:\\Users\\apr12mpsip\\Desktop", filename), FileMode.Create);
        doc1.Open();

        PdfPTable table = new PdfPTable(3);
        PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
        cell.Colspan = 3;
        cell.HorizontalAlignment = 1; 
        table.AddCell(cell);
        table.AddCell("Col 1 Row 1");
        table.AddCell("Col 2 Row 1");

        doc1.Add(table);

        SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = project; Integrated Security = SSPI");

        SqlCommand cm = new SqlCommand("Select typeofcrime, citizenreport from MemberReport where memberreportid='"+DDLCase.SelectedValue+"'", con);
        con.Open();
        SqlDataReader dr;
        dr = cm.ExecuteReader();
        while (dr.Read())
        {
            table.AddCell(dr[0].ToString());
            table.AddCell(dr[1].ToString());
        }
        dr.Close();


        doc1.Close();

    }

I checked my codes and i couldn't find any ways to solve the error and get the value successfully.

我检查了我的代码,我找不到任何方法来解决错误并成功获得值。

1 个解决方案

#1


1  

You do

你做

var doc1 = new Document();
var filename = DDLCase.SelectedItem.Text + ".pdf";
var output = new FileStream(Path.Combine("C:\\Users\\apr12mpsip\\Desktop", filename), FileMode.Create);
doc1.Open();
[... fetching some data and adding that to`doc1 ...]
doc1.Close();

It leaps to the eye that you do not in any way associate the output with the Document doc1. Thus, your file is not written to at all but neither is it ever closed.

它突显出你不会以任何方式将输出与Document doc1相关联。因此,您的文件根本不会被写入,但它也不会被关闭。

Most likely you additionally want to instantiate a PdfWriter which writes to output and receives from doc1:

很可能你还想要实例化一个写入输出并从doc1接收的PdfWriter:

var doc1 = new Document();
var filename = DDLCase.SelectedItem.Text + ".pdf";
var output = new FileStream(Path.Combine("C:\\Users\\apr12mpsip\\Desktop", filename), FileMode.Create);
PdfWriter.GetInstance(doc1, output); // instantiate a PdfWriter for doc1 and output
doc1.Open();
[... fetching some data and adding that to`doc1 ...]
doc1.Close();

#1


1  

You do

你做

var doc1 = new Document();
var filename = DDLCase.SelectedItem.Text + ".pdf";
var output = new FileStream(Path.Combine("C:\\Users\\apr12mpsip\\Desktop", filename), FileMode.Create);
doc1.Open();
[... fetching some data and adding that to`doc1 ...]
doc1.Close();

It leaps to the eye that you do not in any way associate the output with the Document doc1. Thus, your file is not written to at all but neither is it ever closed.

它突显出你不会以任何方式将输出与Document doc1相关联。因此,您的文件根本不会被写入,但它也不会被关闭。

Most likely you additionally want to instantiate a PdfWriter which writes to output and receives from doc1:

很可能你还想要实例化一个写入输出并从doc1接收的PdfWriter:

var doc1 = new Document();
var filename = DDLCase.SelectedItem.Text + ".pdf";
var output = new FileStream(Path.Combine("C:\\Users\\apr12mpsip\\Desktop", filename), FileMode.Create);
PdfWriter.GetInstance(doc1, output); // instantiate a PdfWriter for doc1 and output
doc1.Open();
[... fetching some data and adding that to`doc1 ...]
doc1.Close();