2014-10-27 234 views
1

PDFTron可以選擇提供自定義Javascript文件來修改查看器,但我不確定該文件中應該包含哪些內容,並且我無法在Web上找到任何示例。PDFTron自定義腳本中應該包含哪些內容?

該網站有如下描述:

查看器選項 - 默認情況下,PWS雲文件被加載在Web查看的基本託管版本。您可以通過加載外部JavaScript配置文件來自定義查看器的外觀,品牌和自定義功能。

當我提供此文件,window.WebViewerUniversalInstance爲空,並且的document.getElementById( '的DocumentViewer')也返回一個空對象。

有人能指點我在這裏正確的方向,我迷路了。

感謝

回答

4

這個JavaScript文件是在觀衆的內部HTML頁面(ReaderControl.html)允許您直接在瀏覽器修改東西,所以WebViewerUniversalInstance將是空的(如,在頁內不的上下文中運行)。不過,我用var ele = document.getElementById('DocumentViewer');做了一個小測試文件,ele不是null,所以我不太清楚爲什麼這不適合你。

無論如何,你可以做一些事情的例子。對於自定義操作,您可能需要在「viewerLoaded」或「documentLoaded」事件中運行代碼。對於UI更改,您不需要在任何事件中都有代碼。

因此,舉例來說,如果你想隱藏你可能只是把你的文件打印按鈕:

$('#printButton').hide(); 

修改UI相關的事情,我會建議用鼠標右鍵單擊,檢查元素找到IDS /班你想修改的東西。或者,您可以下載WebViewer並查看ReaderControl.html。

這裏是在viewerLoaded事件做一些的例子:

$(document).on('viewerLoaded', function() { 
    readerControl.docViewer.SetMargin(0); 
}); 

下面是在documentLoaded事件做一些的例子:

$(document).on('documentLoaded', function() { 
    readerControl.setCurrentPageNumber(2); 
    readerControl.rotateClockwise(); 
    readerControl.setZoomLevel(2); 
}); 

有很多很多的事可以你可以在這裏查看文檔:http://www.pdftron.com/webviewer/demo/lib/html5/doc/

你可能感興趣的某些物體:

  • readerControl:viewerLoaded
  • 後可用
  • DocumentViewer:可用之後viewerloaded(readerControl.docViewer)
  • Document:documentLoaded(docViewer後可用。GetDocument())

如果您有更多的問題,你可以隨時要求在Web查看論壇:https://groups.google.com/forum/#!forum/pdfnet-webviewer

+0

如果在Pdftron上託管,是否有任何方法可以爲觀衆提供價值?我們正試圖用已完成的搜索打開查看器。做搜索部分是好的,並且已經掌握瞭如何通常使用Javascript來與查看器一起工作,但是從我們的網站向觀衆提供價值是一種痛苦,因爲跨域請求阻塞等等。可以爲觀衆提供一個價值,也許作爲URL上的一個參數? – JMK 2014-10-31 13:19:46

+0

