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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
// PDF 텍스트 패턴 기반 자동 서명 필드 생성 시스템
interface SignaturePattern {
regex: RegExp;
name: string;
priority: number;
offsetX?: number; // 텍스트로부터 X축 오프셋
offsetY?: number; // 텍스트로부터 Y축 오프셋
width?: number; // 서명 필드 너비
height?: number; // 서명 필드 높이
}
interface DetectedSignatureLocation {
pageIndex: number;
text: string;
rect: {
x1: number;
y1: number;
x2: number;
y2: number;
};
pattern: SignaturePattern;
confidence: number;
}
class AutoSignatureFieldDetector {
private instance: WebViewerInstance;
private signaturePatterns: SignaturePattern[];
constructor(instance: WebViewerInstance) {
this.instance = instance;
this.signaturePatterns = this.initializePatterns();
}
private initializePatterns(): SignaturePattern[] {
return [
// 한국어 패턴들 (우선순위 높음)
{
regex: /서명\s*[::]\s*[_\-\s]{3,}/gi,
name: "한국어_서명_콜론",
priority: 10,
offsetX: 80, // "서명:" 텍스트 오른쪽으로 80px
offsetY: -5, // 약간 위로
width: 150,
height: 40
},
{
regex: /서명란\s*[_\-\s]{0,}/gi,
name: "한국어_서명란",
priority: 9,
offsetX: 60,
offsetY: -5,
width: 150,
height: 40
},
{
regex: /서명\s*[_\-\s]{5,}/gi,
name: "한국어_서명_라인",
priority: 8,
offsetX: 50,
offsetY: -5,
width: 150,
height: 40
},
{
regex: /(계약자|갑|을)\s*서명\s*[::]?\s*[_\-\s]{0,}/gi,
name: "한국어_계약자_서명",
priority: 9,
offsetX: 100,
offsetY: -5,
width: 150,
height: 40
},
// 영어 패턴들
{
regex: /signature\s*[::]\s*[_\-\s]{3,}/gi,
name: "영어_signature_콜론",
priority: 8,
offsetX: 120,
offsetY: -5,
width: 150,
height: 40
},
{
regex: /sign\s+here\s*[::]?\s*[_\-\s]{0,}/gi,
name: "영어_sign_here",
priority: 9,
offsetX: 100,
offsetY: -5,
width: 150,
height: 40
},
{
regex: /sign\s*[::]\s*[_\-\s]{3,}/gi,
name: "영어_sign_콜론",
priority: 7,
offsetX: 60,
offsetY: -5,
width: 150,
height: 40
},
// 날짜와 함께 나오는 패턴들
{
regex: /날짜\s*[::]\s*[_\-\s]{3,}.*?서명\s*[::]\s*[_\-\s]{3,}/gi,
name: "날짜_서명_조합",
priority: 10,
offsetX: 0,
offsetY: -5,
width: 150,
height: 40
},
{
regex: /date\s*[::]\s*[_\-\s]{3,}.*?signature\s*[::]\s*[_\-\s]{3,}/gi,
name: "date_signature_조합",
priority: 10,
offsetX: 0,
offsetY: -5,
width: 150,
height: 40
},
// 일반적인 양식 패턴들
{
regex: /이름\s*[::]\s*[_\-\s]{5,}.*?서명\s*[::]\s*[_\-\s]{5,}/gi,
name: "이름_서명_조합",
priority: 8,
offsetX: 0,
offsetY: -5,
width: 150,
height: 40
},
{
regex: /name\s*[::]\s*[_\-\s]{5,}.*?signature\s*[::]\s*[_\-\s]{5,}/gi,
name: "name_signature_조합",
priority: 8,
offsetX: 0,
offsetY: -5,
width: 150,
height: 40
}
];
}
// 📄 메인 함수: 문서에서 서명 패턴 감지 및 필드 생성
async detectAndCreateSignatureFields(): Promise<string[]> {
console.log("🔍 자동 서명 필드 감지 시작...");
try {
const { Core } = this.instance;
const { documentViewer } = Core;
await Core.PDFNet.initialize();
const doc = await documentViewer.getDocument().getPDFDoc();
// 1. 기존 서명 필드 확인
const existingFields = await this.getExistingSignatureFields(doc);
console.log(`📊 기존 서명 필드: ${existingFields.length}개`);
if (existingFields.length > 0) {
console.log("✅ 기존 서명 필드가 있으므로 자동 생성 스킵");
return existingFields.map(f => f.name);
}
// 2. 텍스트 패턴 기반 서명 위치 감지
const detectedLocations = await this.detectSignatureLocations(doc);
console.log(`🎯 감지된 서명 위치: ${detectedLocations.length}개`);
// 3. 감지된 위치에 서명 필드 생성
const createdFields: string[] = [];
for (const location of detectedLocations) {
try {
const fieldName = await this.createSignatureFieldAtLocation(doc, location);
createdFields.push(fieldName);
console.log(`✅ 서명 필드 생성: ${fieldName}`);
} catch (error) {
console.error(`📛 서명 필드 생성 실패:`, error);
}
}
// 4. 문서 업데이트
if (createdFields.length > 0) {
await documentViewer.refreshAll();
await documentViewer.updateView();
console.log(`🎉 총 ${createdFields.length}개 서명 필드 자동 생성 완료`);
} else {
console.warn("⚠️ 서명 패턴을 찾지 못했습니다. 기본 서명 필드 생성...");
const defaultField = await this.createDefaultSignatureField(doc);
createdFields.push(defaultField);
}
return createdFields;
} catch (error) {
console.error("📛 자동 서명 필드 생성 실패:", error);
return [];
}
}
// 기존 서명 필드 확인
private async getExistingSignatureFields(doc: any): Promise<any[]> {
const { Core } = this.instance;
const fields = [];
try {
const pageCount = await doc.getPageCount();
for (let i = 1; i <= pageCount; i++) {
const page = await doc.getPage(i);
const numAnnots = await page.getNumAnnots();
for (let j = 0; j < numAnnots; j++) {
const annot = await page.getAnnot(j);
const annotType = await annot.getType();
if (annotType === Core.PDFNet.Annot.Type.e_Widget) {
const widget = await Core.PDFNet.WidgetAnnot.cast(annot);
const field = await widget.getField();
const fieldType = await field.getType();
if (fieldType === Core.PDFNet.Field.Type.e_signature) {
const fieldName = await field.getName();
fields.push({ name: fieldName, widget, page: i });
}
}
}
}
} catch (error) {
console.warn("기존 필드 확인 중 에러:", error);
}
return fields;
}
// 텍스트 패턴 기반 서명 위치 감지
private async detectSignatureLocations(doc: any): Promise<DetectedSignatureLocation[]> {
const { Core } = this.instance;
const detectedLocations: DetectedSignatureLocation[] = [];
try {
const pageCount = await doc.getPageCount();
for (let pageNum = 1; pageNum <= pageCount; pageNum++) {
const page = await doc.getPage(pageNum);
// 텍스트 추출
const textExtractor = await Core.PDFNet.TextExtractor.create();
await textExtractor.begin(page);
// 텍스트와 위치 정보 추출
const wordList = [];
let line = await textExtractor.getFirstLine();
while (line) {
let word = await line.getFirstWord();
while (word) {
const wordText = await word.getString();
const wordBox = await word.getBBox();
wordList.push({
text: wordText,
x1: await wordBox.getX1(),
y1: await wordBox.getY1(),
x2: await wordBox.getX2(),
y2: await wordBox.getY2()
});
word = await word.getNext();
}
line = await line.getNext();
}
// 전체 페이지 텍스트 조합
const fullText = wordList.map(w => w.text).join(' ');
// 패턴 매칭
for (const pattern of this.signaturePatterns) {
const matches = Array.from(fullText.matchAll(pattern.regex));
for (const match of matches) {
// 매치된 텍스트의 위치 찾기
const matchText = match[0];
const matchStart = match.index || 0;
// 대략적인 위치 계산 (개선 가능)
const location = this.calculateTextLocation(wordList, matchStart, matchText.length);
if (location) {
detectedLocations.push({
pageIndex: pageNum - 1,
text: matchText,
rect: {
x1: location.x1 + (pattern.offsetX || 0),
y1: location.y1 + (pattern.offsetY || 0),
x2: location.x1 + (pattern.offsetX || 0) + (pattern.width || 150),
y2: location.y1 + (pattern.offsetY || 0) + (pattern.height || 40)
},
pattern: pattern,
confidence: pattern.priority
});
console.log(`🎯 패턴 매치: "${matchText}" (${pattern.name}) 페이지 ${pageNum}`);
}
}
}
}
// 신뢰도 순으로 정렬 (중복 제거 포함)
return this.deduplicateAndSort(detectedLocations);
} catch (error) {
console.error("텍스트 패턴 감지 실패:", error);
return [];
}
}
// 텍스트 위치 계산 (개선된 버전)
private calculateTextLocation(wordList: any[], startIndex: number, length: number): any {
if (wordList.length === 0) return null;
// 간단한 구현: 첫 번째 단어의 위치 사용
// 실제로는 더 정교한 텍스트 매칭 필요
const totalChars = wordList.map(w => w.text).join(' ').length;
const ratio = startIndex / totalChars;
const targetWordIndex = Math.floor(ratio * wordList.length);
const targetWord = wordList[Math.min(targetWordIndex, wordList.length - 1)];
return targetWord;
}
// 중복 제거 및 정렬
private deduplicateAndSort(locations: DetectedSignatureLocation[]): DetectedSignatureLocation[] {
// 같은 페이지의 너무 가까운 위치들 제거
const filtered = locations.filter((loc, index) => {
return !locations.slice(0, index).some(prevLoc =>
prevLoc.pageIndex === loc.pageIndex &&
Math.abs(prevLoc.rect.x1 - loc.rect.x1) < 100 &&
Math.abs(prevLoc.rect.y1 - loc.rect.y1) < 50
);
});
// 신뢰도(우선순위) 순으로 정렬
return filtered.sort((a, b) => b.confidence - a.confidence);
}
// 감지된 위치에 서명 필드 생성
private async createSignatureFieldAtLocation(doc: any, location: DetectedSignatureLocation): Promise<string> {
const { Core } = this.instance;
const fieldName = `auto_signature_${location.pageIndex + 1}_${Date.now()}`;
const page = await doc.getPage(location.pageIndex + 1);
// 디지털 서명 필드 생성
const sigField = await doc.createDigitalSignatureField(fieldName);
// 서명 위젯 생성
const rect = await Core.PDFNet.Rect.init(
location.rect.x1,
location.rect.y1,
location.rect.x2,
location.rect.y2
);
const widget = await Core.PDFNet.SignatureWidget.createWithDigitalSignatureField(
doc, rect, sigField
);
// 위젯 스타일 설정
await widget.setBackgroundColor(
await Core.PDFNet.ColorPt.init(0.95, 0.95, 1.0), // 연한 파란색
3 // RGB
);
await widget.setBorderColor(
await Core.PDFNet.ColorPt.init(0.2, 0.4, 0.8), // 파란색 테두리
3 // RGB
);
// 페이지에 위젯 추가
await page.annotPushBack(widget);
console.log(`✅ 자동 서명 필드 생성: ${fieldName} (패턴: ${location.pattern.name})`);
return fieldName;
}
// 기본 서명 필드 생성 (패턴을 찾지 못한 경우)
private async createDefaultSignatureField(doc: any): Promise<string> {
const { Core } = this.instance;
console.log("⚠️ 서명 패턴 미발견, 기본 위치에 서명 필드 생성");
const pageCount = await doc.getPageCount();
const lastPage = await doc.getPage(pageCount);
const pageInfo = await lastPage.getPageInfo();
const pageWidth = await pageInfo.getWidth();
const pageHeight = await pageInfo.getHeight();
const fieldName = `default_signature_${Date.now()}`;
const sigField = await doc.createDigitalSignatureField(fieldName);
// 마지막 페이지 하단 중앙에 배치
const rect = await Core.PDFNet.Rect.init(
pageWidth * 0.3, // 페이지 너비 30% 지점
pageHeight * 0.1, // 페이지 하단 10% 지점
pageWidth * 0.7, // 페이지 너비 70% 지점
pageHeight * 0.2 // 페이지 하단 20% 지점
);
const widget = await Core.PDFNet.SignatureWidget.createWithDigitalSignatureField(
doc, rect, sigField
);
await widget.setBackgroundColor(
await Core.PDFNet.ColorPt.init(1.0, 0.95, 0.95), // 연한 핑크색 (주의 표시)
3
);
await widget.setBorderColor(
await Core.PDFNet.ColorPt.init(0.8, 0.2, 0.2), // 빨간색 테두리
3
);
await lastPage.annotPushBack(widget);
return fieldName;
}
}
// ✅ BasicContractSignViewer에 통합할 수 있는 함수
export async function addAutoSignatureFieldsToDocument(instance: WebViewerInstance): Promise<string[]> {
if (!instance) {
console.warn("⚠️ WebViewer 인스턴스가 없습니다.");
return [];
}
try {
const detector = new AutoSignatureFieldDetector(instance);
const createdFields = await detector.detectAndCreateSignatureFields();
if (createdFields.length > 0) {
console.log(`🎉 자동 서명 필드 생성 완료: ${createdFields.join(', ')}`);
}
return createdFields;
} catch (error) {
console.error("📛 자동 서명 필드 추가 실패:", error);
return [];
}
}
// ✅ 문서 로드 후 자동 호출되는 Hook
export function useAutoSignatureFields(instance: WebViewerInstance | null) {
const [signatureFields, setSignatureFields] = React.useState<string[]>([]);
const [isProcessing, setIsProcessing] = React.useState(false);
React.useEffect(() => {
if (!instance) return;
const { documentViewer } = instance.Core;
const handleDocumentLoaded = async () => {
try {
setIsProcessing(true);
console.log("📄 문서 로드 완료, 자동 서명 필드 생성 시작...");
// 문서 로드 후 잠시 대기 (안정성을 위해)
await new Promise(resolve => setTimeout(resolve, 1000));
const fields = await addAutoSignatureFieldsToDocument(instance);
setSignatureFields(fields);
} catch (error) {
console.error("📛 자동 서명 필드 처리 실패:", error);
} finally {
setIsProcessing(false);
}
};
documentViewer.addEventListener('documentLoaded', handleDocumentLoaded);
return () => {
documentViewer.removeEventListener('documentLoaded', handleDocumentLoaded);
};
}, [instance]);
return {
signatureFields,
isProcessing,
hasSignatureFields: signatureFields.length > 0
};
}
|