Hi Jakob and Viren. Thanks for your answer.
I am trying to implement a context menu (rightclick menu) on viewer.html in order to process image with OCR after retrieving pdf page. The only problem is that I cannot retrieve the "pageNum" variable from the canvas or document.getElementById('pageNumber').value. Just don't figure out how!!!
Let me show you my approach at
http://bit.ly/ZmbPfQ, go and right-click to see what I mean by context-menu. But I need it to extract just THIS PAGE and don't know how to get the current page number.
I tried with the simplest version of pdf.js viewer (pdfjs-prevnext) (You can check it in
http://jsbin.com/pdfjs-prevnext-v2/2091/edit) with this code:
<html>
<body>
<div>
<button id="prev" onclick="goPrevious()">Previous</button>
<button id="next" onclick="goNext()">Next</button>
<span>Page: <span id="page_numo"></span> / <span id="page_count"></span></span>
<!--<input type=text id="elNum" value=1>-->
</div>
<div>
<canvas id="the-canvas" style="border:0px solid black"></canvas>
</div>
<!-- Use latest PDF.js build from Github -->
<script type="text/javascript" src="
https://raw.github.com/mozilla/pdf.js/gh-pages/build/pdf.js"></script>
<script type="text/javascript">
//
// NOTE:
// Modifying the URL below to another server will likely *NOT* work. Because of browser
// security restrictions, we have to use a file server with special headers
// (CORS) - most servers don't support cross-origin browser requests.
//
var url = '
http://cdn.mozilla.net/pdfjs/tracemonkey.pdf';
//
// Disable workers to avoid yet another cross-origin issue (workers need the URL of
// the script to be loaded, and currently do not allow cross-origin scripts)
//
PDFJS.disableWorker = true;
var pdfDoc = null,
pageNum = 1,
scale = 0.8,
canvas = document.getElementById('the-canvas'),
ctx = canvas.getContext('2d');
numero = 1;
//
// Get page info from document, resize canvas accordingly, and render page
//
function renderPage(num) {
// Using promise to fetch the page
pdfDoc.getPage(num).then(function(page) {
var viewport = page.getViewport(scale);
canvas.height = viewport.height;
canvas.width = viewport.width;
// Render PDF page into canvas context
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
page.render(renderContext);
});
// Update page counters
document.getElementById('page_numo').textContent = pageNum;
document.getElementById('page_count').textContent = pdfDoc.numPages;
//sic = document.getElementById('elNum');
//sic.value = pageNum;
}
crc = 1; //This global variable will be used on viewCurrPage to retrieve current page Number
// Go to previous page
//
function goPrevious() {
if (pageNum <= 1)
return;
pageNum--;
crc = pageNum;
renderPage(pageNum);
viewCurrPage(); //Function to write the content of div with current page Number
}
//
// Go to next page
//
function goNext() {
if (pageNum >= pdfDoc.numPages)
return;
pageNum++;
crc = pageNum;
renderPage(pageNum);
viewCurrPage();
}
//
// Asynchronously download PDF as an ArrayBuffer
//
PDFJS.getDocument(url).then(function getPdfHelloWorld(_pdfDoc) {
pdfDoc = _pdfDoc;
renderPage(pageNum);
});
</script>
<script language=javascript>
var viewCurrPage = function() {
console.log('Current page is '+crc);
document.getElementById('one').innerHTML = '<a href="edit_meta.php?page='+crc+'">Editar Metadata pagina '+crc+'</a>';
}
</script>
<div id="one"><a href="edit_meta.php?page=1">Editar Metadata pagina 1</a></div>
</body>
</html>
The function is simple and it tries to rewrite the content of div #one with the function viewCurrPage(). However, this version is so simple. PDF.js is a completely different thing and until now I have no idea how to implement this. I know there must be a simplest approach to this.
Thanks for your help!!