0

How to use a link label to open a pdf in a web browser ?

I have a Windows Forms application and I am making a REST service request. The response contains the URL of PDF I want to open in a web browser. Is this possible ?

I am referencing this article : http://support.microsoft.com/kb/320478

But when I change the url to request a pdf nothing happens.

Here is my code.

UrlLink.Text = "http://testurl.com/test.pdf";
               UrlLink.Links.Remove(UrlLink.Links[0]);
               UrlLink.Links.Add(0, UrlLink.Text.Length,
               "http://testurl.com/test.pdf");

And the link label event handler:

private void UrlLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    ProcessStartInfo sInfo = new ProcessStartInfo(e.Link.LinkData.ToString());
    Process.Start(sInfo);
}
BentOnCoding
  • 27,307
  • 14
  • 64
  • 92
  • Does it work if you remove the extra speech marks that are around the URL? (i.e. UrlLink.Text = "http://testurl.com/test.pdf"; UrlLink.Links.Remove(UrlLink.Links[0]); UrlLink.Links.Add(0, UrlLink.Text.Length, "http://testurl.com/test.pdf");` Put a breakpoint on `e.Link.LinkData` and check the value of it. – keyboardP Jan 03 '12 at 05:09
  • the double quotes were from poor copy paste skills – BentOnCoding Jan 03 '12 at 05:42
  • What's the value of 'LinkData' in the LinkClicked event? – keyboardP Jan 03 '12 at 18:10

1 Answers1

0

If removing the extra speech marks doesn't work, you could try adding the link to the Description:

UrlLink.Links[0].Description = "http://testurl.com/test.pdf";

then

ProcessStartInfo sInfo = new ProcessStartInfo(e.Link.Description);
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • I think this adds the linkdata `UrlLink.Links.Add(....` , atleast [MSDN says so](http://msdn.microsoft.com/en-us/library/tk796z28.aspx) – V4Vendetta Jan 03 '12 at 05:04
  • Ah yes, it seems like that it that's the case. It might just be a case of OP putting too many speech marks around his URL. – keyboardP Jan 03 '12 at 05:13