- Using Standalone PDF Viewer
- Using Server-Backed PDF Viewer
Contact Support
Save PDF file to Dropbox cloud file storage
5 Aug 20248 minutes to read
PDF Viewer allows to load PDF file from Drop Box using either the Standalone or Server-backed PDF Viewer. Below are the steps and a sample to demonstrate how to open a PDF from Drop Box.
Using Standalone PDF Viewer
To load a PDF file from Dropbox cloud file storage in a PDF Viewer, you can follow the steps below
Step 1 Create a Dropbox API
To create a Dropbox API App, you should follow the official documentation provided by Dropbox link. The process involves visiting the Dropbox Developer website and using their App Console to set up your API app. This app will allow you to interact with Dropbox programmatically, enabling secure access to files and data.
Step 2: Create a Simple PDF Viewer Sample in Typescript
Start by following the steps provided in this link to create a simple PDF viewer sample in Typescript. This will give you a basic setup of the PDF viewer component.
Step 3: Modify the src/app/app.ts
File in the Angular Project
- Import the required namespaces at the top of the file:
import { Dropbox } from 'dropbox';
- Configure a custom toolbar item for the download function to save a PDF file in Azure Blob Storage.
let toolItem1: CustomToolbarItemModel = {
prefixIcon: 'e-icons e-pv-download-document-icon',
id: 'download_pdf',
tooltipText: 'Download file',
align: 'right'
};
pdfviewer.toolbarSettings = { toolbarItems: [ 'OpenOption', 'PageNavigationTool', 'MagnificationTool', 'PanTool', 'SelectionTool', 'SearchOption', 'PrintOption', toolItem1, 'UndoRedoTool', 'AnnotationEditTool', 'FormDesignerEditTool', 'CommentTool', 'SubmitForm']}
pdfviewer.toolbarClick = function (args) {
if (args.item && args.item.id === 'download_pdf') {
saveDocument();
}
};
- Retrieve the PDF viewer instance and save the current PDF as a Blob. Then, read the Blob using a FileReader to convert it into an ArrayBuffer, and upload the ArrayBuffer to Drop Box using the filesUpload method of the Drop Box instance.
NOTE
Replace Your Access Token with the actual Access Token of your Drop Box account.
function saveDocument() {
pdfviewer.saveAsBlob().then(function (value) {
var reader = new FileReader();
reader.onload = async () => {
if (reader.result) {
const dbx = new Dropbox({ accessToken: 'Your Access Token' });
if(reader && reader.result){
const uint8Array = new Uint8Array(reader.result as ArrayBuffer);
dbx.filesUpload({ path: '/' + pdfviewer.fileName, contents: uint8Array })
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error);
});
}
}
};
reader.readAsArrayBuffer(value);
});
}
NOTE
The npm install dropbox package must be installed in your application to use the previous code example.
Using Server-Backed PDF Viewer
To save a PDF file to Dropbox cloud file storage, you can follow the steps below:
Step 1 Create a Dropbox API
To create a Dropbox API App, you should follow the official documentation provided by Dropbox link. The process involves visiting the Dropbox Developer website and using their App Console to set up your API app. This app will allow you to interact with Dropbox programmatically, enabling secure access to files and data.
Step 2: Create a PDF Viewer sample in TypeScript
Follow the instructions provided in this link to create a simple PDF Viewer sample in typescript. This will set up the basic structure of your PDF Viewer application.
Step 3: Modify the PdfViewerController.cs
File in the Web Service Project
-
Create a web service project in .NET Core 3.0 or above. You can refer to this link for instructions on how to create a web service project.
-
Open the
PdfViewerController.cs
file in your web service project. -
Import the required namespaces at the top of the file:
using System.IO;
using Dropbox.Api;
using Dropbox.Api.Files;
- Add the following private fields and constructor parameters to the
PdfViewerController
class, In the constructor, assign the values from the configuration to the corresponding fields
private IConfiguration _configuration;
public readonly string _accessToken;
public readonly string _folderName;
public PdfViewerController(IWebHostEnvironment hostingEnvironment, IMemoryCache cache, IConfiguration configuration)
{
_hostingEnvironment = hostingEnvironment;
_cache = cache;
_configuration = configuration;
_accessToken = _configuration.GetValue<string>("AccessToken");
_folderName = _configuration.GetValue<string>("FolderName");
}
- Modify the Download() method to save the downloaded PDF files to Dropbox cloud file storage bucket
[HttpPost("Download")]
[Microsoft.AspNetCore.Cors.EnableCors("MyPolicy")]
[Route("[controller]/Download")]
//Post action for downloading the PDF documents
public async Task<IActionResult> Download([FromBody] Dictionary<string, string> jsonObject)
{
//Initialize the PDF Viewer object with memory cache object
PdfRenderer pdfviewer = new PdfRenderer(_cache);
string documentBase = pdfviewer.GetDocumentAsBase64(jsonObject);
byte[] documentBytes = Convert.FromBase64String(documentBase.Split(",")[1]);
string documentId = jsonObject["documentId"];
string result = Path.GetFileNameWithoutExtension(documentId);
string fileName = result + "_downloaded.pdf";
using (var dropBox = new DropboxClient(_accessToken))
{
using (var stream = new MemoryStream(documentBytes))
{
// Upload the document to Dropbox
var uploadedFile = await dropBox.Files.UploadAsync(
_folderName + "/" + fileName,
WriteMode.Overwrite.Instance,
body: stream
);
}
}
return Content(documentBase);
}
- Open the
appsettings.json
file in your web service project, Add the following lines below the existing"AllowedHosts"
configuration
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"AccessToken": "Your_Dropbox_Access_Token",
"FolderName": "Your_Folder_Name"
}
NOTE
Replace Your_Dropbox_Access_Token with your actual Dropbox access token and Your_Folder_Name with your folder name.
Step 4: Set the PDF Viewer Properties in Typescript PDF viewer component
Modify the serviceUrl
property of the PDF viewer component with the accurate URL of your web service project, replacing https://localhost:44396/pdfviewer
with the actual URL of your server. Set the documentPath
property of the PDF viewer component to the desired name of the PDF file you wish to load from Dropbox cloud file storage. Ensure that you correctly pass the document name from the files available in your dropbox folder to the documentPath property.
import { PdfViewer, Toolbar, Magnification, Navigation, LinkAnnotation,ThumbnailView,
BookmarkView, TextSelection, Annotation, FormFields, FormDesigner} from '@syncfusion/ej2-pdfviewer';
PdfViewer.Inject( Toolbar,Magnification,Navigation, LinkAnnotation,ThumbnailView,
BookmarkView, TextSelection, Annotation, FormFields, FormDesigner);
let viewer: PdfViewer = new PdfViewer();
// Replace the "localhost:44396" with the actual URL of your server
viewer.serviceUrl = 'https://localhost:44396/pdfviewer';
viewer.appendTo('#pdfViewer');
viewer.load('PDF_Succinctly.pdf', null);
NOTE
The Dropbox.Api NuGet package must be installed in your application to use the previous code example.