【问题标题】:Unable to check if PDF file is created correctly无法检查 PDF 文件是否正确创建
【发布时间】:2014-06-06 15:08:54
【问题描述】:

我无法打开我的 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

【问题讨论】:

    标签: c# asp.net pdf printing itextsharp


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      • 2012-04-30
      • 1970-01-01
      相关资源
      最近更新 更多