/** * ReaderControl config file * ------------------------------ * This js file is meant to simplify configuring commonly used settings for ReaderControl. * You can override default settings through ReaderControl.config properties, or add JavaScript code directly here. */ (() => { 'use strict'; // Create a custom tool for redaction class CustomRedactionCreateTool extends Tools.GenericAnnotationCreateTool { constructor(docViewer) { super(docViewer, Annotations.RectangleAnnotation); } mouseLeftUp(e) { const annot = this.annotation; super.mouseLeftUp.call(this, e); if (annot) { // if an annot is created... const am = readerControl.docViewer.getAnnotationManager(); const docCore = readerControl.docViewer.getDocument(); let pdfDoc; // When any outstanding operation have completed, begin redacting PDFNet.initialize().then(function() { docCore.getPDFDoc().then(function(viewedDoc) { pdfDoc = viewedDoc; // waits for page to be downloaded before continuing return pdfDoc.requirePage(annot.getPageNumber()); }).then(function() { return redactElementsInBox(pdfDoc, docCore, annot, am); }).then(function() { // remove our selection box am.deleteAnnotation(annot); // refresh the page with the newly updated document readerControl.docViewer.refreshPage(annot.getPageNumber()); // update viewer with new document readerControl.docViewer.updateView(); //readerControl.docViewer.getDocument().refreshTextData(); }); }); } } } $(document).on('documentLoaded', () => { const redactToolName = 'AnnotationCreateRedactionTool'; const redactTool = new CustomRedactionCreateTool(readerControl.docViewer); readerControl.registerTool({ toolName: redactToolName, toolObject: redactTool }); readerControl.setHeaderItems((header) => { const items = header.getItems(); items.splice(9, 0, { type: 'actionButton', img: '../../../samples/full-apis/ViewerRedactTest/annot_custom_redact.png', onClick: () => { readerControl.setToolMode(redactToolName); } }); header.update(items); }); readerControl.setToolMode(redactToolName); }); let redactElementsInBox = (pdfDoc, docCore, annot, annotManager) => { // Convert an iterator of sequentially dependent promises (that take each result in the sequence as the next one's parameter) into a single promise const main = async() => { /* eslint-disable no-unused-vars */ let ret = 0; try { let islocked = false; const pageNumber = annot.getPageNumber(); const doc = pdfDoc; doc.initSecurityHandler(); doc.lock(); islocked = true; const redactRectX1 = annot.getX(); const redactRectY1 = annot.getY(); const redactRectX2 = redactRectX1 + annot.getWidth(); const redactRectY2 = redactRectY1 + annot.getHeight(); // Turn element coordinates into PDF coordinates const pdfCoord = docCore.getPDFCoordinates(pageNumber - 1, redactRectX1, redactRectY1); const pdfCoord2 = docCore.getPDFCoordinates(pageNumber - 1, redactRectX2, redactRectY2); console.log(pdfCoord); console.log(pdfCoord2); const pageCount = await doc.getPageCount(); const page = await doc.getPage(pageNumber); // we need to adjust the coordinates to what Stamper class expects const mtx = await page.getDefaultMatrix(); const coord = await mtx.mult(pdfCoord.x, pdfCoord.y); const coord2 = await mtx.mult(pdfCoord2.x, pdfCoord2.y); //const coord = pdfCoord; //const coord2 = pdfCoord2; console.log(coord); console.log(coord2); const x1 = Math.min(coord.x, coord2.x); const x2 = Math.max(coord.x, coord2.x); const y1 = Math.min(coord.y, coord2.y); const y2 = Math.max(coord.y, coord2.y); const w = x2 - x1; const h = y2 - y1; console.log(x1 + ", " + x2 + ", " + y1 + ", " + y2); const stamper = await PDFNet.Stamper.create(PDFNet.Stamper.SizeType.e_absolute_size, w, h); stamper.setAlignment(PDFNet.Stamper.HorizontalAlignment.e_horizontal_left, PDFNet.Stamper.VerticalAlignment.e_vertical_bottom); stamper.setPosition(x1, y1); const img = await PDFNet.Image.createFromURL(doc, '../../../samples/full-apis/TestFiles/peppers.jpg'); let pageCounter = 1; while (pageCounter <= pageCount) { // This section is only required to ensure the page is available // for incremental download. At the moment the call to requirePage must be // be wrapped in this manner to avoid potential deadlocks and // allow other parts of the viewer to run while the page is being downloaded. doc.unlock(); await PDFNet.finishOperation(); await doc.requirePage(pageCounter); await PDFNet.beginOperation(); doc.lock(); // load the page and begin processing const page = await doc.getPage(pageCounter); const pgSet = await PDFNet.PageSet.createSinglePage(pageCounter); stamper.stampImage(doc, img, pgSet); console.log('page ' + pageCounter + ' finished editing'); pageCounter++; } console.log('Done.'); } catch (err) { console.log(err.stack); ret = 1; } }; return PDFNet.runWithCleanup(main); }; })(); // eslint-disable-next-line spaced-comment //# sourceURL=config.js