Sunday, January 15, 2012

Displaying Images in a Visual Force Page that are stored in Document/Attachment Object


Many Times you might have ended up into a situation where you want to display an image that is stored either in Document or Attachment object. It is very easy to display an image contained in the static resource but what if you want to use Document or Attachment object instead? This article will help you to show an image into the visual force page.

The image can be displayed in the VF page by using <apex : image> tag. Image source is specified by "URL" attribute.
But the url should be in the form : " /servlet/servlet.FileDownload?file= 01590000000Itdo" where  01590000000Itdo is the the unique id of the Document/Attachment record containing the image.

Example :
I have created a sample Visual Force page along with the custom controller. For this example, I have stored an image in the Document object with name"sample pic". The code in the controller class and Visual Force is self explanatory but still I would explain some important things.

Controller :
public with sharing class ImageController {
 public String imageURL{get;set;}
  
  public ImageController()
  {
    imageURL='/servlet/servlet.FileDownload?file=';
    List< document > documentList=[select name from document where 
                                    Name='SamplePic'];
  
    if(documentList.size()>0)
    {
      imageURL=imageURL+documentList[0].id;
    }
  }
}
  • Here I have taken a String variable declared as public. I am initializing this variable in controller with  "servlet/servlet.FileDownload?file=" value. This variable will be used in VF page as a URL parameter in <apex : image> tag. 
  • The controller fetches the document record from the Document object based on the "name" field specified in the SOQL query.
  • The only thing remaining in the imageURL is the unique id of document record. So after fetching record from the object, append "id" to the imageURL variable. 
Thats it. We are done with the controller class. Now the next step is to design the Visual Force page to display an image.

Visual Force Page :


    
      
    




  • Here we are binding the imageURL ( String variable of controller class ) to the url attribute of <apex : image> tag. 
So finally now our Visual Force page is complete and ready to show an image.

I hope this article would be useful for you to solve your problem to display an image from Document / Attachment Objects.

26 comments:

  1. Beautiful !!! Could you please post the VF page code again? I don't see it there...Thx

    ReplyDelete
    Replies
    1. Thanks for the comment!
      Here is the code for VF page. If you are still not getting code, let me know your email id, I will mail you the code.

      < apex:page controller="ImageController" showheader="false" sidebar="false" >

      < apex:form >
      < apex:image url="{!imageURL}" >
      < /apex:image >
      < /apex:form >

      < /apex:page >

      Delete
    2. Hi Dhawal,
      It was also good examples for displaying attachments on visualforce Page.
      Thanks for your nice post.

      Delete
    3. Thanks Amit Vaidya! :) I am glad that it helped you.

      Delete
  2. @Dhawal Saiya. Can you please tell me how to show the image from a particular hard coded url to visualforce page that is rendered in pdf? for instance:




    its working before rendered the page into pdf but its doesn't show an image when i renderedin pdf.. i didn't want to include in document or resources. i have to get images dynmically from diff url's like above. please help ! Thanks

    ReplyDelete
    Replies
    1. Hi Murtaza,

      Thanks for the comment.
      To display external image in VF page which is rendered as PDF, just add external site's link to remote site settings (Setup | Security Controls | Remote Site Settings).

      After it is added to Remote Site Settings, your images will be displayed in PDF.

      Delete
  3. Hi,

    Since the id is unique in the link, wont we face a problem when moving the code to production. That time the Id of the object will change right?

    Thanks,
    Calvin

    ReplyDelete
    Replies
    1. Hi Calvin,

      Thanks for the comment. Yes ID is unique and is dependent on org. But since we are fetching id of the record in controller through soql query, So moving this code (VF page with controller) to other org will not cause any problem.

      Thanks
      Dhawal

      Delete
  4. Hai

    how to display the pop up from the image

    ReplyDelete
    Replies
    1. You can use jQuery plugins to display popups.

      Delete
  5. how to Master- Detail records render as pdf in vf page...?

    plz provide code for this qstn to this mail id

    giri.arni@gmail.com

    ReplyDelete
  6. Hi Dhawal, Thank you. I am facing same problem to get the images from Documents to product obj. I want to display the Images automatically inside the product obj based on the product name. here product name and document image name both are same. I am using Professional Edition. So in PE we cannot write any apex code.If any idea to get the image by using Visualforce page with scripting. Help me to solve my problem. Send any solution to my mail id: dhanunjaya520@gmail.com

    ReplyDelete
  7. Hi its excellent example but i want to add 10-50 images on my VF page what will be changes have to be do in VF page as well as controller???

    ReplyDelete
    Replies
    1. Thanks Prachi. In order to display variable number of images on VF page, you will have to make following changes in VF and controller:
      Controller:
      - Create list of Attachment/Document
      VF Page:
      - By using < apex:repeat > tag, iterate on list and display image.

      Let me know if you did not get anything and want me to explain further

      Delete
    2. In my PC image is there at E:/abc.jpg how to upload that image in visualforce page that in PDF i am using renderAs="pdf" Here I want that image in my pdf page

      please I am new to SFDC please send code to my email id
      ""bhaskarchowdary1.1.87@gmail.com""

      Delete
  8. how to display image on visualforce page which is store in static resources ???

    ReplyDelete
  9. i'm trying to use static resource image into my visualforce page but it is not showing up there by this command......


    please reply me on nitinkhunal@gmail.com

    ReplyDelete
  10. Hi

    I need to show pdf file in VF page. which was coming by document object record.
    Ex.
    Document doc = [select id,Name from document where name ='test'];

    its returning one record which was pdf constant. those constant i need to show in vf page. is it possible?

    Thnaks
    Mitul.

    ReplyDelete
  11. Please send me some basic to expert level example related to all salesforce those are used in project.
    as i new in salesforce.com
    my email id:pushk_8888@yahoo.co.in
    Its help me lot.
    Thanks in advance.

    ReplyDelete
  12. Really helpful and worked first time. Thanks.

    ReplyDelete
  13. I have attached a zip and trying to access it content from the page. I am getting cross browser accessibility error.

    ReplyDelete
  14. I'm not getting my jpeg or png image in the VF with this ... It just gives the image symbol on the page but no image

    ReplyDelete
  15. Hi, first of all thanks for your post. I am trying to use the above method to display an image from document, but rather than showing an image, it shows small cross sign. Seems like its not picking up the URL or something else is wrong?

    ReplyDelete
  16. Morning
    I want to ask you a question sir
    I will make an Android app, I use salesforce too
    how to send/upload image to salesforce? and how to display the picture in my app
    do you have a tutorial for it?

    thank you

    ReplyDelete
  17. how to display image for a single record..images are stored in documents

    ReplyDelete