Calling PDFNet.PDFDoc.pagePushFront() results in last page not being displayed on WebViewer

97 views
Skip to first unread message

Benjamin Roedell

unread,
Oct 20, 2016, 1:22:07 PM10/20/16
to PDFTron WebViewer
I'm trying to add cover page to a PDF before displaying it on the WebViewer.

All my code is available here: https://github.com/benrobot/PdfManipulation

This code:
var runCustomViewerCode = function(pdfDoc)
{
function* main()
{
...
console.log("doc pagecount BEFORE pagePushFront: ", currentPageCount);

doc.pagePushFront(coverPage);

currentPageCount = yield doc.getPageCount();
console.log("doc pagecount AFTER pagePushFront: ", currentPageCount);
return PDFNet.runGeneratorWithCleanup(main());
}

Results in this output:
doc pagecount BEFORE pagePushFront: 2
doc pagecount AFTER pagePushFront: 3


But this code:
runCustomViewerCode(pdfDoc).then(function(){
console.log("Page count BEFORE refreshAll: ", readerControl.docViewer.getPageCount());
// Refresh the cache with the newly updated document
readerControl.docViewer.refreshAll();

console.log("Page count BEFORE updateView: ", readerControl.docViewer.getPageCount());
// Update viewer with new document
readerControl.docViewer.updateView();
});

Results in this output:
Page count BEFORE refreshAll: 2
Page count BEFORE updateView: 2

How do I get my inserted to be recognized by the WebViewer?

David Tippett

unread,
Oct 21, 2016, 7:25:21 PM10/21/16
to PDFTron WebViewer
Hi Benjamin,

In the current version of WebViewer refreshAll will only update the content of pages rather than the page structure. However, I agree this is confusing and we will change this in a future version.

In the meantime you can implement your use case in the following manner using CoreControls.Document.insertPages(). Note that in your index.html file you will want to pass useDownloader:false in the WebViewer constructor. This is generally necessary when using page operations within WebViewer.

Config.js:

(function() {
    $(document).on('documentLoaded', function() {
        PDFNet.initialize().then(function(){
            var doc = readerControl.docViewer.getDocument();
doc.getPDFDoc().then(function(pdfDoc){
console.log("Page count BEFORE ALL: ", readerControl.docViewer.getPageCount());
// note that requirePage is not necessary here without 

console.log("Page count BEFORE runCustomViewerCode: ", readerControl.docViewer.getPageCount());
// Run our script
runCustomViewerCode(pdfDoc).then(function(count){
// load the cover document
var partRetriever = new CoreControls.PartRetrievers.ExternalPdfPartRetriever("/resources/This_is_a_cover_page.pdf", {useDownloader: false});
var coverDoc = new CoreControls.Document(null, "pdf");
var coverDocReady = function() {
// copy page 1 from coverDoc to the beginning of doc
// insertPages will automatically update the structure in WebViewer
doc.insertPages(coverDoc, [1], 1).then(function() {
// Since pages changed before we still need to make sure all pages are redrawn
// Refresh the cache with the newly updated document
readerControl.docViewer.refreshAll();

// Update viewer with changes
readerControl.docViewer.updateView();
console.log("Page count after insertPages: ", readerControl.docViewer.getPageCount());
});
};
// Note: since initPDFWorkerTransports has already been called by WebViewer constructor and PDFNet.initialize
// it doesn't require any arguments.
coverDoc.loadAsync(partRetriever, coverDocReady, {workerTransportPromise: CoreControls.initPDFWorkerTransports()});
});
});
        });
    });

    var runCustomViewerCode = function(pdfDoc)
    {
        function* main()
        {
            console.log("Hello WebViewer!");
            var doc = pdfDoc;
            doc.lock();
            
            // Example 1) Add text stamp to all pages, then remove text stamp from odd pages. 
            try 
            {
                // start stack-based deallocation with startDeallocateStack. Later on when endDeallocateStack is called,
                // all objects in memory that were initialized since the most recent startDeallocateStack call will be
                // cleaned up. Doing this makes sure that memory growth does not get too high.
                yield PDFNet.startDeallocateStack();

                var stamper = yield PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_relative_scale, 0.5, 0.5); //Stamp size is relative to the size of the crop box of the destination page
                stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_center, PDFNet.Stamper.VerticalAlignment.e_vertical_center);

                var redColorPt = yield PDFNet.ColorPt.init(1, 0, 0);
                stamper.setFontColor(redColorPt);
                var pgSet = yield PDFNet.PageSet.createRange(1, (yield doc.getPageCount()));
                stamper.stampText(doc, "If you are reading this\nthis is an even page", pgSet);
                var oddPgSet = yield PDFNet.PageSet.createFilteredRange(1, (yield doc.getPageCount()), PDFNet.PageSet.Filter.e_odd);
                // delete all text stamps in odd pages
                PDFNet.Stamper.deleteStamps(doc, oddPgSet);

                console.log("Sample 1 complete");

                yield PDFNet.endDeallocateStack();
            } catch (err) {
                console.log(err.stack)
                ret = 1;
            }
            
        }
        return PDFNet.runGeneratorWithCleanup(main());
    }
})();
//# sourceURL=config.js

Best Regards,

David

Benjamin Roedell

unread,
Oct 24, 2016, 12:52:00 PM10/24/16
to PDFTron WebViewer
Thank you very much.  That worked like a charm.
Reply all
Reply to author
Forward
0 new messages