Power Apps PDF Viewer

There is a control in PowerApps called the PDF Viewer.  This lets you preview PDF files.  In my last blog post, PowerApps Gallery of Attachments I showed how to preview image attachments in a gallery.  Today in SharePoint Power Hour, I did a demo of that solution, and also figured out a way to do previews of PDF files as well.  Although the PDF viewer control cannot be placed inside of a gallery, we can still set it up so that as you click the name of each attachment, it shows the big preview in the middle of the screen.  Here’s how to do it with the PDF Viewer.  Some of these instructions are the same as the ones in my previous post about the image gallery.

(Note that this same thing cannot currently be done with Word or Excel files, there is no viewer for them.  You can just directly link to them, which would open them in another window.)

  1. Customize your form with PowerApps, and make sure you’ve added the “Attachments” card to your form control.
  2. Unlock the card where the attachments are.  Name the attachment control attachmentBox.
  3. For the OnAddFile property of the attachmentBox, use this function:
    ClearCollect
    (colAttachments, attachmentBox.Attachments)
  4. Go to the OnRemoveFile property and do the same thing.  This function grabs all the attachments and puts them in a collection.  Also, go to the SharePointIntegration property (bottom left), and add this function to the OnNew, OnEdit, and OnView properties.
    If you’re doing this on a standalone app (not a customized list form), put this function in the first screen’s OnStart property.
    For the OnSave and OnCancel properties, add this function:
    Clear(colAttachments)

  5. Add a new gallery.  Name it galAttachments
    For the Items property of galAttachments, type colAttachments
  6. For the OnSelect property of galAttachments, set it to this:
    Set(varShowBigImage,true)
  7. Put a label in the gallery, to show the file name, and set the Text property to ThisItem.Name
  8. Insert a large rectangle right in the middle of your form, you can cover most of it up except for the gallery you just added.  You want to be able to click through the items.  Call it recBehindImage
  9. Insert a PDF Viewer control, right on top and the same size as recBehindImage.  Call it pdfViewFile.
  10. For the Document property of pdfViewFile, use this:
    galAttachments.Selected.Value
    image
  11. Insert an icon that is an X, and put it at the top right corner in front of your PDF viewer control.  Call it icoCancel
  12. Now, it’s time to make those show and hide when you open and close images.  A variable is used for this, I call it varShowBigImage.  Go to the SharePointIntegration control, and add this to the OnEdit and OnView properties.  Basically, as soon as the form is opened, we want to set the variable to false.
    Set (   varShowBigImage,     false )
    Here’s an example of what the NewForm property will look like now. The EditForm property is almost identical, except instead of NewForm, it’s the EditForm command at the beginning.
  13. To make the controls show and hide, change the Visible property of icoCancel, pdfViewFile, and recBehindImage (you can select them all at once):
    Visible property = varShowBigImage
  14. Finally, for the OnSelect property of icoCancel, it should be
    Set(varShowBigImage, false)
    That will make the icon, rectangle, and image disappear when you click the X.
  15. Now, you’ll want the PDF viewer to only show if a PDF file is clicked, especially if you’re using this in conjunction with my previous post, for showing images as you click each one.  There needs to be logic to determine the file type.  Unfortunately, the attachments control does not provide us with the attachment type, such as whether it’s an image.  There could be lots of different image file types, like PNG, JPG, JPEG, etc.  I did figure out how to look at the file extension, so here’s how to show the PDF viewer only if a PDF file is clicked.
    For the pdfViewFile control, set the Visible property to this:
    varShowBigImage  &&  EndsWith( galAttachments.Selected.Name, “pdf“)
    image
  16. If you’ve combined this solution with the one from my previous post, then on the imgBigAttach control, set the Visible property to this.  The exclamation mark means the opposite.  So it will show this control if the selected item is NOT a PDF.
    varShowBigImage  &&  !EndsWith( galAttachments.Selected.Name, “pdf“)

Have fun trying out the attachment control and the PDF viewer!

Are you brand new to PowerApps, and interested in getting started learning some fundamentals?  Try out my

FREE Power Apps Basics Course

Here is the full video of SharePoint Power Hour, where I demonstrate this gallery of image attachments, PDF attachments, and make a few more discoveries!

9 comments

  • HI Laura
    This is great, but not a lot of use if you cannot create a PDF file in the first place. You cannot create a pdf file with PowerApps and Flow does not allow you to create one either, unless you use One Drive for Business which is not a lot of use if you have an enterprise PowerApp. I dont know why Microsoft does not allow PowerApps to create a pdf file or this a licencing thing with Adobe ?

    Regards

    Nigel

  • Inspiring post. Why do you connect to a collection instead of directly loading the attachments from your attachmentBox?
    for galAttachments: Items = attachmentBox.Attachments

    And when you have a pdf file in your gallery, I like to show a dummy icon of a pdf file instead of the blank
    for ImgAttached: If(EndsWith(ThisItem.Name;”pdf”);icon_pdf;Text(ThisItem.Value))
    where icon_pdf is the name of the uploaded pdf icon

  • Hi,good to see you!
    I found 2 small bug here, and I haven’t figure out yet. It is the ‘secondary load’, it means that when you ‘onview’ this item in the sharepoint list, it can’t load the gallery in which we are going to click to show the pdf file in the first time. The strange thing is when I click ‘x’ to exit the item and enter the item again, well, the pdf shows.
    And the second bug is we can’t automatically refresh the pdf viewer when the attachments is little bigger(around 1M to 2M). It means it take times to load it.

  • Carlos A Roman Roman

    Hi, Laura. I implemented you PDF viewer solution in one PowerApps I designed for my office. However, recently the PDF viewer is shwoing a message saying the PDF cannot be shown and to ensure the link to the PDF came from an HTTPS. When searching on Google I found a document from MS which shows that the DOCUMENT property must have the URL link to the PDF enclosed in quotes. This appear to be a change on how the PDF Viewer works. No I cannot see the PDF files on the viewer using your implementation. Do you have any suggestions on how to see the PDF file on the viewer?

  • Hi Laura, I found a problem with PDF viewer, it shows a Word file but the resolution is bad, could you help me with that?

Leave a Reply