Simple AcroForms support
This commit is contained in:
parent
112115c013
commit
f3ee85efab
90
src/core.js
90
src/core.js
@ -265,46 +265,87 @@ var Page = (function pagePage() {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
getLinks: function pageGetLinks() {
|
getLinks: function pageGetLinks() {
|
||||||
|
var links = [];
|
||||||
|
var annotations = pageGetAnnotations();
|
||||||
|
var i, n = annotations.length;
|
||||||
|
for (i = 0; i < n; ++i) {
|
||||||
|
if (annotations[i].type != 'Link')
|
||||||
|
continue;
|
||||||
|
links.push(annotations[i]);
|
||||||
|
}
|
||||||
|
return links;
|
||||||
|
},
|
||||||
|
getAnnotations: function pageGetAnnotations() {
|
||||||
var xref = this.xref;
|
var xref = this.xref;
|
||||||
var annotations = xref.fetchIfRef(this.annotations) || [];
|
var annotations = xref.fetchIfRef(this.annotations) || [];
|
||||||
var i, n = annotations.length;
|
var i, n = annotations.length;
|
||||||
var links = [];
|
var items = [];
|
||||||
for (i = 0; i < n; ++i) {
|
for (i = 0; i < n; ++i) {
|
||||||
var annotation = xref.fetch(annotations[i]);
|
var annotation = xref.fetch(annotations[i]);
|
||||||
if (!isDict(annotation))
|
if (!isDict(annotation))
|
||||||
continue;
|
continue;
|
||||||
var subtype = annotation.get('Subtype');
|
var subtype = annotation.get('Subtype');
|
||||||
if (!isName(subtype) || subtype.name != 'Link')
|
if (!isName(subtype))
|
||||||
continue;
|
continue;
|
||||||
var rect = annotation.get('Rect');
|
var rect = annotation.get('Rect');
|
||||||
var topLeftCorner = this.rotatePoint(rect[0], rect[1]);
|
var topLeftCorner = this.rotatePoint(rect[0], rect[1]);
|
||||||
var bottomRightCorner = this.rotatePoint(rect[2], rect[3]);
|
var bottomRightCorner = this.rotatePoint(rect[2], rect[3]);
|
||||||
|
|
||||||
var link = {};
|
var item = {};
|
||||||
link.x = Math.min(topLeftCorner.x, bottomRightCorner.x);
|
item.type = subtype.name;
|
||||||
link.y = Math.min(topLeftCorner.y, bottomRightCorner.y);
|
item.x = Math.min(topLeftCorner.x, bottomRightCorner.x);
|
||||||
link.width = Math.abs(topLeftCorner.x - bottomRightCorner.x);
|
item.y = Math.min(topLeftCorner.y, bottomRightCorner.y);
|
||||||
link.height = Math.abs(topLeftCorner.y - bottomRightCorner.y);
|
item.width = Math.abs(topLeftCorner.x - bottomRightCorner.x);
|
||||||
var a = this.xref.fetchIfRef(annotation.get('A'));
|
item.height = Math.abs(topLeftCorner.y - bottomRightCorner.y);
|
||||||
if (a) {
|
switch (subtype.name) {
|
||||||
switch (a.get('S').name) {
|
case 'Link':
|
||||||
case 'URI':
|
var a = this.xref.fetchIfRef(annotation.get('A'));
|
||||||
link.url = a.get('URI');
|
if (a) {
|
||||||
|
switch (a.get('S').name) {
|
||||||
|
case 'URI':
|
||||||
|
link.url = a.get('URI');
|
||||||
|
break;
|
||||||
|
case 'GoTo':
|
||||||
|
link.dest = a.get('D');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
TODO('other link types');
|
||||||
|
}
|
||||||
|
} else if (annotation.has('Dest')) {
|
||||||
|
// simple destination link
|
||||||
|
var dest = annotation.get('Dest');
|
||||||
|
link.dest = isName(dest) ? dest.name : dest;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'Widget':
|
||||||
|
var fieldType = annotation.get('FT');
|
||||||
|
if (!isName(fieldType))
|
||||||
break;
|
break;
|
||||||
case 'GoTo':
|
item.fieldType = fieldType.name;
|
||||||
link.dest = a.get('D');
|
var fieldName = [];
|
||||||
break;
|
var name = stringToPDFString(annotation.get('T'));
|
||||||
default:
|
if (name)
|
||||||
TODO('other link types');
|
fieldName.push(name)
|
||||||
}
|
var parent = xref.fetchIfRef(annotation.get('Parent'));
|
||||||
} else if (annotation.has('Dest')) {
|
while (parent) {
|
||||||
// simple destination link
|
name = stringToPDFString(parent.get('T'));
|
||||||
var dest = annotation.get('Dest');
|
if (name)
|
||||||
link.dest = isName(dest) ? dest.name : dest;
|
fieldName.unshift(name);
|
||||||
|
parent = xref.fetchIfRef(parent.get('Parent'));
|
||||||
|
}
|
||||||
|
item.fullName = fieldName.join('.');
|
||||||
|
var alternativeText = stringToPDFString(annotation.get('TU') || '');
|
||||||
|
item.alternativeText = alternativeText;
|
||||||
|
var da = annotation.get('DA') || '';
|
||||||
|
var m = /([\d\.]+)\sTf/.exec(da);
|
||||||
|
if (m)
|
||||||
|
item.fontSize = parseFloat(m[1]);
|
||||||
|
item.flags = annotation.get('Ff') || 0;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
links.push(link);
|
items.push(item);
|
||||||
}
|
}
|
||||||
return links;
|
return items;
|
||||||
},
|
},
|
||||||
startRendering: function pageStartRendering(ctx, callback) {
|
startRendering: function pageStartRendering(ctx, callback) {
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
@ -342,6 +383,7 @@ var PDFDocModel = (function pdfDoc() {
|
|||||||
assertWellFormed(stream.length > 0, 'stream must have data');
|
assertWellFormed(stream.length > 0, 'stream must have data');
|
||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
this.setup();
|
this.setup();
|
||||||
|
this.acroForm = this.xref.fetchIfRef(this.catalog.catDict.get('AcroForm'));
|
||||||
}
|
}
|
||||||
|
|
||||||
function find(stream, needle, limit, backwards) {
|
function find(stream, needle, limit, backwards) {
|
||||||
|
@ -231,6 +231,23 @@ canvas {
|
|||||||
-webkit-box-shadow: 0px 2px 10px #ff0;
|
-webkit-box-shadow: 0px 2px 10px #ff0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.page > .inputHint {
|
||||||
|
opacity: 0.2;
|
||||||
|
background: #ccc;
|
||||||
|
position: absolute;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page > .inputControl {
|
||||||
|
background: transparent;
|
||||||
|
border: 0px none;
|
||||||
|
position: absolute;
|
||||||
|
margin: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.page > .inputControl[type='checkbox'] {
|
||||||
|
margin: 0px;
|
||||||
|
}
|
||||||
|
|
||||||
#viewer {
|
#viewer {
|
||||||
margin: 44px 0px 0px;
|
margin: 44px 0px 0px;
|
||||||
padding: 8px 0px;
|
padding: 8px 0px;
|
||||||
@ -281,6 +298,10 @@ canvas {
|
|||||||
display: block;
|
display: block;
|
||||||
page-break-after: always;
|
page-break-after: always;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.inputHint {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#loading {
|
#loading {
|
||||||
|
@ -33,6 +33,7 @@ var PDFView = {
|
|||||||
thumbnails: [],
|
thumbnails: [],
|
||||||
currentScale: kDefaultScale,
|
currentScale: kDefaultScale,
|
||||||
initialBookmark: document.location.hash.substring(1),
|
initialBookmark: document.location.hash.substring(1),
|
||||||
|
formFields: [],
|
||||||
|
|
||||||
setScale: function pdfViewSetScale(val, resetAutoSettings) {
|
setScale: function pdfViewSetScale(val, resetAutoSettings) {
|
||||||
var pages = this.pages;
|
var pages = this.pages;
|
||||||
@ -362,7 +363,7 @@ var PageView = function pageView(container, content, id, pageWidth, pageHeight,
|
|||||||
div.removeAttribute('data-loaded');
|
div.removeAttribute('data-loaded');
|
||||||
};
|
};
|
||||||
|
|
||||||
function setupLinks(content, scale) {
|
function setupAnnotations(content, scale) {
|
||||||
function bindLink(link, dest) {
|
function bindLink(link, dest) {
|
||||||
link.href = PDFView.getDestinationHash(dest);
|
link.href = PDFView.getDestinationHash(dest);
|
||||||
link.onclick = function pageViewSetupLinksOnclick() {
|
link.onclick = function pageViewSetupLinksOnclick() {
|
||||||
@ -371,18 +372,82 @@ var PageView = function pageView(container, content, id, pageWidth, pageHeight,
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
function bindInputItem(input, item) {
|
||||||
|
if (input.name in PDFView.formFields) {
|
||||||
|
var value = PDFView.formFields[input.name];
|
||||||
|
if (input.type == 'checkbox')
|
||||||
|
input.checked = value;
|
||||||
|
else if (!input.type || input.type == 'text')
|
||||||
|
input.value = value;
|
||||||
|
}
|
||||||
|
input.onchange = function pageViewSetupInputOnBlur() {
|
||||||
|
if (input.type == 'checkbox')
|
||||||
|
PDFView.formFields[input.name] = input.checked;
|
||||||
|
else if (!input.type || input.type == 'text')
|
||||||
|
PDFView.formFields[input.name] = input.value;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
function createElementWithStyle(tagName, item) {
|
||||||
|
var element = document.createElement(tagName);
|
||||||
|
element.style.left = (Math.floor(item.x - view.x) * scale) + 'px';
|
||||||
|
element.style.top = (Math.floor(item.y - view.y) * scale) + 'px';
|
||||||
|
element.style.width = Math.ceil(item.width * scale) + 'px';
|
||||||
|
element.style.height = Math.ceil(item.height * scale) + 'px';
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
function assignFontStyle(element, item) {
|
||||||
|
var fontStyles = '';
|
||||||
|
if ('fontSize' in item)
|
||||||
|
fontStyles += 'font-size: ' + Math.round(item.fontSize * scale) + 'px';
|
||||||
|
element.setAttribute('style', element.getAttribute('style') + fontStyles);
|
||||||
|
}
|
||||||
|
|
||||||
var links = content.getLinks();
|
var items = content.getAnnotations();
|
||||||
for (var i = 0; i < links.length; i++) {
|
for (var i = 0; i < items.length; i++) {
|
||||||
var link = document.createElement('a');
|
var item = items[i];
|
||||||
link.style.left = (Math.floor(links[i].x - view.x) * scale) + 'px';
|
switch (item.type) {
|
||||||
link.style.top = (Math.floor(links[i].y - view.y) * scale) + 'px';
|
case 'Link':
|
||||||
link.style.width = Math.ceil(links[i].width * scale) + 'px';
|
var link = createElementWithStyle('a', item);
|
||||||
link.style.height = Math.ceil(links[i].height * scale) + 'px';
|
link.href = item.url || '';
|
||||||
link.href = links[i].url || '';
|
if (!item.url)
|
||||||
if (!links[i].url)
|
bindLink(link, ('dest' in item) ? item.dest : null);
|
||||||
bindLink(link, ('dest' in links[i]) ? links[i].dest : null);
|
div.appendChild(link);
|
||||||
div.appendChild(link);
|
break;
|
||||||
|
case 'Widget':
|
||||||
|
if (item.fieldType != 'Tx' && item.fieldType != 'Btn' &&
|
||||||
|
item.fieldType != 'Ch')
|
||||||
|
break;
|
||||||
|
var inputDiv = createElementWithStyle('div', item);
|
||||||
|
inputDiv.className = 'inputHint';
|
||||||
|
div.appendChild(inputDiv);
|
||||||
|
var input;
|
||||||
|
if (item.fieldType == 'Tx') {
|
||||||
|
input = createElementWithStyle('input', item);
|
||||||
|
}
|
||||||
|
if (item.fieldType == 'Btn') {
|
||||||
|
input = createElementWithStyle('input', item);
|
||||||
|
if (item.flags & 32768) {
|
||||||
|
input.type = 'radio';
|
||||||
|
TODO('radio button is not supported');
|
||||||
|
} else if (item.flags & 65536) {
|
||||||
|
input.type = 'button';
|
||||||
|
TODO('pushbutton is not supported');
|
||||||
|
} else {
|
||||||
|
input.type = 'checkbox';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (item.fieldType == 'Ch') {
|
||||||
|
input = createElementWithStyle('select', item);
|
||||||
|
TODO('select box is not supported');
|
||||||
|
}
|
||||||
|
input.className = 'inputControl';
|
||||||
|
input.name = item.fullName;
|
||||||
|
input.title = item.alternativeText;
|
||||||
|
assignFontStyle(input, item);
|
||||||
|
bindInputItem(input, item);
|
||||||
|
div.appendChild(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -489,7 +554,7 @@ var PageView = function pageView(container, content, id, pageWidth, pageHeight,
|
|||||||
stats.begin = Date.now();
|
stats.begin = Date.now();
|
||||||
this.content.startRendering(ctx, this.updateStats);
|
this.content.startRendering(ctx, this.updateStats);
|
||||||
|
|
||||||
setupLinks(this.content, this.scale);
|
setupAnnotations(this.content, this.scale);
|
||||||
div.setAttribute('data-loaded', true);
|
div.setAttribute('data-loaded', true);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -738,6 +803,8 @@ window.addEventListener('pagechange', function pagechange(evt) {
|
|||||||
|
|
||||||
window.addEventListener('keydown', function keydown(evt) {
|
window.addEventListener('keydown', function keydown(evt) {
|
||||||
var curElement = document.activeElement;
|
var curElement = document.activeElement;
|
||||||
|
if (curElement && curElement.tagName == 'INPUT')
|
||||||
|
return;
|
||||||
var controlsElement = document.getElementById('controls');
|
var controlsElement = document.getElementById('controls');
|
||||||
while (curElement) {
|
while (curElement) {
|
||||||
if (curElement === controlsElement)
|
if (curElement === controlsElement)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user