开发者

Opening a pdf in .NET

开发者 https://www.devze.com 2022-12-09 20:09 出处:网络
In the application we are working on we are trying to implement Help.We have a help pdf document and for the moment it has been deemed acceptable that when users click the help button it just opens th

In the application we are working on we are trying to implement Help. We have a help pdf document and for the moment it has been deemed acceptable that when users click the help button it just opens the pdf. The application is a desktop app and the pdf file needs to be included in the install somehow and installed on the local machine.

I essentially need two things:

  1. How would I add this pdf to the application so that it is available on the client machine after install? I can either put it in the resources of the project, or else I could put it in the Application Folder of the client setup installer.

  2. How would I open this pdf? I can either open this pdf by just launching the default pdf viewer for the file, or I can use the System开发者_开发知识库.WinForms.WebBrowser tool to display it. However if I choose the second option, I am unsure of how to access the file stored in the resources or find it in the install folder.

If you could please provide me with the code to do this (either in VB.Net or C#.Net) that would be great.


This is how we did it:

Put all the help folders in %ApplicationFolder%/Help. So, your pdf will have the path %ApplicationFolder%/Help/Manual.pdf. You can create a Help folder and put in your pdfs at your csproject, and attach the whole whole to the csproject, and set it to copy always. In this way you can make sure that the pdfs you copy to the Help folder are always the latest.

And copy everything ( minus the pdb files, of course) from your debug folder to your deploy folder.

Then, use System.Diagnostics.Process.Start(pdfPath) to start the help file when the help button is clicked. Here's how you can get the pdfPath:

Here's the code snippet:

public void Help_Click(object sender, System.EventArgs e)
{
    var appDir = Path.GetDirectoryName(Application.StartupPath);
    var helDir = Path.Combine(appDir, "Help");
    var pdfPath = Path.Combine(helDir, "Manual.pdf");

    System.Diagnostics.Process.Start(pdfPath);
}

You can test this in debug mode. Just press F5 ( remember to copy the pdf file to the debug folder) and you can get it


  1. IMHO i would be better to store your pdf in application folder, becouse it's easier to access. Help file name may be hardcoded, so there are no problem to find it.

  2. I prefer to use pdf in .NET through ActiveX, without WebBrowser. It's easy approach and the result is quite useable - you will get complete Acrobat toolbar and behaviour which can be easily integrated with your interface. To do so, you should create separate form and put AxAcroPDF control on it, then use AxAcroPDF.LoadFile. Of course, you will need to reference AxAcroPDFLib. Control could be initialized like this:

    var axAcroPDF = new AxAcroPDFLib.AxAcroPDF();
    axAcroPDF.Dock = System.Windows.Forms.DockStyle.Fill;
    axAcroPDF.Enabled = true;
    axAcroPDF.Name = "axAcroPDF1";
    axAcroPDF.LoadFile(fullFilepath);
    axAcroPDF1.Visible = true;
    

ps Any solution would require an installed Adobe Acrobat on the client machine.


For your first need, I would find it easiest to just add the PDF to the Visual Studio project and set its "Copy to Output Directory" to "Copy if newer." As for how to open the file, I'd recommend using the default PDF viewer. Using the .NET Process class, you can just launch the file name using:

System.Diagnostics.Process.Start(fullFilePath);


  1. How would I add this pdf to the application so that it is available on the client machine after install? I can either put it in the resources of the project, or else I could put it in the Application Folder of the client setup installer.

I would go for the second option, just because it's easier to manipulate a file than an embedded resource...

  1. How would I open this pdf? I can either open this pdf by just launching the default pdf viewer for the file, or I can use the System.WinForms.WebBrowser tool to display it. However if I choose the second option, I am unsure of how to access the file stored in the resources or find it in the install folder.

The web browser is not a very good option IMHO... not all PDF readers provide a good browser plugin, and anyway it will probably be more comfortable for the user to read the document in the standalone reader application


i am not a very experienced programmer but i will suggest "Application Folder" as suggested by "Thomas" plus if you are worried about if the client system might not have a default PDF viewer you might try to implement "TRichEditPDF 1.01" / "TRichView llPDFLib 1.0" or "PDFsharp 1.30". don't know if it is very much practical or not but its an option.

0

精彩评论

暂无评论...
验证码 换一张
取 消