无法检查PDF文件是否正确创建

本文关键字:创建 是否 文件 检查 PDF | 更新日期: 2023-09-27 17:59:30

我无法打开我的pdf文件。它说其他进程正在使用它。但是我已经停止运行我的程序了。我做错什么了吗?

我的按钮运行select程序,将其存储在数据表中

 protected void btnPrint_Click(object sender, EventArgs e)
    {
    Connection con = new Connection();
    SqlDataAdapter da;
    DataTable ds;
    con.con = new SqlConnection(con.str);
    con.cmd.CommandText = "aSelectProcedure";
    con.cmd.CommandType = CommandType.StoredProcedure;
    da = new SqlDataAdapter();
    da.SelectCommand = con.cmd;
    ds = new DataTable();
    try
        {
        con.con.Open();
        da.Fill(ds);
        }
    catch (Exception ex)
        {
        }
    finally
        {
        con.con.Close();
        con.con.Dispose();
        ExportToPdf(ds);
        }
    }

我在这里打印数据表:

public void ExportToPdf(DataTable dt)
    {
    string pdfFilePath = @"D:/myPdf.pdf";
    Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
    PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));
    doc.Open();
   if (dt != null)
     {
         //Craete instance of the pdf table and set the number of column in that table
         PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
         PdfPCell PdfPCell = null;
         Font font8 = FontFactory.GetFont("ARIAL", 7);
         //Add Header of the pdf table
         PdfPCell = new PdfPCell(new Phrase(new Chunk("ID", font8)));
         PdfTable.AddCell(PdfPCell);
         PdfPCell = new PdfPCell(new Phrase(new Chunk("Name", font8)));
         PdfTable.AddCell(PdfPCell);
         //How add the data from datatable to pdf table
         for (int rows = 0; rows < dt.Rows.Count; rows++)
         {
             for (int column = 0; column < dt.Columns.Count; column++)
             {
                 PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8)));
                 PdfTable.AddCell(PdfPCell);
             }
         }
         PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table
         Paragraph paragraph = new Paragraph("Using ITextsharp I am going to show how to create simple table in PDF document ");
         doc.Add(paragraph);// add paragraph to the document
         doc.Add(PdfTable); // add pdf table to the document

无法检查PDF文件是否正确创建

添加段落等后是否关闭文档?在iTextSharp中,示例代码显示:

pdfDoc.Open();
//Some content added in between
pdfDoc.Close();

我不知道你是否关闭了它,因为它不在你的样品中。有时,在处理文件时,如果不关闭它们,它们就会损坏,无法打开。这可能是个问题。