Some test text!

menu
search
Connect to serverkeyboard_arrow_down

Realtime collaboration server

A server can be setup to be the backend that hosts all your files and annotations to use realtime collaboration with PDFTron SDK.

Realtime collaboration basics

This tutorial only applies to Xamarin.Android.

linkHost annotations in your own server

In realtime collaboration, a server will merely act as an online database that triggers events upon data creation/modification/deletion. As long as the above requirement is met, your server can be built in any language and stack of your choice. PDFTron provides an interface that will allow you to connect your data to the out-of-box UI.

  1. Your will need to have a class that implements PDFTron's CustomService interface, it will contain an abstract method sendAnnotation for you to implement, for example:

    public class AnnotationService : Java.Lang.Object, ICustomService
    {
    
        private CollabDatabase mDatabase;
    
        public AnnotationService(Context applicationContext)
        {
            mDatabase = CollabDatabase.GetInstance(applicationContext);
        }
    
        public void SendAnnotation(string action, IList<AnnotationEntity> annotations,
                string documentId, string userName)
        {
            // this represents local annotation change
            // send annotation information here to your server
        }
    }
  2. Annotations on the client side is represented by an AnnotationEntity object, it is required that your server data contains the following mandatory fields:

    • Unique identifier of the annotation
    • Unique identifier of the document that the annotation is in
    • Unique identifier of the author of the annotation
    • XFDF string of the annotation
    • (Optional) author name

    Once you have the above, a typical AnnotationEntity construction will look like:

    private AnnotationEntity CreateAnnotationEntity(string annotId, string documentId, string authorId, string userName, string xfdf) {
        var annotationEntity = new AnnotationEntity();
        annotationEntity.Id = annotId;
        annotationEntity.DocumentId = documentId;
        annotationEntity.AuthorId = authorId;
        annotationEntity.AuthorName = userName;
        annotationEntity.Xfdf = xfdf;
        XfdfUtils.FillAnnotationEntity(annotationEntity);
        return annotationEntity;
    }
  3. When data creation/modification/deletion events come in, you will construct the data into an AnnotationEntity object per annotation and add it to the local data structure. Note that all operations need to happen in the background thread (the following methods exist in pdftron.Collab.WebViewerServer.CustomServiceUtils class):

    MethodDescription
    addUser(mDatabase, authorId, authorName)First, add the current user
    addDocument(mDatabase, documentId)Then, add the document that the collaboration is happening on
    addAnnotations(mDatabase, annotationEntityMap)Call this when your server notifies you of the initial batch of annotations that already exist on the document
    addAnnotation(mDatabase, annotationEntity)Call this when your server notifies you of created annotation
    modifyAnnotation(mDatabase, annotationEntity)Call this when your server notifies you of modified annotation
    deleteAnnotation(mDatabase, annotId)Call this when your server notifies you of deleted annotation
    cleanup(mDatabase)Call this when the session finishes
  4. In the same Activity where you added the viewer, add:

    private AnnotationService mAnnotationService;
    private CollabViewerTabHostFragment mCollabViewerTabHostFragment;
    
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        
        mAnnotationService = new AnnotationService(this.ApplicationContext);
    
        DocumentViewModel documentViewModel = (DocumentViewModel) ViewModelProviders.Of(this).Get(Java.Lang.Class.FromType(typeof(DocumentViewModel)));
        documentViewModel.SetCustomConnection(mAnnotationService);
    
        CreateViewerFragment();
        mCollabViewerTabHostFragment.TabDocumentLoaded += (sender, e) =>
        {
            HandleTabDocumentLoaded();
        };
    }
    
    public void CreateViewerFragment() {
        // Create your CollabViewerTabHostFragment host fragment
        // and instantiate mCollabViewerTabHostFragment
    }
    
    void HandleTabDocumentLoaded() {
        // Start your server connection here
    }

The annotations appearing on the document in real time are managed through a separate XFDF format, while the original version of the document is preserved on the server. Note that the client downloads the original untouched document at the start of the collaboration session.

And that is it! PDFTron will handle all annotation syncing and annotation reply for you.

See a detailed tutorial on an example of how to use Firebase as backend here: How to Build an Android Real-time Document Collaboration App with Firebase.

linkHost annotations in your WebViewer Server

If you are using WebViewer Server as backend, PDFTron provides a utility add-on package that will have realtime collaboration working out-of-box with your WebViewer Server. The following tutorial will go through how to add and use this package.

linkPrerequisites

Insall:

NuGet for Collab-WVS

linkConnect to the server

In the same Activity where you added CollabViewerTabHostFragment, add the following:

private BlackBoxConnection mBlackBoxConnection;
private CollabViewerTabHostFragment mCollabViewerTabHostFragment;
private string WVS_ROOT = "MY_WVS_ROOT_ADDRESS";
private string mFileUrl = "MY_DOCUMENT_URL";
private string mShareId = "MY_SESSION_ID";

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    mBlackBoxConnection = new BlackBoxConnection(this.ApplicationContext);

    CreateViewerFragment();
    mCollabViewerTabHostFragment.TabDocumentLoaded += (sender, e) =>
    {
        HandleTabDocumentLoaded();
    };
}

public void CreateViewerFragment() {
    // Create your CollabViewerTabHostFragment host fragment
    // and instantiate mCollabViewerTabHostFragment
}

private void HandleTabDocumentLoaded() {
    DocumentViewModel documentViewModel = (DocumentViewModel) ViewModelProviders.Of(this).Get(Java.Lang.Class.FromType(typeof(DocumentViewModel)));

    documentViewModel.SetCustomConnection(mBlackBoxConnection);
    mBlackBoxConnection.Start(WVS_ROOT, DEFAULT_FILE_URL, DEFAULT_SHARE_ID);
}

protected override void OnDestroy()
{
    base.OnDestroy();

    mBlackBoxConnection.Stop();
}

Where:

  • WVS_ROOT is the address of your WebViewer Server instance
  • mFileUrl is the Url to the file to be collaborated on
  • mShareId is a unique identifier for an existing collaboration session, pass null to generate a new session

And that is it! PDFTron will handle all annotation syncing and annotation reply for you.

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn more
close