Viewing PDF in Windows Forms Using C#

Viewing PDF in Windows forms using C#

you can use System.Diagnostics.Process.Start as well as WIN32 ShellExecute function by means of interop, for opening PDF files using the default viewer:

System.Diagnostics.Process.Start("SOMEAPP.EXE","Path/SomeFile.Ext");

[System.Runtime.InteropServices.DllImport("shell32. dll")]
private static extern long ShellExecute(Int32 hWnd, string lpOperation,
string lpFile, string lpParameters,
string lpDirectory, long nShowCmd);

Another approach is to place a WebBrowser Control into your Form and then use the Navigate method for opening the PDF file:

ThewebBrowserControl.Navigate(@"c:\the_file.pdf");

View PDF File using Windows Form C# from Remote URL

Check this out. I'm using the PDF Viewer control by DevExpress to display PDF documents within the app.

using System.Net;

string Url = "YOUR_URL";
using(WebClient client = new WebClient()) {
using(MemoryStream ms = new MemoryStream(client.DownloadData(Url))) {
pdfViewer1.LoadDocument(ms);
}
}

Opening a .pdf file in windows form through a button click

To open a file with a system default viewer you need call

System.Diagnostics.Process.Start(filename);

But I haven't understood the problem with a filepath.
If you need a relative path from the program .exe file to a folder with resources, then you can add "Resources\" or "..\Resources\" (if Resources folder is higher) to your filepath.

Or you can add your pdf to a project as an embedded resource and then, when you need to open it, you can save it to some temporal location using

Path.GetTempPath()

and open it.

Display a PDF in winforms

ITextSharp allows you to create and manipulate pdf's, but does not provide any rendering options like Bradley Smith said in a comment above

I did something similar a long time ago and what I ended up doing was using Ghostscript to generate a tiff image from my pdf and displaying that instead. Obviously that just displays an image so if you need to edit the pdf, this won't work for you.

Ghostscript is command line only so I think you have to run it something like this:

       Process.Start(@"c:\gs\gs.exe",
String.Format("-o {0} -sDEVICE=tiffg4 -dFirstPage={1} -dLastPage={2} {3}", "tiffPages.tif", fromPage, toPage, "inputfile.pdf"));

Displaying a pdf file from Winform

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

Update

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:

  1. Right click your project and select Add - Existing Item
  2. Select your file (don't double click it)

    • Click the little arrow next to the Add button and select Add As Link
  3. Double click on Properties - Resources.resx
  4. Click the little arrow next to Add Resource and select Add Existing File
  5. Select the same file again in the open dialog
  6. Now you can access the file within your code as 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)

open Pdf document in C# in Window application

How you want to open the created PDF ? Just open the PDF file separately or open within windows form ?

Just to Open PDF file

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
process.StartInfo = startInfo;

startInfo.FileName = @"C:\Users\user\Desktop\eBook.pdf";
process.Start();

Load PDF within windows form

You can use WebBrowser control to load PDF by passing file path to Navigate()

private void OpenPdf(string filePath)
{
if (!string.IsNullOrWhiteSpace(filePath))
{
webBrowser1.Navigate(@filePath);
}
}


Related Topics



Leave a reply



Submit