你可以在你的服務器上啓用CORS來處理你正在做的請求(http://enable-cors.org/server.html)嗎?我只是在我的服務器上啓用了一個小測試,然後做了一些簡單的$ .get(「myserver.com/test.php」,function(result){...})。然後在回調中,我開始搜索,似乎所有的工作。 – mparizeau 2014-11-07 19:20:07

+1

嗨@mparizeau您是否考慮過公開一些更常用的功能,例如通過URL進行搜索。這將避免消費者需要考慮CORS。會很有幫助。 – richardwhatever 2014-11-10 12:45:55

1

我不知道我完全理解你的問題,但這裏是我做了在外部配置文件PDF查看器,希望這將有助於在一定程度上:

<script type="text/javascript"> 
$(function() { 
    var customData = { serviceUrl: 'services/PDFWebService.asmx', token: '<%=initialDoc.Value %>', isReadonly: '<%=IsReadonly?"yes":"no" %>' }; 
    var myWebViewer = new PDFTron.WebViewer({ 
     path: "Resources/js/PDFTron", 
     mobileRedirect: false, // Disable redirect in mobile view. 
     stream: true, 
     config: 'Resources/js/PDFViewerConfig.js', 
     documentType: "pdf", 
     custom: JSON.stringify(customData), 
     l: '<%=LicenseKey%>', 
     initialDoc: customData.serviceUrl + '/GetFile?token=' + customData.token 
    }, document.getElementById('viewer')); 
}); 

我PDFViewerConfig.js是:

(function() { 

$(document).on('viewerLoaded', function() { 
    customData = JSON.parse(window.ControlUtils.getCustomData()); 
    SetupCustomizations(); 
}); 

$(document).on('documentLoaded', function() { 
    setDisabled("#btnSave"); 
    setDisabled("#btnReset"); 
    setDisabled("#btnPushUp"); 
}); 

$(document).on('pageChanged', function (event) { 

    var currentPageNumber = readerControl.getCurrentPageNumber(); 
    var totalPages = readerControl.docViewer.getDocument().getPageCount(); 
    if (currentPageNumber == totalPages) { 
     setDisabled("#btnPushDown"); 
     setEnabled("#btnPushUp"); 
    } 
    else if (currentPageNumber == 1) { 
     setDisabled("#btnPushUp"); 
     setEnabled("#btnPushDown"); 
    } 
    else { 
     setEnabled("#btnPushUp"); 
     setEnabled("#btnPushDown"); 
    } 
}); 
})(); 

function SetupCustomizations() { 

if (customData && customData.isReadonly != "yes") { 
    var removeButton = $('<span aria-disabled="false" role="button" tabindex="0" class="glyphicons remove" title="remove page"></span>'); 
    var saveButton = $('<span aria-disabled="true" role="button" tabindex="0" id="btnSave" class="glyphicons floppy_disk disabled" title="save changes"></span>'); 
    var resetButton = $('<span aria-disabled="true" role="button" tabindex="0" id="btnReset" class="glyphicons restart disabled" title="reset to original"></span>'); 

    var rotateRightButton = $('<span aria-disabled="false" role="button" tabindex="0" class="glyphicons share" title="rotate right"></span>'); 
    var rotateLeftButton = $('<span aria-disabled="false" role="button" tabindex="0" class="glyphicons unshare" title="rotate left"></span>'); 

    var pushDownButton = $('<span aria-disabled="false" role="button" tabindex="0" id="btnPushDown" class="glyphicons down_arrow" title="move current page down"></span>'); 
    var pushUpButton = $('<span aria-disabled="true" role="button" tabindex="0" id="btnPushUp" class="glyphicons up_arrow disabled" title="move current page up"></span>'); 


    removeButton.on('click', onRemove); 
    removeButton.on('keydown', onRemove); 

    saveButton.on('click', onSave); 
    saveButton.on('keydown', onSave); 

    resetButton.on('click', onReset); 
    resetButton.on('keydown', onReset); 

    rotateRightButton.on('click', onRotateRight); 
    rotateRightButton.on('keydown', onRotateRight); 

    rotateLeftButton.on('click', onRotateLeft); 
    rotateLeftButton.on('keydown', onRotateLeft); 

    pushDownButton.on('click', onPushDown); 
    pushDownButton.on('keydown', onPushDown); 

    pushUpButton.on('click', onPushUp); 
    pushUpButton.on('keydown', onPushUp); 

    var newButtonsPlaceholder = $("#downloadButton").parent(); 
    newButtonsPlaceholder.prepend(removeButton); 
    newButtonsPlaceholder.prepend(rotateLeftButton); 
    newButtonsPlaceholder.prepend(rotateRightButton); 
    newButtonsPlaceholder.prepend(pushDownButton); 
    newButtonsPlaceholder.prepend(pushUpButton); 
    newButtonsPlaceholder.prepend(saveButton); 
    newButtonsPlaceholder.prepend(resetButton); 
} 

//508 
$("#ui-id-3").attr("tabindex", "0"); 
$("#prevPage").attr("tabindex", "0"); 
$("#nextPage").attr("tabindex", "0"); 
$("#printButton").attr("tabindex", "0"); 
$("#fullScreenButton").attr("tabindex", "0"); 
$("#downloadButton").attr("tabindex", "0"); 
$("#zoomIn").attr("tabindex", "0"); 
$("#zoomOut").attr("tabindex", "0"); 
$("#fitWidth").attr("tabindex", "0"); 
$("#fitPage").attr("tabindex", "0"); 

$("#prevPage").attr("role", "button"); 
$("#nextPage").attr("role", "button"); 
$("#printButton").attr("role", "button"); 
$("#fullScreenButton").attr("role", "button"); 

$("#zoomIn").attr("role", "button"); 
$("#zoomOut").attr("role", "button"); 
removeExtraButtons(); 
}