Share via


How to include a pdf document in Visual Studio 2010 (and C#)

Question

Monday, May 30, 2011 5:21 PM

Hi,

At the moment, my application has a button when clicked opens a pdf from a web-site I would like to include a pdf.  Overall, this is using 'Process.Start(www.mysite.com/info.pdf);'

While this works okay, it takes a little time to open the file.  So what I would like to do is to include this pdf (info.pdf) into the VS 2010 solution, and then an .msi will be created which presumably will incorporate this pdf.  I found a site which covers including an image file in VS @ www.csharp411.com/embedded-image-resources/

Some questions on this:

1) Is this a good idea / good programming practice to include a pdf in a solution, which will in turn end up as part of the .msi installer?

2) If so then I would like to include this pdf file in the resources folder in the VS project.  So should I include this as an embedded resource like the example or is there a better way?

3) If this is the best way, then the line in the example 'Bitmap bmp = new Bitmap( myStream );', what should this be for a pdf file?

Any idea's / suggestions appreciated.

Stephen

All replies (7)

Thursday, June 2, 2011 4:07 AM âś…Answered | 1 vote

Hi SWClarke,

It is odd that as you have added the pdf file into your application as embedded resources, and I have tested the code in win7, it can work well. I am not sure which line of code causes the problem.

>Process.Start(www.mysite.com/info.pdf);

When you have added the pdf file, so I am not sure how do you write the "Process.Start()" method, try to use as follows to start the pdf file:

Process.Start("test.pdf")

 Also, I am not sure where do you store the "test.pdf" file or where do you deploy the app in your harddisk when you use "FileStream fs = File.Create("test.pdf")", is it in the "C:\ProgramFile" disk or others? If so, try to store the PDF file in another directory. Such as follows:

FileStream fs = File.Create(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\test.pdf")", 

Also, try to run the app as Administator(Right-click the app, then click "Run as Administrator").

Vin Jin [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Tuesday, May 31, 2011 5:57 AM | 1 vote

pdf should be a file path. dll then you should include in your installtion.

chanmm

chanmm


Tuesday, May 31, 2011 12:03 PM

Thanks for your reply.  I'm not sure what you mean by this.  I had hoped to include/add this pdf into the 'Project1\Resources' folder in Visual Studio in the VS solution, and then to specify this path from a class like program.cs e.g. 'Process.Start(Project1\Resources\info.pdf);'.

Is this what you mean i.e. to include it in a project in the VS solution?  If so then how do I reference it from a class in the same project to call it when needed which should then open the pdf?

Stephen


Wednesday, June 1, 2011 1:53 AM | 3 votes

Hi SWClarke,

When you add a file in to a solution and set it to asn embeded resource, then the file will be added into your assembly as binary stream. For your first question, I am not sure whether the pdf is a big or small file, it is a good choice to add a small resource file into a solutioin. But if it is so big, and it will enlarge the whole .msi file, but if you don't care it, then it is OK.

For your sceond question, yes, you should include the file as embedded resource if you want to deploy it within your project. Then embedded resource option in build action is typically used for resource files.

You can take the following steps to add a pdf file into as resource into your project:

  1. Right-click your project and select the "Properties" option.
  2. Then click the "Resources" tab and it will show the dialog for you to add resources in the design time.
  3. The default page is for add String resources, you can select the combobox in the top-right to select the "file" item.
  4. Then click the "Add Resource" button to select the pdf file and click OK.
  5. At last, the pdf file will show in the blank area. It means that you have added it successfully.

Then you can use the following code to get the all bytes of the pdf file:

Byte[] bytes = Properties.Resources.Test;

Then use a Stream class to write all bytes into a .pdf file then open it.

Byte[] bytes = Properties.Resources.Test;

      using (FileStream fs = File.Create("test.pdf"))
      {
        fs.Write(bytes, 0, bytes.Length);
      }
      Process.Start("test.pdf");

Or you can use the way as link says. But the FCL(Framework class library never provides the pdf class as the Bitmap), so you should write the stream into your .pdf file as follows:

Assembly myAssembly = Assembly.GetExecutingAssembly();
      Stream myStream = myAssembly.GetManifestResourceStream("MyNamespace.SubFolder.Mypdf.pdf");
      using (FileStream fs = File.Create("test.pdf"))
      {
        int b = myStream.ReadByte();
        while (b != -1)
        {
          fs.WriteByte((byte)b);
          b = myStream.ReadByte();
        }
      }

Vin Jin [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Wednesday, June 1, 2011 3:03 PM

Thanks a lot for the information.  As you suggested I included my pdf as an embedded file in the resources, and then used the code

Byte[] bytes = Properties.Resources.Test; .....

to open the pdf from the application (using a menu).

However, even though this works fine in Win XP, when I try to run the application in Win 7 it returns the message that access to the pdf is denied.  This is even though I am logged into the Win 7 machine as admin.  Is there any reason that you know of for this to happen?

I included this in a try-catch so that the catch (in the Win 7 case) reverts to the older method of calling the pdf from the website and opening it in a browser.  While this older method works okay it takes much longer to open than embedding it as above.

 

Stephen


Tuesday, June 7, 2011 2:22 PM

Thanks Vin Jin

I tried as you suggested to create fs SpecialFolder, and create the file in a local directory as below:

FileStream fs = File.Create(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\test.pdf")", 

and then open it from this path as below

Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\test.pdf")

As you say the first way should have worked fine i.e. no need to save it in a location on the hard drive and access it from here.  However this works fine, and no harm to have the pdf stored on a known location.

Thanks again,

Stephen

Stephen


Wednesday, June 18, 2014 1:28 PM

Thanx a lot ,i were also facing the same problem but now it resolved ....