Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to open a pdf file using c# . i have written the following code and got an error as "Use of unassigned local variable a"

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AxAcroPDFLib;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            AxAcroPDF a;
            a.LoadFile("d:/novels/master_of_the_game.pdf");

            a.Show();
        }

        private void axAcroPDF1_OnError(object sender, EventArgs e)
        {
            MessageBox.Show("unable to open file");
        }
    }
}
Posted
Comments
manojm6023 19-Dec-12 7:22am    
In
code
---------
a.LoadFile("d:/novels/master_of_the_game.pdf");
a.Show();
--------------
a.LoadFile()
returns false...
How to solve it

Like this?

AxAcroPDF a = new AxAcroPDF();
a.loadFile(@"C:\a.pdf");

from stackoverflow:
http://stackoverflow.com/questions/5562691/axacropdf-doesn-not-display-pdf-on-windows-xp[^]
 
Share this answer
 
v2
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "Open";
            open.Filter = "All Files|*.*";
            try
            {
                if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    axAcroPDF1.LoadFile(open.FileName);
                }
            }
            catch (ArgumentException ex)
            {
            MessageBox.Show(ex.Message.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);

            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
 
Share this answer
 
string path = "C:\\Report\\filename.pdf";
FileStream fs;
fs = File.Open(path, FileMode.Open);
byte[] bytBytes = new byte[fs.Length];
fs.Read(bytBytes, 0, Convert.ToInt32(fs.Length));
fs.Close();
Response.ContentType = "application/pdf";
Response.BinaryWrite(bytBytes);
FileInfo fi1 = new FileInfo(path);
fi1.Delete();
Response.Flush();
Response.End();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900