-
Notifications
You must be signed in to change notification settings - Fork 170
/
PDFParser.js
75 lines (67 loc) · 2.44 KB
/
PDFParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var hummus = require('../hummus');
var assert = require('chai').assert;
var fs = require('fs');
describe('PDFParser', function() {
it('should complete without error', function() {
var mTabLevel = 0;
var mIteratedObjectIDs = {};
var outputFile = fs.openSync(__dirname + '/output/parseLog.txt','w');
function logToFile(inString) {
fs.writeSync(outputFile,addTabs() + inString + '\r\n');
}
function addTabs() {
var output='';
for (var i=0;i<mTabLevel;++i) {
output+=' ';
}
return output;
}
function iterateObjectTypes(inObject,inReader) {
var output = '';
if (inObject.getType() == hummus.ePDFObjectIndirectObjectReference) {
output+= 'Indirect object reference:';
logToFile(output);
var objectID = inObject.toPDFIndirectObjectReference().getObjectID();
if (!mIteratedObjectIDs.hasOwnProperty(objectID)) {
mIteratedObjectIDs[objectID] = true;
iterateObjectTypes(inReader.parseNewObject(objectID),inReader);
}
for (var i=0;i<mTabLevel;++i) {
output+=' ';
}
output+='was parsed already';
logToFile(output);
} else if (inObject.getType() == hummus.ePDFObjectArray) {
output+= hummus.getTypeLabel(inObject.getType());
logToFile(output);
++mTabLevel;
inObject.toPDFArray().toJSArray().forEach(function(element, index, array){iterateObjectTypes(element,inReader);});
--mTabLevel;
} else if (inObject.getType() == hummus.ePDFObjectDictionary) {
output+= hummus.getTypeLabel(inObject.getType());
logToFile(output);
++mTabLevel;
var aDictionary = inObject.toPDFDictionary().toJSObject();
Object.getOwnPropertyNames(aDictionary).forEach(function(element,index,array)
{
logToFile(element);
iterateObjectTypes(aDictionary[element],inReader);
});
--mTabLevel;
} else if (inObject.getType() == hummus.ePDFObjectStream) {
output+= 'Stream . iterating stream dictionary:';
logToFile(output);
iterateObjectTypes(inObject.toPDFStream().getDictionary(),inReader);
} else {
output+= hummus.getTypeLabel(inObject.getType());
logToFile(output);
}
}
var pdfReader = hummus.createReader(__dirname + '/TestMaterials/XObjectContent.PDF');
assert.equal(pdfReader.getPDFLevel(), 1.3, 'getPDFLevel');
assert.equal(pdfReader.getPagesCount(), 2, 'getPagesCount');
var catalog = pdfReader.queryDictionaryObject(pdfReader.getTrailer(),'Root');
iterateObjectTypes(catalog,pdfReader);
fs.closeSync(outputFile);
});
});