如何重数据库中取出PDF文件并显示

解决方案 »

  1.   

    下载个pdf控件就下了,可以加载文件也可以加载数据流.
      

  2.   

    数据库的PDF格式的文件是以二进制格式存储,
    利用Command取出后,转换为二进制格式的数据流或文件。
    把数据流或文件利用PDF格式文件的读取控件读出来就好了。
    PDF格式是Adobe公司的产品有专门的ACTIVEX控件。
      

  3.   

    控件我也找到了用的是COM组件  里面有一个LOADFILE 方法不知道怎么把二进制数组转成PDF然后显示出来
      

  4.   

    哎呀,你搞这么复杂干吗?贴一段代码给你,记得给分。
    写入:
                 FileStream fs = File.OpenRead(@“D:\aa.pdf”
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                SqlConnection con = new SqlConnection(ConStr);
                SqlCommand cmd = new SqlCommand("", con);
                cmd.CommandText = "Insert into test2 values('10','','D:\aa.pdf',@files)";
                SqlParameter paraFile = new SqlParameter("@files", SqlDbType.Image);
                paraFile.Value = bytes;
                cmd.Parameters.Add(paraFile);
                this.Cursor = Cursors.WaitCursor;
                con.Open();
                int i = cmd.ExecuteNonQuery();
                con.Close();
                this.Cursor = Cursors.Default;
    读出:
                  byte[] mypdffile = null;
                SqlConnection con = new SqlConnection(ConStr);
                SqlCommand cmd = new SqlCommand("", con);
                cmd.CommandText = "Select Files From test2 Where id='10'";
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while(dr.Read())
                {
                    mypdffile = (byte[])dr.GetValue(0);              
                }
                con.Close();
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    FileStream fs = new FileStream("D:\\aaa.pdf", FileMode.Create);
                    fs.Write(mypdffile, 0, mypdffile.Length);
                    fs.Flush();
                    fs.Close();
                }
                catch { }
                this.Cursor = Cursors.Default;
    显示(采用webBrowser控件):
                  this.webBrowser1.Navigate(“D:\\aaa.pdf”);
    楼主分太少了。
      

  5.   

    我想不保存到文件直接用PDF控件显示,怎么做呢 axAcroPDF1.LoadFile(fileName); 这个方法只能加载文件有没有什么其它的方法,重数据库中取出 直接就显示不要保存到方件中
      

  6.   

    ..................
    保存到temp,完后再删除不就和没有保存到文件一样的效果了啊
      

  7.   

    我也试过用你这个方法,但显示的是错误的不能打开
      private void ShowPDF_Load(object sender, EventArgs e)
            {
                this.TopLevel = false;
               SqlConnection con= DB.createcon();
               con.Open();
                       
               
                string sqlstr = "select   top   1   tu   from   tuzhi  where   id='b1409'";
                SqlCommand cm = new SqlCommand(sqlstr, con);
                SqlDataReader dr = cm.ExecuteReader();
                byte[] imageBody = null;
                try
                {
                    //byte[] imageBody = (byte[])cm.ExecuteScalar();
                  
                    while (dr.Read())
                    {
                        imageBody = (byte[])dr.GetValue(0);              
                    }
                   
                   
                    try
                    {
                        FileStream fs = new FileStream("D:\\aaa.pdf", FileMode.Create);
                        fs.Write(imageBody, 0, imageBody.Length);
                        
                        fs.Flush();
                        fs.Close();
                    }
                    catch { }
                    this.Cursor = Cursors.Default;                 //PdfReader pdfread = new PdfReader(imageBody );
                    
                        axAcroPDF1.LoadFile("D:\\aaa.pdf");
          
                 
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.Message);
                }
                cm.Dispose();   
            }
       
      

  8.   

     private void button2_Click(object sender, EventArgs e)
            {
                SqlConnection con = new SqlConnection("server=.;database=drawing;uid=sa;pwd=7612098");
                con.Open();
                FileStream myStream = new FileStream(Filename, FileMode.Open, FileAccess.Read);
                Byte[] imageBody = new byte[myStream.Length];
                myStream.Read(imageBody, 0, (int)myStream.Length);
                myStream.Close();
                string sqlstr = "Insert   into   tuzhi   values('"+textBox1.Text+"','"+textBox2.Text +"','" + imageBody + "','2009-11-4' )";
                try
                {
                    SqlCommand cm = new SqlCommand(sqlstr, con);
                    cm.ExecuteNonQuery();
                    cm.Dispose();
                    MessageBox.Show("上传成功!");
                }
                catch (Exception er)
                {
                    MessageBox.Show(er.Message);
                }   
            }这个是写入 是不 是写入的时候就不对呀
      

  9.   


       /// <summary>
        /// 实现PDF复制,并控制打印权限
        /// </summary>
        /// <param name="filePath">源PDF文件</param>
        /// <param name="toPath">目标PDF文件</param>private void ConvertPDFToPDF(string filePath, string toPath, bool print)
        {
            try
            {
                //读取源pdf中的内容
                PdfReader reader = new PdfReader(filePath);
                Document document = new Document(reader.GetPageSizeWithRotation(1));
                //取得源pdf的页数
                int n = reader.NumberOfPages;
                FileStream baos = new FileStream(toPath, FileMode.Create, FileAccess.Write);
                PdfCopy copy = new PdfCopy(document, baos);
                //往pdf中写入内容   
                document.Open();
                for (int i = 1; i <= n; i++)
                {
                    PdfImportedPage page = copy.GetImportedPage(reader, i);
                    copy.AddPage(page);
                }
                document.Close();
                reader.Close();
            }
            catch
            {
                Response.Write("<script language='javascript'>window.alert('该文件在目录中不存在');window.close();</script>");
                Dtsc.Common.Utility.PageHelper.Close(this.Page);
            }    }我这个方法是在线阅读并要控制打印权限和菜单栏是否的方法(已去掉控制打印权限的代码),希望对你有所帮助。
      

  10.   

    首先,你的数据表的存文件的字段要用Image类型的,
    其次,要用byte[]型的参数将数据流写入该字段。
    红色部分没有传参数。
      

  11.   

    我用你的代码试了一下还是一样的,写好的文件里面没有内容,你的可以把PDF文档,写入数据库并读取出来吗?可以的话,能把代码发个邮件给我吗?[email protected]
      

  12.   

    邮件我收到了,谢谢你,你用的是把TEXTBOX里的文字写到数据库里,而不是把PDF文件存到数据库,有两处代码我有点不明白
     SqlCommand cmd = new SqlCommand("", con);
                cmd.CommandText = "Select Files From test3 Where ID='1'";commandText 为什么会写在 sqlcommand 后面呢,这样会起什么作用的呢,还有SqlCommand cmd = new SqlCommand("", con);  前面这个参数是空又怎么插入到数据库里面的呢?我家里这台电脑上没有数据库也没法看到效果,但我总觉得有点问题
      

  13.   

    数据库的 pdf 格式是二进制的 你读数据库的时候返回的只读器 传入一个文件流内然后把它保存到硬盘 再打开读 就可以了;
      

  14.   

    textbox中写的是你准备写入数据库里的pdf文件在硬盘里的存放地址。
      

  15.   

    SqlCommand cmd = new SqlCommand("", con); 
                cmd.CommandText = "Select Files From test3 Where ID='1'"; 
    第一行只是定义一个SqlCommand型的变量cmd,并且将其连接设为con,其CommandText赋空值,第二行给cmd的CommandText属性重新赋值。