Find your content:

Search form

You are here

Document preview with Visualforce


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.

Attribution to: Dedo

Possible Suggestion/Solution #1

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;

Attribution to: DerekLansing

Possible Suggestion/Solution #2

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

Attribution to: Akrikos
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content