zoukankan      html  css  js  c++  java
  • Preview PDF from Database

    If you want to preview pdf in the winform, you need to make sure Adobe PDF Reader is installed on your computer. Then you can add control "Adobe PDF Reader" by the following steps.

    1.Choose "Tools" and click "Choose Toolbox items…"

    2.Click "COM Components" and choose "Adobe PDF Reader", then click "OK"

    3.Drag "Adobe PDF Reader" to winform

    Then try the code:

    static byte[] filestream;
    
    private void Form1_Load(object sender, EventArgs e)
    {
        Image i = Image.FromFile(@"D:1.png");
        filestream = ConvertToBinary(@"D:a.pdf");
        DataGridViewRow dr = new DataGridViewRow();
        dr.CreateCells(dataGridView1);
        dr.Cells[1].Value = i;
        dr.Cells[2].Value = filestream;
        dataGridView1.Rows.Add(dr);
        dataGridView1.AllowUserToAddRows = false;
    }
    
    public static byte[] ConvertToBinary(string Path)
    {
        FileStream stream = new FileInfo(Path).OpenRead();
        byte[] buffer = new byte[stream.Length];
        stream.Read(buffer, 0, Convert.ToInt32(stream.Length));
        return buffer;
    }
    
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        int CIndex = e.ColumnIndex;
        if (CIndex == 0)
        {
            string savepath = @"D:
    ew.pdf";
            if (System.IO.File.Exists(savepath))
            {
                System.IO.File.Delete(savepath);
            }
            FileStream fs = new FileStream(savepath, FileMode.CreateNew);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(filestream, 0, filestream.Length);
            bw.Close();
            fs.Close();
            axAcroPDF.LoadFile(savepath);
        }
    }
    View Code

    Otherwise, if you want to preview it with other softwares, such as the browser, modify the code as follows.

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        int CIndex = e.ColumnIndex;
        if (CIndex == 0)
        {
            string savepath = @"D:
    ew.pdf";
            if (System.IO.File.Exists(savepath))
            {
                System.IO.File.Delete(savepath);
            }
            FileStream fs = new FileStream(savepath, FileMode.CreateNew);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(filestream, 0, filestream.Length);
            bw.Close();
            fs.Close();
            
            // modify this
            Process.Start(savepath);
        }
    }
    View Code

    The result of winform like as follows,

  • 相关阅读:
    rm
    Linux下解包/打包,压缩/解压命令
    虚拟机安装---vm12+ubuntukylin16.04
    mysql-5.6.41-winx64安装
    tensorflow学习笔记一------下载安装,配置环境(基于ubuntu16.04 pycharm)
    大一上学期C语言学习心得总结
    常见HTTP状态码
    Java语言基础及java核心
    linux下安装JMeter(小白教程)
    Linux下安装JDK(小白教程)
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/9970150.html
Copyright © 2011-2022 走看看