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:\new.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:\new.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,

转载于:https://www.cnblogs.com/jizhiqiliao/p/9970150.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值