XtremeDocumentStudio
.NET
PDFOne
.NET
XtremeDocumentStudio
(for Java)
PDFOne
(for Java)
XtremeDocumentStudio
Delphi
eDocEngine
VCL
PDFtoolkit
VCL
StarDocs
Web APIs

Using StarDocs with Delphi and IntraWeb

Creating a multiformat document viewer with Web APIs using Delphi and IntraWeb
by Santosh Patil
If you are new to StarDocs, we suggest you read the introductory article and the getting started article first. This article builds on the steps explained in those foundational articles to avoid repetition.

The API reference documentation can be found here.

IntraWeb which is bundled with Delphi contains powerful components and controls making it easy to develop web applications. IntraWeb is designed to leverage your native Delphi development skills and take them to the web. In this article we'll show you how to create a simple web application to view multiformat documents stored on the server using Gnostice StarDocs APIs. The demo will simulate how a banking application can enable customers to view their statements online.

This article uses the Delphi 10.1 Berlin IDE with bundled IntraWeb XIV. However you should be able to follow it for earlier versions of Delphi as well down to Delphi XE7 (StarDocs SDK is supported only from XE7 onward).

Here is a short videos covering the topic if you'd prefer watching over reading! However we suggest you also read the article.

  1. In the IDE choose File > New > Other. In the dialog that opens choose Delphi Project > IntraWeb > IntraWeb Application Wizard and click OK.
  2. In the IntraWeb Application Wizard dialog choose Application Type as StandAlone Application, type in project name (say DocumentViewer) and click Ok
  3. If a popup asks about enabling "Visual Component Library" then click on Yes
  4. Open the design view of Unit1.pas. Place the following controls on the form: TIWLabel, TIWListbox, TIWButton and TIWURLWindow.
  5. Change the caption of IWLabel1 to "Choose a statement and click on View button". Change the caption of IWButton1 to "View".
  6. Appropriately size, position and align the controls to match the screenshot shown below
  7. Anchor IWLabel1 to Top, Left. Anchor IWListbox1 to Top, Left. Anchor IWButton1 to Top, Left. Anchor IWURLWindow1 to all the sides.
  8. Let's authenticate to the StarDocs server when our newly created server starts up. Add the following code to
    ServerController.pas
    file.
  9. // ...
    uses ..., gtxStarDocsSDK;
    
    // ...
    type
      TIWServerController = class(TIWServerControllerBase)
        //...
        procedure IWServerControllerBaseCreate(Sender: TObject);
        //...
    
    public
      StarDocs: TgtStarDocsSDK;
    
    // ...
    procedure TIWServerController.IWServerControllerBaseCreate(
      Sender: TObject);
    begin
      // Set up connection details
      StarDocs := TgtStarDocsSDK.Create(nil);
      StarDocs.ConnectionInfo.ApiServerUri.URI :=
        'http://api.gnostice.com/stardocs/v1';
      StarDocs.ConnectionInfo.ApiKey := '<API Key>';
      StarDocs.ConnectionInfo.ApiSecret := '<API Secret>';
      // Force full permissions on PDF files protected 
      // with an permissions/owner/master password
      StarDocs.Preferences.DocPasswordSettings.ForceFullPermission := True;
    
      // Authenticate
      StarDocs.Auth.loginApp;
    end;
    
  10. Next let's populate the list box with some sample statements when IWForm1 is created. The code for that is shown below.
  11. // type
    //  TIWForm1 = class(TIWAppForm)
      private
        SelectedFile: string;
    // end;
    
    // ...
    
    uses ServerController;
    
    procedure TIWForm1.IWAppFormCreate(Sender: TObject);
    var
      DocsPath: string;
    begin
      DocsPath := ServerController.IWServerController.ContentPath + 'docs\';
      IWListbox1.Items.AddPair('Bank Statement #1', DocsPath + 'statement1.pdf');
      IWListbox1.Items.AddPair('Bank Statement #2', DocsPath + 'statement2.pdf');
      IWListbox1.Items.AddPair('Bank Statement #3', DocsPath + 'statement3.pdf');
      SelectedFile := '-1';
    end;
    
  12. ServerController.IWServerController.ContentPath
    points to the server deployment home folder (wwwroot). In the development environment this will be
    <debug-folder>\wwwroot
    or
    <release-folder>\wwwroot
    based on the build configuration. Create a folder called docs there and place a few PDF files under it titled 'statement1.pdf', 'statement2.pdf' and 'statement3.pdf'.
  13. Now we will set up the listbox change handler to record the selection. The code for that is shown below.
  14. procedure TIWForm1.IWListbox1AsyncChange(Sender: TObject;
      EventParams: TStringList);
    begin
      SelectedFile := EventParams.ValueFromIndex[0];
    end;
    
  15. Next let's setup the View button click handler to load and display the statements. Add the following code to do that.
  16. uses gtxStarDocsSDK;
    
    // ...
    
    procedure TIWForm1.IWButton1Click(Sender: TObject);
    var
      StarDocs: TgtStarDocsSDK; // A copy, so don't free it here!
      DocObject: TgtDocObject;
      ViewResponse: TgtViewResponse;
    begin
      DocObject := nil;
      ViewResponse := nil;
      if SelectedFile <> '-1' then
      begin
        DocObject := nil;
        ViewResponse := nil;
        try
          // Load file in viewer
          StarDocs := ServerController.IWServerController.StarDocs;
          // Upload file
          DocObject := StarDocs.Storage.Upload(SelectedFile , '');
          ViewResponse := StarDocs.Viewer.CreateView(DocObject, '');
          // Launch browser pointing to the view url
          IWURLWindow1.URI := ViewResponse.Url;
        finally
          if Assigned(ViewResponse) then
            FreeAndNil(ViewResponse);
          if Assigned(DocObject) then
            FreeAndNil(DocObject);
        end;
      end;
    end;
    
  17. That's it! Build and run the application. IntraWeb will show a launcher. You can click on any of the buttons representing different browsers on the launcher's toolbar to open the page of your web application. We have shown below the demo app in action.
  18. Note that the viewer can be customized as per your need. For this please see our related article Viewing PDF and image files in your web apps using Web APIs.

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2017 Gnostice Information Technologies Private Limited. All rights reserved.