-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathModifyingExistingFileContent.js
252 lines (227 loc) · 6.73 KB
/
ModifyingExistingFileContent.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
var muhammara = require("../lib/muhammara");
// [look...not gonna write it 100% right...you JS experts can probably write this better]
// PDFComment object
function PDFComment(
inText,
inCommentator,
inPosition,
inColor,
flag,
inReplyTo,
) {
this.time = new Date();
this.text = inText;
this.commentator = inCommentator;
this.position = inPosition;
this.color = inColor;
this.replyTo = inReplyTo;
this.objectID = 0;
this.flag = flag;
}
// PDFCommentWriter object
function PDFCommentWriter(inPDFWriter) {
this.pdfWriter = inPDFWriter;
}
PDFCommentWriter.prototype.writeCommentTree = function (inComment) {
this._writeCommentsTree(inComment).first;
};
PDFCommentWriter.prototype._writeCommentsTree = function (inComment) {
var repliedTo = 0;
var result;
// if already written, let go
if (inComment.objectID != 0) {
return inComment.objectID;
}
// if has referred comment, write it first
if (inComment.replyTo) {
repliedTo = this._writeCommentsTree(inComment.replyTo);
}
var objectsContext = this.pdfWriter.getObjectsContext();
result = objectsContext.startNewIndirectObject();
var dictionaryContext = objectsContext.startDictionary();
dictionaryContext
.writeKey("Type")
.writeNameValue("Annot")
.writeKey("Subtype")
.writeNameValue("Text")
.writeKey("Rect")
.writeRectangleValue(inComment.position) // when implementing allow also 4 numbers input
.writeKey("Contents")
.writeLiteralStringValue(
this.pdfWriter.createPDFTextString(inComment.text).toBytesArray(),
)
.writeKey("C");
objectsContext.startArray();
inComment.color.forEach(function (element, index, array) {
objectsContext.writeNumber(element / 255);
});
objectsContext.endArray(muhammara.eTokenSeparatorEndLine);
dictionaryContext
.writeKey("T")
.writeLiteralStringValue(
this.pdfWriter.createPDFTextString(inComment.commentator).toBytesArray(),
)
.writeKey("M")
.writeLiteralStringValue(
this.pdfWriter.createPDFDate(inComment.time).toString(),
);
if (inComment.replyTo) {
dictionaryContext
.writeKey("IRT")
.writeObjectReferenceValue(repliedTo)
.writeKey("RT")
.writeNameValue("R");
}
dictionaryContext
.writeKey("Open")
.writeBooleanValue(false)
.writeKey("Name")
.writeNameValue("Comment")
.writeKey("F")
.writeNumberValue(getFlagBitNumberByName(inComment.flag));
objectsContext.endDictionary(dictionaryContext).endIndirectObject();
inComment.objectID = result;
return result;
};
function getFlagBitNumberByName(name) {
name = name || "";
// 12.5.3 Annotation Flags
switch (name.toLowerCase()) {
case "invisible":
return 1;
case "hidden":
return 2;
case "print":
return 4;
case "nozoom":
return 8;
case "norotate":
return 16;
case "noview":
return 32;
case "readonly":
return 64;
case "locked":
return 128;
case "togglenoview":
return 256;
// 1.7+
// case 'lockedcontents':
// return 512;
default:
return 0;
}
}
describe("ModifyingExistingFileContent", function () {
var inPDFWriter;
before(function () {
inPDFWriter = muhammara.createWriterToModify(
__dirname + "/TestMaterials/AddedPage.pdf",
{
modifiedFilePath:
__dirname + "/output/ModifyingExistingFileContent.pdf",
},
);
});
describe("page size modification", function () {
it("should complete without error", function () {
var copyingContext = inPDFWriter.createPDFCopyingContextForModifiedFile();
var thirdPageID = copyingContext
.getSourceDocumentParser()
.getPageObjectID(2);
var thirdPageObject = copyingContext
.getSourceDocumentParser()
.parsePage(2)
.getDictionary()
.toJSObject();
var objectsContext = inPDFWriter.getObjectsContext();
objectsContext.startModifiedIndirectObject(thirdPageID);
var modifiedPageObject = inPDFWriter
.getObjectsContext()
.startDictionary();
Object.getOwnPropertyNames(thirdPageObject).forEach(
function (element, index, array) {
if (element != "MediaBox") {
modifiedPageObject.writeKey(element);
copyingContext.copyDirectObjectAsIs(thirdPageObject[element]);
}
},
);
modifiedPageObject.writeKey("MediaBox");
objectsContext
.startArray()
.writeNumber(0)
.writeNumber(0)
.writeNumber(500)
.writeNumber(500)
.endArray()
.endLine()
.endDictionary(modifiedPageObject)
.endIndirectObject();
});
});
describe("adding comments", function () {
it("should complete without error", function () {
var commentWriter = new PDFCommentWriter(inPDFWriter);
var aComment = new PDFComment(
"a very important text",
"someone",
[100, 500, 200, 600],
[255, 0, 0],
"noview",
);
commentWriter.writeCommentTree(aComment);
var bComment = new PDFComment(
"I have nothing to say about this",
"someone",
[100, 100, 200, 200],
[255, 0, 0],
"readOnly",
);
var cComment = new PDFComment(
"yeah. me too. it's just perfect",
"Someone else",
[150, 150, 250, 250],
[0, 255, 0],
"nozoom",
bComment,
);
commentWriter.writeCommentTree(cComment);
var copyingContext = inPDFWriter.createPDFCopyingContextForModifiedFile();
var fourthPageID = copyingContext
.getSourceDocumentParser()
.getPageObjectID(3);
var fourthPageObject = copyingContext
.getSourceDocumentParser()
.parsePage(3)
.getDictionary()
.toJSObject();
var objectsContext = inPDFWriter.getObjectsContext();
objectsContext.startModifiedIndirectObject(fourthPageID);
var modifiedPageObject = inPDFWriter
.getObjectsContext()
.startDictionary();
Object.getOwnPropertyNames(fourthPageObject).forEach(
function (element, index, array) {
if (element != "Annots") {
modifiedPageObject.writeKey(element);
copyingContext.copyDirectObjectAsIs(fourthPageObject[element]);
}
},
);
modifiedPageObject.writeKey("Annots");
objectsContext.startArray();
objectsContext.writeIndirectObjectReference(aComment.objectID);
objectsContext.writeIndirectObjectReference(bComment.objectID);
objectsContext.writeIndirectObjectReference(cComment.objectID);
objectsContext
.endArray()
.endLine()
.endDictionary(modifiedPageObject)
.endIndirectObject();
});
});
after(function () {
inPDFWriter.end();
});
});