kirk.burleson
kirk.burleson

Reputation: 1251

How Do I Open A PDF In Sitecore

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;

Upvotes: 1

Views: 5982

Answers (2)

Holger
Holger

Reputation: 2271

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>

Upvotes: 0

Alex Shyba
Alex Shyba

Reputation: 1262

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>

Upvotes: 5

Related Questions