Question

How do I open a pdf document stored in Sitecore when a user clicks a link? The pdf document is stored in the Media Library. Here's the code I have now:

Sitecore.Data.Fields.LinkField linkField = item.Fields["Url"];
tab.NavigateUrl = linkField.Url;
Was it helpful?

Solution

Same rules apply to a PDF media as to any other type in media library. If you need just to retrieve the media url to construct a link, do the following:

MediaItem mediaItem = linkField.TargetItem;
if(mediaItem != null)
MediaManager.GetMediaUrl(mediaItem);

You can also simply use web control or xsl control to render the link: Web control:

<sc:Link Field="Url" Item="if you need to process specific item" runat="server" />

If your question concerns browser behavior when the link is clicked, set ForceDownload to true:

<mediaType name="PDF file" extensions="pdf">
  <mimeType>application/pdf</mimeType>
  **<forceDownload>true</forceDownload>**
  <sharedTemplate>system/media/unversioned/pdf</sharedTemplate>
  <versionedTemplate>system/media/versioned/pdf</versionedTemplate>
</mediaType>

OTHER TIPS

Be aware of problems with Firefox on a Mac, as PDFs wont seem to work there.

For that you need to setup some stuff according to this.

if you can't access that is basically tells you to go to the web.config, find this:

<mediaType name="PDF file" extensions="pdf">

and change the

<forceDownload>false</forceDownload>

to

<forceDownload>true</forceDownload>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top