1

In our application we're using Autodesk Forge Viewer to render 3D and 2D design files. Files with other formats get rendered pretty well. But in case of the pdf files, only the first page gets rendered even if the file actually has multiple pages. But we need to display all the pages.

Viewer loading only the first page

Here's the part of code I'm using to initialize the viewer:

function doInitializeTheViewer(urn, token, element) {
    const options = {
        'env': 'AutodeskProduction',
        'accessToken': token
    };

    let documentId = 'urn:' + urn;

    return new Promise((resolve, reject) => {
        Autodesk.Viewing.Initializer(options, function onInitialized() {
            let viewerApp = new Autodesk.A360ViewingApplication(element.id);

            viewerApp.onDocumentLoaded = function (doc) {

                resolve(getViewerInstance().then(viewer => {
                    state.viewer = viewer;
                    return state;
                }));
            };

            viewerApp.onDocumentFailedToLoad = (reason, errorCode) => {
                reject({errorCode, reason});
            };

            viewerApp.registerViewer(viewerApp.k3D, Autodesk.Viewing.Private.GuiViewer3D);
            viewerApp.loadDocumentWithItemAndObject(documentId);

            state.viewerApp = viewerApp;
        });
    });
}

And, this is how it gets invoked:

let element = document.getElementById('#the-viewer');

fetch2LegToken().then(
    ({accessToken}) => doInitializeTheViewer(urnB64, accessToken, element)
);

What else do I need to do here to get the viewer also render multi-page pdf files along with other 3D/2D files?

I couldn't find any way to configure this in the API documentation as well nor could I find it in any sample.

kabirbaidhya
  • 3,264
  • 3
  • 34
  • 59
  • Are you trying to render a .pdf file or a file that linked a pdf but have trouble loading it in the viewer? If the latter, what type of file are you trying to load? – Shiya Luo Aug 19 '16 at 05:11
  • @ShiyaLuo No, I'm trying to load the `pdf` file itself in the viewer. It does loads properly without any error. But the problem is only the first page gets loaded even if the pdf file has multiple pages – kabirbaidhya Aug 19 '16 at 05:20

1 Answers1

2

.pdf files are translated as 2D sheets in the viewer, each page in the .pdf file should appear as individual 2D views.

If you just use the boilerplate code from the Instantiate a Basic Viewer you'll get multiple views like so:

multiple 2D views

Since you override onDocumentLoaded, take a look at how the Autodesk360App.js implemented onDocumentLoaded method. At line 621:

function showDesignExplorer( modelDocument )
{
    var viewableItems = Autodesk.Viewing.Document.getSubItemsWithProperties(modelDocument.getRootItem(), {'type':'folder','role':'viewable'}, true);
    var root = viewableItems[0];
    var geometryItems = Autodesk.Viewing.Document.getSubItemsWithProperties(root, {'type':'geometry'}, true);
    if (geometryItems.length === 0)
        return false;

    if (geometryItems.length === 1) {
        // Check if the item has camera views.
        return modelDocument.getNumViews( geometryItems[0] ) > 1;
    }
    return true;
}

In your onDocumentLoaded method, call the Autodesk.Viewing.Document.getSubItemsWithProperties method to get all the views.

There's also a line at lmvdbg at demonstrate how to load all the views.

Shiya Luo
  • 208
  • 1
  • 6
  • That does make sense. And the `Autodesk.Viewing.Document.getSubItemsWithProperties` did return the other pages. Thanks. BTW, the reason why I did override `onDocumentLoaded` and `onDocumentFailedToLoad` was because I need to handle those events and trigger necessary actions in our application. So, what is the recommended way for handling this without overriding these methods? – kabirbaidhya Aug 19 '16 at 07:19
  • Also, the boilerplate code shown in the example here https://developer.autodesk.com/en/docs/viewer/v2/tutorials/basic-viewer/ uses `viewerApp.loadDocumentWithItemAndObject(documentId);` method which itself doesn't accept any callbacks to handle document loaded or error events which I need to handle. – kabirbaidhya Aug 19 '16 at 07:35
  • 1
    Feel free to copy + paste the code that you need into your own `onDocumentLoaded` function. – Shiya Luo Aug 19 '16 at 07:57