How can I build a page which previews doc, docx and pdf files? I want to preview these docs side-by-side to some other data from a record.

  • Only preview functionality I'm aware of is what's available when you upload a file to Chatter. At least it displays preview of PowerPoints and PDFs for me (requires Flash to use). No idea how to salvage it though - experiment with passing different variables like file urls to the <embed>? Or check out this SO answer: stackoverflow.com/questions/1219890/flash-document-viewer – eyescream Nov 22 '12 at 22:57
  • is there any JS tools/libs that I can use? – Dedo Nov 23 '12 at 7:22
  • stackoverflow.com/questions/3539879/… points to some JS library or using Google Docs. And there is an option to enable out-of-the-box Salesforce-Google Apps integration (though I have no idea if it's also doable in prof. edition). – eyescream Nov 23 '12 at 8:01

So, I don't have an answer on displaying .doc and .docx files but you can use some of the built-in pdf viewing capabilities of modern browsers with a visualforce page like the following. Since this example uses a standardcontroller as well as an extension controller to get an attachment to display, you can even put it on a page layout for an object (in this case, Account).

This also is assuming you're using an attachment on the object which might not make sense in all circumstances...

Here's the page:

<apex:page standardController="Account" extensions="InlinePDFController" showHeader="false" sidebar="false">
    <apex:outputPanel layout="none" rendered="{!AND(attachmentExists,contentPdf)}">
        <script type="text/javascript">
            document.location.href = '/servlet/servlet.FileDownload?file={!attachmentId}';
    <apex:outputPanel layout="block" rendered="{!AND(attachmentExists,NOT(contentPdf))}">
        Attachment is not in PDF format. 
    <apex:outputPanel rendered="{!NOT(attachmentExists)}">
        No attachment found.

And the controller:

public without sharing class InlinePdfController {
    public Boolean attachmentExists {get; set; }
    public Boolean contentPdf {get; set; }
    public Id attachmentId {get; set; }

    public InlinePDFController(ApexPages.StandardController standardController) {
        Id accountId = standardController.getId();
        List<Attachment> attachments = [SELECT Id, Name, ContentType, ParentId 
                                        FROM Attachment
                                        WHERE ParentId = :accountId
                                        AND Name like '%some search term%'
                                        ORDER BY LastModifiedDate DESC];
        attachmentExists = false;
        contentPdf = false;
        if(attachments.length() > 0) {
            attachmentExists = true;
            Attachment a = attachments[0];
            if(a.ContentType != null && ((a.ContentType.toLowerCase().indexOf('pdf') > -1) || (a.Name.toLowerCase().endsWith('.pdf')))
                contentPdf = true;
            attachmentId = a.Id;
| improve this answer | |
  • 1
    Thanks Derek; I really need a solution for doc and docx files. – Dedo Nov 30 '12 at 13:18
  • Anyone knows how to achieve that? any JS lib that does that? – Dedo Nov 30 '12 at 13:18
  • @Dedo, even I am looking for something similar. Did you ever find a solution for this? – Richard N May 6 '13 at 15:53
  • No, I didn't find – Dedo May 7 '13 at 15:54

Have you looked at the Box Viewer? It works well with most MS files and pdfs.

| improve this answer | |
  • No I haven't, do you know if this viewer supports search? – Dedo Aug 22 '14 at 16:13
  • The preview is in html5 with actual text, so text searching in your browser works awesomely. – Akrikos Aug 25 '14 at 20:42
  • I got 'This site can’t be reached' – unidha May 31 '17 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.