How to open a PDF file, which is also a project resource?

I have a PDF file that I imported as a resource into my project. The file is a reference document, so I want to be able to include it with every deployment. I want to open this file with the click of a button.

I set the build action to "Embedd Resource". So now I want to open it. However, when I try to access a resource - My.Resources.HelpFile is an array of bytes. How do I open this if I know that the end user has a program suitable for opening PDF documents?

If I skipped the previous question, please point me in the right direction. I found a few questions about opening a PDF file in an application, but I don't care if Adobe Reader opens separately.

+8
source share
7 answers

Create a new process:

 string path = Path.Combine(Directory.GetCurrentDirectory(), "PDF-FILE.pdf"); Process P = new Process { StartInfo = {FileName = "AcroRd32.exe", Arguments = path} }; P.Start(); 

For this to work, the PDF Studio Copy to Output Directory parameter must be set to Copy Always for the PDF file.

+7
source

Check it easy to open a PDF file from a resource.

 private void btnHelp_Click(object sender, EventArgs e) { String openPDFFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\HelpDoc.pdf";//PDF DOc name System.IO.File.WriteAllBytes(openPDFFile, global::ProjectName.Properties.Resources.resourcePdfFileName);//the resource automatically creates System.Diagnostics.Process.Start(openPDFFile); } 
+15
source

If a single PDF point needs to be opened by a PDF reader, do not paste it as a resource. Instead, copy it to a suitable location (you can place it where the EXE is located) and run it from there. It makes no sense to copy it again and again.

+2
source

"ReferenceGuide" is the name of the pdf file that I added to my resources.

 using System.IO; using System.Diagnostics; private void OpenPdfButtonClick(object sender, EventArgs e) { //Convert The resource Data into Byte[] byte[] PDF = Properties.Resources.ReferenceGuide; MemoryStream ms = new MemoryStream(PDF); //Create PDF File From Binary of resources folders helpFile.pdf FileStream f = new FileStream("helpFile.pdf", FileMode.OpenOrCreate); //Write Bytes into Our Created helpFile.pdf ms.WriteTo(f); f.Close(); ms.Close(); // Finally Show the Created PDF from resources Process.Start("helpFile.pdf"); } 
+2
source

You need to convert the resource to a format acceptable for the program that should consume your file.

One way to do this is to write the contents of the resource to a file (temporary file), and then run the program, pointing it to the file.

Whether the resource can be directly loaded into the program depends on the program. I'm not sure if this can be done using Adobe Reader.

To write the contents of a resource to a file, you can create an instance of the MemoryStream class and pass your byte array to its constructor

+1
source

This should help - I often use this code to open various executable files, documents, etc., which I put as a resource.

 private void button1_Click(object sender, EventArgs e) { string openPDFfile = @"c:\temp\pdfName.pdf"; ExtractResource("WindowsFormsApplication1.pdfName.pdf", openPDFfile); Process.Start(openPDFfile); } void ExtractResource( string resource, string path ) { Stream stream = GetType().Assembly.GetManifestResourceStream( resource ); byte[] bytes = new byte[(int)stream.Length]; stream.Read( bytes, 0, bytes.Length ); File.WriteAllBytes( path, bytes ); } 

enter image description here

+1
source
 File.Create("temp path"); File.WriteAllBytes("temp path", Resource.PDFFile) 
+1
source

Source: https://habr.com/ru/post/904344/


All Articles