PDFViewer Documentation
Overview
Welcome to the official documentation for the PDFViewer Unity Asset. This document provides a comprehensive guide on how to integrate and use PDFViewer within your Unity projects.
PDFViewer is an easy-to-use solution for displaying PDF files directly within a Unity application. It supports viewing local and remote PDF files.
Platforms Supported:
- Windows
- Linux
- Android (has 16KB page support)
- iOS
Note: Android and iOS have not been tested on app stores.
Prerequisites
To use the PDFViewer asset, ensure your project meets the following requirements:
- Unity Version: Unity 2021.3 LTS or higher (though may work on older versions)
Installation
- Purchase and download the PDFViewer asset from the Unity Asset Store.
- In your Unity project, navigate to
My Assets→ Search and Select PDFViewer - Import all files.
- After the import process is complete, you will find the asset files in the
Assets/PDFViewerfolder.
Getting Started
1. Adding the Viewer to a Scene
The PDFViewer comes with a prefab that is ready to use.
- Open the desired scene in your Unity project.
- Navigate to the
Assets/PDFViewerfolder. - Drag and drop the PDFViewer prefab into your canvas.
- Position and scale the viewer component to fit your UI layout.
2. Basic Usage - Loading a PDF
To load a PDF, you will primarily interact with the PDFViewer.cs script, which is attached to the prefab.
- Select your file source (Streaming Assets, Persistent data path, WebAddress) and enter the path to the asset.
- For Streaming Assets and Persistent data path, use a relative path to the asset. For example, if you have your pdf within
StreamingAssets/pdfs/my-pdf.pdfthen simply use"pdfs/my-pdf.pdf"in the pdfFilePath. - For web addresses, please include the entire web address. If the
https://is excluded, it will automatically assume https rather than http and add it to the start. - For none, any full address can be used, this includes web addresses.
PDFViewer Component Properties
The PDFViewer.cs script exposes several configurable properties in the Inspector:
| Property | Type | Description |
|---|---|---|
fileSource |
enum | The fileSource of the path you enter in pdfFilePath. Using None requires you to enter the full path. |
pdfFilePath |
string | The relative or full file path for the pdf dependent on the fileSource selected. |
loadOnStart |
bool | Whether to load the pdf on start. |
dpi |
int | The dpi to render the pdf at. |
withAlpha |
bool | Whether to render alpha. |
flipY |
bool | Whether to flip the texture on the Y axis. |
unPremultiply |
bool | Whether to unPremultiply whilst rendering the pdf. |
maxDimension |
float | The maximum dimension/size either on the X or Y for the page of the pdf. |
zoomIncrement |
float | The increment that ZoomIn & ZoomOut use. |
minZoom |
float | The minimum zoom. |
maxZoom |
float | The maximum zoom. |
scrollSensitivity |
float | The scroll sensitivity. |
touchZoomSensitivity |
float | The sensitivity of the zoom whilst using touch. |
PDFViewer Public Methods
You can control the PDF Viewer using the public API functions:
| Function | Description |
|---|---|
LoadPDF() |
Loads the pdf based on the current fileSource and pdfFilePath. |
LoadPDF(string inFilePath, FileSource inFileSource) |
Loads the pdf based on the inputted filepath and filesource. |
LoadPDF(byte[] pdfBytes) |
Loads the pdf directly from the byte array given. |
DisplayPDF(List<Texture2D> pageTextures) |
Display the list of textures within the viewer. |
ZoomIn() |
Zoom in by the zoomIncrement. |
ZoomOut() |
Zoom out by the zoomIncrement. |
Zoom(float increment) |
Zoom by your own inputted increment. |
Events
The viewer provides two events. You can subscribe to these events via script or in the Inspector.
- OnLoadComplete: Triggered after the PDF is successfully loaded.
- OnError: Triggered if loading or rendering fails.
Advanced Usage
PDFTextureLoader
This class provides two methods to load a single page of a PDF, the difference between the class is one being synchronous and the other being asynchronous.
Both return a Texture2D. To call these methods see below (replace variables with your own values/variables of the correct type):
Texture2D texture = PDFTextureLoader.LoadPageTexture(pdfBytes, pageNumber, dpi, withAlpha, flipY, unPremultiply);
Texture2D texture = await PDFTextureLoader.LoadTextureAsync(pdfBytes, pageNumber, dpi, withAlpha, flipY, unPremultiply);
For usage of the LoadTextureAsync function, please see the PDFViewer class.
FileLoader
This class provides two methods to load the pdfFile to return a byte[] array which is needed for the PDFTextureLoader. See below:
byte[] pdfBytes = await FileLoader.LoadByteArrayAsync(fullpath);
FileLoader.LoadByteArray(fullpath, pdfBytes =>
{
// Do some code in here with pdfBytes
});
For usage of the LoadByteArrayAsync, please see the PDFViewer class.
Known Limitations
- No macOS Support
- No WebGL Support
- Unknown iOS app store usability
Support and Feedback
If you encounter any issues or have questions regarding the PDFViewer asset, please contact the developer at contact@coleosborne.com.