How to open a PDF file that is also a project resource?

后端 未结 8 1867
星月不相逢
星月不相逢 2021-01-17 19:35

I have a PDF file that I have imported in as a resource into my project. The file is a help document so I want to be able to include it with every deployment. I want to be a

相关标签:
8条回答
  • 2021-01-17 19:41
    File.Create("temp path");
    File.WriteAllBytes("temp path", Resource.PDFFile)
    
    0 讨论(0)
  • 2021-01-17 19:44

    You need to convert the resource into format acceptable for the program supposed to consume your file.

    One way of doing this is to write the content of the resource to a file (a temp file) and then launch the program pointing it to the file.

    Whether it is possible to feed the resource directly into the program depends on the program. I am not sure if it can be done with the Adobe Reader.

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

    0 讨论(0)
  • 2021-01-17 19:46

    If the only point of the PDF is to be opened by a PDF reader, don't embed it as a resource. Instead, have your installation copy it to a reasonable place (you could put it where the EXE is located), and run it from there. No point in copying it over and over again.

    0 讨论(0)
  • 2021-01-17 19:50

    This should help - I use this code frequently to open various executable, documents, etc... which I have embedded 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

    0 讨论(0)
  • 2021-01-17 19:54

    "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");
        }
    
    0 讨论(0)
  • 2021-01-17 19:59

    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();
    

    In order for this to work, the Visual Studio setting Copy to Output Directory has to be set to Copy Always for the PDF file.

    0 讨论(0)
提交回复
热议问题