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.)
- Customize your form with PowerApps, and make sure you’ve added the “Attachments” card to your form control.
- Unlock the card where the attachments are. Name the attachment control attachmentBox.
- For the OnAddFile property of the attachmentBox, use this function:
ClearCollect (colAttachments, attachmentBox.Attachments)
- 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:
- Add a new gallery. Name it galAttachments
For the Items property of galAttachments, type colAttachments
- For the OnSelect property of galAttachments, set it to this:
- Put a label in the gallery, to show the file name, and set the Text property to ThisItem.Name
- 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
- Insert a PDF Viewer control, right on top and the same size as recBehindImage. Call it pdfViewFile.
- For the Document property of pdfViewFile, use this:
- 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
- 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.
- 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
- Finally, for the OnSelect property of icoCancel, it should be
That will make the icon, rectangle, and image disappear when you click the X.
- 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“)
- 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
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!