I\'m just creating a simple calculator in C# (windows form)
I\'ve created a \"User Help\" which is a pdf file, what I want is to display that pdf file if the user cl
In this case the created Application has to run on several PC´s. To reference on a file which is not in the network, but in the Programm Folder itself, use the following code Snippet:
First of all include the following Library:
using System.IO;
Then use a Button, picturebox, or whatever to create a ClickEvent and use the following code snippet:
private void pictureBox2_Click(object sender, EventArgs e)
{
//get current folderpath of the .exe
string ProgramPath = AppDomain.CurrentDomain.BaseDirectory;
//jump back relative to the .exe-Path to the Resources Path
string FileName = string.Format("{0}Resources\\Master_Thesis_Expose.pdf", Path.GetFullPath(Path.Combine(ProgramPath, @"..\..\")));
//Open PDF
System.Diagnostics.Process.Start(@"" + FileName + "");
}
Getting the embedded file out should not be a problem at all. This is not dependent on it being .pdf format, and you can just look for a separate solution there.
For display, unless you know Acrobat or similar is installed (well, even Edge can open those files nowadays), or if you want to display the file embedded in a WinForms application, there is
Codeproject Solution
https://www.codeproject.com/Articles/37458/PDF-Viewer-Control-Without-Acrobat-Reader-Installe
written in VB relying on lots of (partially commercial, if your solution is commercial) libraries.
PdfiumViewer
https://github.com/pvginkel/PdfiumViewer
is great and also available via NuGet.
The PdfiumViewer library primarily consists out of three components:
•The PdfViewer control. This control provides a host for the PdfRenderer control and has a default toolbar with limited functionality;
•The PdfRenderer control. This control implements the raw PDF renderer. This control displays a PDF document, provides zooming and scrolling functionality and exposes methods to perform more advanced actions;
•The PdfDocument class provides access to the PDF document and wraps the Pdfium library.
It is an all-in-one solution for display and comes with a friendlier Apache 2.0 license.
edit, added sample code, for your convenience :) I included the following
data = File.ReadAllBytes(@"C:\temp\abc.pdf");
PdfiumViewer.PdfDocument doc;
using (Stream stream = new MemoryStream(data))
{
doc = PdfiumViewer.PdfDocument.Load(stream);
var viewer = new PdfiumViewer.PdfViewer();
viewer.Document = doc;
var form = new System.Windows.Forms.Form();
form.Size = new Size(600, 800);
viewer.Dock = System.Windows.Forms.DockStyle.Fill;
form.Controls.Add(viewer);
form.ShowDialog();
}
This generates a form on the fly, of course you could also use the designer.
If you want to display a pdf inside your application, the WebBrowser control is probably preferable over the Adobe Reader control, as it will open the file very smoothly in PDF Reader or whatever IE is using as a default to open pdfs. You simply add the WebBrowser control to an existing or new form and navigate to the pdf file.
Never assume that a user has Adobe or any other third party controls or libraries installed on their machines, always package them with your executable or you may have problems.
The Adobe Reader control obviously doesn't integrate as well with .NET as an intrinsic Windows component. As a rule, I always favor the use of built in .Net controls over third party vendors. As far as embedding the file in the actual executable; not going to happen until Microsoft decides any old PDF can be worked into the CLS and compiled into MSIL. What you have when you develop any app in .NET is code that can be compiled into intermediate MSIL to be translated at runtime by the CLR into native code and executed in the OS.
WebBrowser1.Navigate("SomePDF.pdf");
I would suggest converting your pdf file to a Microsoft help file, so that you don't need to have Adobe Reader installed (it's buggy, and has way too much security issues). You cannot expect users to have this.
In reply to the starter's comment:
Yes you would need to create your help file as an HTML document instead of a pdf. There is no easy way to convert pdf to HTML.
It might be possible to embed Adobe's Reader in your form as an ActiveX component. But that means you'll have to make sure Reader is installed on the client machine for that to work.
In case it doesn't have to be strictly embedded you can just launch the PDF file and let whatever viewer the user has open it.
I would put it on within my program folder, add a link within my Start Menu folder to allow a direct access (without starting my tool) and just at on some click event System.Diagnostics.Process.Start(@".\Manual.pdf");
Ok, now we come to a completely new question: How to embed a file in my application and start it?
For this question you'll find already several answers here, but here is the short version:
byte[]
from Properties.Resources.NameOfResource
With these steps you reference your file where ever it exists within your structure. If you like that a copy of your pdf file will be put into a subfolder Resources within your project, just skip the points one and two in the above list.
To get your pdf now opened, you'll have to write the byte[] down to disk (maybe with Path.GetTempFileName()
) and start it with Adobe Reader. (Don't forget to delete the file after usage)