1
|
using System;
|
2
|
using System.Collections.Generic;
|
3
|
using System.Data.Objects.DataClasses;
|
4
|
using System.Drawing;
|
5
|
using System.Drawing.Imaging;
|
6
|
using System.IO;
|
7
|
using System.Linq;
|
8
|
using System.Security.Permissions;
|
9
|
using System.Threading.Tasks;
|
10
|
using DZConverterLib.DAL;
|
11
|
using System.Windows.Media.Imaging;
|
12
|
using DeepViewDataModel.DataModel;
|
13
|
using DeepViewDataModel.Common;
|
14
|
using DeepView.Toolkit.GuidGenerator;
|
15
|
using System.Threading;
|
16
|
using DZConverterLib.Event;
|
17
|
using System.Diagnostics;
|
18
|
using iTextSharp.text.pdf;
|
19
|
using iTextSharp.text.exceptions;
|
20
|
using iTextSharp.text.error_messages;
|
21
|
using iTextSharp.text;
|
22
|
using System.Globalization;
|
23
|
using System.Runtime.InteropServices;
|
24
|
using System.Reflection;
|
25
|
|
26
|
using Bytescout.PDFRenderer;
|
27
|
|
28
|
//using Leadtools;
|
29
|
//using Leadtools.Codecs;
|
30
|
using pdftron.PDF;
|
31
|
|
32
|
//using Leadtools;
|
33
|
//using Leadtools.Codecs;
|
34
|
|
35
|
//using pdftron.PDF;
|
36
|
using iTextSharp.text.pdf.parser;
|
37
|
using Leadtools.ImageProcessing;
|
38
|
using PropertiesType;
|
39
|
using System.Windows;
|
40
|
using System.Runtime.ExceptionServices;
|
41
|
|
42
|
namespace DZConverterLib
|
43
|
{
|
44
|
public class DZConverter : IDisposable
|
45
|
{
|
46
|
public string DownloadDir_PDF { get; set; }
|
47
|
public int pageCount { get; set; }
|
48
|
public CONVERTER_DOC ConverterItem { get; set; }
|
49
|
//private PDFDoc doc { get; set; }
|
50
|
public EntityCollection<DOCPAGE> pageSet { get; set; }
|
51
|
private object _lock = new object();
|
52
|
private List<Task<string>> _Task = new List<Task<string>>();
|
53
|
private List<Task<Boolean>> _Task1 = new List<Task<Boolean>>();
|
54
|
public event EventHandler<MakeConverterErrorArgs> ConverterMakeError;
|
55
|
public event EventHandler<EndConverterEventArgs> EndConverter;
|
56
|
public string ghost_path = @"\gsdll64.dll";
|
57
|
private string VipsFilePath { get; set; }
|
58
|
string DownloadFilePath;
|
59
|
|
60
|
public System.Threading.Thread FactorialThread;
|
61
|
//RasterCodecs codecs_;
|
62
|
ImageFileSaver saver_;
|
63
|
|
64
|
public DZConverter()
|
65
|
{
|
66
|
pageCount = 0;
|
67
|
pageSet = new EntityCollection<DOCPAGE>();
|
68
|
//pdftron.PDFNet.Initialize("daelim.co.kr(Doftech Corp):CPU:2::W:AMC(20120315):EF6E886F25A414FFB5F8C1F2999CF2DA33DC6C5164315BAF7011B87AF0FA");
|
69
|
}
|
70
|
|
71
|
private void SendError(int Level, string ErrorCode, string _Exception, bool ThreadStop)
|
72
|
{
|
73
|
if (ConverterMakeError != null)
|
74
|
ConverterMakeError(this, new MakeConverterErrorArgs
|
75
|
{
|
76
|
ThreadStop = ThreadStop,
|
77
|
ConverterID = ConverterItem.ID,
|
78
|
_Exception = _Exception,
|
79
|
ErrorCode = ErrorCode,
|
80
|
Level = Level
|
81
|
});
|
82
|
|
83
|
using (DeepView_Entity _entity = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString()))
|
84
|
{
|
85
|
var _items = _entity.CONVERTER_DOC.Where(converter => converter.ID == this.ConverterItem.ID);
|
86
|
if (_items.Count() > 0)
|
87
|
{
|
88
|
_items.First().EXCEPTION = ErrorCode;
|
89
|
_items.First().STATUS = 5;
|
90
|
_entity.SaveChanges();
|
91
|
}
|
92
|
}
|
93
|
|
94
|
//List<EmailModel> toEmailModels = new List<EmailModel>();
|
95
|
|
96
|
//EmailModel fromEmailModel = null;
|
97
|
|
98
|
//List<string> _lstID = new List<string>();
|
99
|
|
100
|
//using (DeepView_Entity de = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString()))
|
101
|
//{
|
102
|
// //수신자
|
103
|
// toEmailModels.Add(new EmailModel()
|
104
|
// {
|
105
|
// Host = "naver.com",
|
106
|
// UserID = "kig881111",
|
107
|
// ServiceId = GuidGenerator.GetUniqueGuid()
|
108
|
// });
|
109
|
|
110
|
|
111
|
// //발신자
|
112
|
// fromEmailModel = new EmailModel()
|
113
|
// {
|
114
|
// Host = "gmail.com",
|
115
|
// UserID = "kig881111",
|
116
|
// Smtp = "smtp.gmail.com",
|
117
|
// ServiceId = GuidGenerator.GetUniqueGuid(),
|
118
|
// SmtpPort = 587
|
119
|
// };
|
120
|
//}
|
121
|
|
122
|
EmailClient.EmailClient _client = new EmailClient.EmailClient("H2011357");
|
123
|
var _emailResult = _client.SendEmail("H2011357", string.Empty, "제목", ErrorCode, true);
|
124
|
|
125
|
return;
|
126
|
}
|
127
|
|
128
|
//Net Framework 4.5 이상에서만 가능
|
129
|
//파일에 대한 권한을 나타난다.
|
130
|
[FileIOPermission(SecurityAction.LinkDemand)]
|
131
|
public void GetSingleFile(object obj)
|
132
|
{
|
133
|
try
|
134
|
{
|
135
|
|
136
|
ConverterItem = obj as CONVERTER_DOC;
|
137
|
|
138
|
using (DeepView_Entity _deepview = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString()))
|
139
|
{
|
140
|
var _property = _deepview.PROPERTIES.Where(pro => pro.PROPERTY == ConverterItem.PROJECT_NO);
|
141
|
|
142
|
if (_property.Count() > 0)
|
143
|
{
|
144
|
var DownloadDir_PDFs = _property.Where(t => t.TYPE == PropertiesType.PropertiesType.Const_TileSorceStorage).ToList();
|
145
|
|
146
|
if (DownloadDir_PDFs.Count > 0)
|
147
|
{
|
148
|
DownloadDir_PDF = DownloadDir_PDFs.First().VALUE;
|
149
|
//테스트
|
150
|
if (!Directory.Exists(DownloadDir_PDF))
|
151
|
{
|
152
|
Directory.CreateDirectory(DownloadDir_PDF);
|
153
|
}
|
154
|
|
155
|
//var properties = _deepview.PROPERTIES.Where(t => t.TYPE == PropertiesType.PropertiesType.Const_VipsSetting);
|
156
|
|
157
|
//if(properties.Count() > 0)
|
158
|
//{
|
159
|
// VipsFilePath = properties.First().VALUE;
|
160
|
//}
|
161
|
|
162
|
//if(string.IsNullOrEmpty(VipsFilePath))
|
163
|
//{
|
164
|
// throw new Exception("프로퍼티 내 VipsSetting 라는 이름의 Type이 존재 하지 않습니다");
|
165
|
//}
|
166
|
|
167
|
}
|
168
|
else
|
169
|
{
|
170
|
throw new Exception("Don't Create Directionary to Check TileSorceStorage in Properties");
|
171
|
}
|
172
|
}
|
173
|
}
|
174
|
|
175
|
DownloadFile();
|
176
|
}
|
177
|
catch (Exception e)
|
178
|
{
|
179
|
SendError(0, "Converter Error ", e.ToString(), true);
|
180
|
}
|
181
|
}
|
182
|
|
183
|
private void SetState(IConverterPDF.ConverterStatus Status)
|
184
|
{
|
185
|
using (DeepView_Entity _entity = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString()))
|
186
|
{
|
187
|
var _items = _entity.CONVERTER_DOC.Where(converter => converter.ID == this.ConverterItem.ID);
|
188
|
if (_items.Count() > 0)
|
189
|
{
|
190
|
if (Status == IConverterPDF.ConverterStatus.Create)
|
191
|
{
|
192
|
_items.First().START_DATETIME = DateTime.Now;
|
193
|
}
|
194
|
else if (Status == IConverterPDF.ConverterStatus.Success)
|
195
|
{
|
196
|
_items.First().END_DATETIME = DateTime.Now;
|
197
|
}
|
198
|
|
199
|
_items.First().STATUS = (int)Status;
|
200
|
_entity.SaveChanges();
|
201
|
}
|
202
|
}
|
203
|
}
|
204
|
//Convert Doc 상태 변경
|
205
|
private void SetCurrentPage(int CurrentPage, IConverterPDF.ConverterStatus Status)
|
206
|
{
|
207
|
using (DeepView_Entity _entity = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString()))
|
208
|
{
|
209
|
var _items = _entity.CONVERTER_DOC.Where(converter => converter.ID == this.ConverterItem.ID);
|
210
|
if (_items.Count() > 0)
|
211
|
{
|
212
|
_items.First().STATUS = (int)Status;
|
213
|
_items.First().CURRENT_PAGE = CurrentPage;
|
214
|
_entity.SaveChanges();
|
215
|
}
|
216
|
}
|
217
|
}
|
218
|
|
219
|
System.Net.WebClient _fileFactory = new System.Net.WebClient();
|
220
|
|
221
|
[FileIOPermission(SecurityAction.LinkDemand)]
|
222
|
public void DownloadFile()
|
223
|
{
|
224
|
string DocUri = ConverterItem.DOCUMENT_URL; //PDF 전체 경로
|
225
|
string FileName = DocUri.Remove(0, DocUri.LastIndexOf("/") + 1);
|
226
|
string ProjectFolderPath = DownloadDir_PDF + "\\" + ConverterItem.PROJECT_NO + "_Tile"; //프로젝트 폴더
|
227
|
string ItemListPath = ProjectFolderPath + "\\" + (System.Convert.ToInt64(ConverterItem.DOCUMENT_ID) / 100).ToString();
|
228
|
string ItemPath = ItemListPath + "\\" + ConverterItem.DOCUMENT_ID;
|
229
|
DownloadFilePath = ItemPath + "\\" + FileName;
|
230
|
|
231
|
#region 폴더 생성
|
232
|
try
|
233
|
{
|
234
|
DirectoryInfo _dir = new DirectoryInfo(ItemPath);
|
235
|
|
236
|
if (_dir.Exists)
|
237
|
{
|
238
|
_dir.Empty(false);
|
239
|
|
240
|
}
|
241
|
else
|
242
|
{
|
243
|
Directory.CreateDirectory(ItemPath);
|
244
|
}
|
245
|
|
246
|
}
|
247
|
catch (Exception ex)
|
248
|
{
|
249
|
//DeleteDirectoryDirs(ItemPath);
|
250
|
SendError(0, "폴더삭제 에러", ex.ToString(), false);
|
251
|
}
|
252
|
|
253
|
#endregion
|
254
|
|
255
|
#region 다운로드
|
256
|
try
|
257
|
{
|
258
|
_fileFactory.UseDefaultCredentials = true;
|
259
|
|
260
|
_fileFactory.DownloadFileCompleted += (snd, evt) =>
|
261
|
{
|
262
|
|
263
|
if (File.Exists(DownloadFilePath) && evt.Error == null)
|
264
|
{
|
265
|
SetState(IConverterPDF.ConverterStatus.Create);
|
266
|
|
267
|
#region 기본 설정
|
268
|
//doc = new PDFDoc(DownloadFilePath);
|
269
|
#endregion
|
270
|
|
271
|
ConvertProcess(DownloadFilePath, ItemPath, FileName);
|
272
|
}
|
273
|
else
|
274
|
{
|
275
|
SendError(1, "File Not Found", "File Download Error" + DownloadFilePath, true);
|
276
|
}
|
277
|
};
|
278
|
|
279
|
//비동기로 파일 받아오기.
|
280
|
_fileFactory.DownloadFileAsync(new Uri(DocUri), DownloadFilePath);
|
281
|
#endregion
|
282
|
}
|
283
|
catch (Exception ex)
|
284
|
{
|
285
|
SendError(1, "File Download Error : Url - " + DocUri + " Download Path : " + DownloadFilePath, ex.ToString(), true);
|
286
|
}
|
287
|
finally
|
288
|
{
|
289
|
|
290
|
}
|
291
|
}
|
292
|
|
293
|
[FileIOPermission(SecurityAction.LinkDemand)]
|
294
|
public void ConvertProcess(string DownloadFilePath, string ItemPath, string FileName)
|
295
|
{
|
296
|
ResultSet Result_Check = new ResultSet();
|
297
|
|
298
|
#region PDF 체크
|
299
|
try
|
300
|
{
|
301
|
Result_Check = Check_PDF(DownloadFilePath);
|
302
|
}
|
303
|
catch (Exception ex)
|
304
|
{
|
305
|
System.Diagnostics.Debug.WriteLine(ex.ToString());
|
306
|
Result_Check.Message = ex.ToString();
|
307
|
Result_Check.result = false;
|
308
|
}
|
309
|
|
310
|
if (!Result_Check.result)
|
311
|
{
|
312
|
SendError(1, "PDF 체크 실패 : 보안설정으로 인한 PDF 체크 실패", Result_Check.Message, false);
|
313
|
return;
|
314
|
}
|
315
|
#endregion
|
316
|
|
317
|
#region PDF에서 이미지로 변환
|
318
|
|
319
|
try
|
320
|
{
|
321
|
//SendError(0, "SetPageInfoToDB()", "떡락이다", true);
|
322
|
|
323
|
if (EndConverter != null)
|
324
|
EndConverter(this, new EndConverterEventArgs { ConverterPDF = ConverterItem });
|
325
|
|
326
|
int _pageCount = ConvertImage(DownloadFilePath, ItemPath);
|
327
|
|
328
|
//if (_pageCount == pageSet.Count())
|
329
|
//{
|
330
|
// SetCurrentPage(_pageCount,IConverterPDF.ConverterStatus.Crop);
|
331
|
|
332
|
// var _infotoDBResult = SetPageInfoToDB(); // 데이터베이스 입력
|
333
|
|
334
|
// if (_infotoDBResult != null)
|
335
|
// SendError(0, "SetPageInfoToDB()", _infotoDBResult, true);
|
336
|
//}
|
337
|
//else
|
338
|
//{
|
339
|
// SendError(0, "converter Check", "Page Count :" + _pageCount + " PageSet : " + pageSet.Count(), true);
|
340
|
// return;
|
341
|
//}
|
342
|
}
|
343
|
catch (Exception ex)
|
344
|
{
|
345
|
SendError(0, "converter Check ", "if (!ConvertImage(ItemPath, FileName)) " + ex.ToString(), true);
|
346
|
}
|
347
|
|
348
|
#endregion
|
349
|
|
350
|
if (EndConverter != null)
|
351
|
EndConverter(this, new EndConverterEventArgs { ConverterPDF = ConverterItem });
|
352
|
}
|
353
|
//PDFNET 사용
|
354
|
public ResultSet Check_PDF(string DownloadFilePath)
|
355
|
{
|
356
|
ResultSet _result = new ResultSet();
|
357
|
//bool _Security = false;
|
358
|
//bool _isEncrypte = true;
|
359
|
|
360
|
try
|
361
|
{
|
362
|
try
|
363
|
{
|
364
|
//인구 수정
|
365
|
using (PdfReader pdfReader = new PdfReader(DownloadFilePath))
|
366
|
{
|
367
|
|
368
|
//PDF접근 권한 체크
|
369
|
if (!pdfReader.IsOpenedWithFullPermissions)
|
370
|
{
|
371
|
_result.result = false;
|
372
|
_result.Message = "Permission error";
|
373
|
}
|
374
|
else
|
375
|
{
|
376
|
_result.result = true;
|
377
|
}
|
378
|
}
|
379
|
}
|
380
|
//PDF 암호화
|
381
|
catch (BadPasswordException)
|
382
|
{
|
383
|
_result.result = false;
|
384
|
_result.Message = "Document is Encrypted";
|
385
|
}
|
386
|
//PDF 파일 형식 에러
|
387
|
catch (BadPdfFormatException)
|
388
|
{
|
389
|
_result.result = false;
|
390
|
_result.Message = "file is having invalid format";
|
391
|
}
|
392
|
|
393
|
|
394
|
//_Security = doc.InitSecurityHandler();
|
395
|
//_isEncrypte = doc.IsEncrypted();
|
396
|
|
397
|
////접근권한 체크
|
398
|
//if (!_Security)
|
399
|
//{
|
400
|
// _result.result = false;
|
401
|
// _result.Message = "Document authentication Error";
|
402
|
//}
|
403
|
//else
|
404
|
// if (_isEncrypte)
|
405
|
// {
|
406
|
// _result.result = false;
|
407
|
// _result.Message = "Document is Encrypted";
|
408
|
// }
|
409
|
// else
|
410
|
// _result.result = true;
|
411
|
|
412
|
}
|
413
|
catch (Exception ex)
|
414
|
{
|
415
|
System.Diagnostics.Debug.WriteLine(ex.ToString());
|
416
|
_result.Message = ex.ToString();
|
417
|
_result.result = false;
|
418
|
}
|
419
|
|
420
|
return _result;
|
421
|
}
|
422
|
|
423
|
|
424
|
|
425
|
private int ConvertImage(string DownloadFilePath, string FilePath)
|
426
|
{
|
427
|
try
|
428
|
{
|
429
|
using (PdfReader pdfReader = new PdfReader(DownloadFilePath))
|
430
|
{
|
431
|
using (DeepView_Entity _entity = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString().ToString()))
|
432
|
{
|
433
|
var _items = _entity.CONVERTER_DOC.Where(converter => converter.ID == this.ConverterItem.ID);
|
434
|
|
435
|
if (_items.Count() > 0)
|
436
|
{
|
437
|
//인구 수정
|
438
|
pageCount = pdfReader.NumberOfPages;
|
439
|
//pageCount = doc.GetPageCount();
|
440
|
|
441
|
_items.First().TOTAL_PAGE = pageCount;
|
442
|
|
443
|
_entity.SaveChanges();
|
444
|
|
445
|
createImg(DownloadFilePath, FilePath);
|
446
|
}
|
447
|
}
|
448
|
}
|
449
|
}
|
450
|
catch (Exception ex)
|
451
|
{
|
452
|
SendError(0, "code 1230saf325 Error :", ex.ToString(), true);
|
453
|
}
|
454
|
finally
|
455
|
{
|
456
|
//doc.Close();
|
457
|
//doc.Dispose();
|
458
|
//pdftron.PDFNet.Terminate();
|
459
|
//doc = null;
|
460
|
}
|
461
|
|
462
|
return pageCount;
|
463
|
}
|
464
|
|
465
|
#region 원본
|
466
|
//public void Raster()
|
467
|
//{
|
468
|
// RasterImage srcImage = (RasterImage)null;
|
469
|
// RasterImage destImage = (RasterImage)null;
|
470
|
// int cnt = 0;
|
471
|
// Task.Factory.StartNew((Action)(() =>
|
472
|
// {
|
473
|
// RasterCodecs codecs_ = new RasterCodecs();
|
474
|
// CodecsImageInfo information = codecs_.GetInformation(this.DownloadFilePath, true);
|
475
|
|
476
|
// Parallel.For(1, information.TotalPages + 1, new ParallelOptions()
|
477
|
// {
|
478
|
// MaxDegreeOfParallelism = 2
|
479
|
// }, (Action<int>)(i =>
|
480
|
// {
|
481
|
// codecs_ = new RasterCodecs();
|
482
|
|
483
|
// srcImage = codecs_.Load(this.DownloadFilePath, i);
|
484
|
|
485
|
// //if (i == 1)
|
486
|
// //{
|
487
|
// //Thread.Sleep(100);
|
488
|
// //}
|
489
|
|
490
|
// //EntityCollection<DOCPAGE> pageSet = this.pageSet;
|
491
|
|
492
|
// DOCPAGE entity = new DOCPAGE()
|
493
|
// {
|
494
|
// ID = GuidGenerator.shortGuid(),
|
495
|
// PAGE_WIDTH = srcImage.Width.ToString(),
|
496
|
// PAGE_HEIGHT = srcImage.Height.ToString(),
|
497
|
// PAGE_NUMBER = i,
|
498
|
// PAGE_ANGLE = 0
|
499
|
// };
|
500
|
// pageSet.Add(entity);
|
501
|
// ++cnt;
|
502
|
|
503
|
// //if (i == 1)
|
504
|
// //{
|
505
|
// //Thread.Sleep(100);
|
506
|
// //}
|
507
|
|
508
|
// codecs_.Save(srcImage, System.IO.Path.GetDirectoryName(this.DownloadFilePath) + "\\" + i + ".cmp", (RasterImageFormat)5, 24);
|
509
|
// destImage = new RasterImage((RasterMemoryFlags)1, srcImage.Width / 4, srcImage.Height / 4, srcImage.BitsPerPixel, srcImage.Order, srcImage.ViewPerspective, srcImage.GetPalette(), IntPtr.Zero, 0L);
|
510
|
// ResizeCommand resizeCommand = new ResizeCommand();
|
511
|
// resizeCommand.DestinationImage = destImage;
|
512
|
// resizeCommand.Flags = (RasterSizeFlags)4;
|
513
|
// ((RasterCommand)resizeCommand).Run(srcImage);
|
514
|
// codecs_.Save(destImage, System.IO.Path.GetDirectoryName(this.DownloadFilePath) + "\\" + i + ".jpg", (RasterImageFormat)10, 24);
|
515
|
|
516
|
// this.SetCurrentPage(cnt, IConverterPDF.ConverterStatus.Crop);
|
517
|
// }));
|
518
|
|
519
|
// if (information.TotalPages == pageSet.Count())
|
520
|
// {
|
521
|
// SetState(IConverterPDF.ConverterStatus.Success);
|
522
|
|
523
|
// //데이터 베이스 입력 위치 변경
|
524
|
// var _infotoDBResult = SetPageInfoToDB(); // 데이터베이스 입력
|
525
|
|
526
|
// if (_infotoDBResult != null)
|
527
|
// SendError(0, "SetPageInfoToDB()", _infotoDBResult, true);
|
528
|
|
529
|
// codecs_.Dispose();
|
530
|
// }
|
531
|
|
532
|
// //if (information.TotalPages != cnt)
|
533
|
// // return;
|
534
|
|
535
|
// //string db = this.SetPageInfoToDB();
|
536
|
|
537
|
// //if (db != null)
|
538
|
// //{
|
539
|
// // this.SendError(0, db, db, true);
|
540
|
// //}
|
541
|
|
542
|
// //if (information.TotalPages == cnt)
|
543
|
// //if (information.TotalPages == pageSet.Count())
|
544
|
// //{
|
545
|
// // this.SetCurrentPage(0, IConverterPDF.ConverterStatus.Success);
|
546
|
|
547
|
// // //데이터 베이스 입력 위치 변경
|
548
|
// // var _infotoDBResult = SetPageInfoToDB(); // 데이터베이스 입력
|
549
|
|
550
|
// // if (_infotoDBResult != null)
|
551
|
// // SendError(0, "SetPageInfoToDB()", _infotoDBResult, true);
|
552
|
|
553
|
// //}
|
554
|
// //codecs_.Dispose();
|
555
|
|
556
|
|
557
|
// }));
|
558
|
//}
|
559
|
#endregion
|
560
|
|
561
|
[HandleProcessCorruptedStateExceptions]
|
562
|
public void Raster()
|
563
|
{
|
564
|
int cnt = 0;
|
565
|
|
566
|
//RasterRenderer renderer = new RasterRenderer();
|
567
|
//renderer.RegistrationName = "demo";
|
568
|
//renderer.RegistrationKey = "demo";
|
569
|
//renderer.LoadDocumentFromFile(this.DownloadFilePath);
|
570
|
|
571
|
//BitmapMetadata destination_PNG = new BitmapMetadata("PNG");
|
572
|
//BitmapMetadata destination_JPG = new BitmapMetadata("JPG");
|
573
|
PDFDoc doc = new PDFDoc(this.DownloadFilePath);
|
574
|
|
575
|
ParallelOptions po = new ParallelOptions();
|
576
|
po.MaxDegreeOfParallelism = 1;
|
577
|
string ImageName_P = "";
|
578
|
string ImageName_J = "";
|
579
|
int _pageNo = 0;
|
580
|
|
581
|
Task.Factory.StartNew(() =>
|
582
|
{
|
583
|
PageIterator itr = doc.GetPageIterator();
|
584
|
RasterRenderer renderer = new RasterRenderer();
|
585
|
renderer.RegistrationName = "demo";
|
586
|
renderer.RegistrationKey = "demo";
|
587
|
renderer.LoadDocumentFromFile(this.DownloadFilePath);
|
588
|
|
589
|
Parallel.For(1, doc.GetPageCount() + 1, po, (num) =>
|
590
|
{
|
591
|
using (PDFDraw draw = new PDFDraw())
|
592
|
{
|
593
|
try
|
594
|
{
|
595
|
int a = doc.GetPageCount();
|
596
|
//throw new AccessViolationException();
|
597
|
_pageNo = itr.GetPageNumber();
|
598
|
Page _page = itr.Current();
|
599
|
//Page _page = doc.GetPage(27);
|
600
|
itr.Next();
|
601
|
|
602
|
int widthData = (int)_page.GetPageWidth();
|
603
|
int heightData = (int)_page.GetPageHeight();
|
604
|
|
605
|
ImageName_P = System.IO.Path.GetDirectoryName(this.DownloadFilePath) + "\\" + _pageNo + ".png";
|
606
|
ImageName_J = System.IO.Path.GetDirectoryName(this.DownloadFilePath) + "\\" + _pageNo + ".jpg";
|
607
|
|
608
|
draw.SetDPI(150);
|
609
|
draw.Export(_page, ImageName_P, "PNG");
|
610
|
draw.SetDPI(50);
|
611
|
draw.Export(_page, ImageName_J, "JPEG");
|
612
|
}
|
613
|
//catch (Exception ex)
|
614
|
catch (AccessViolationException ex)
|
615
|
{
|
616
|
renderer.Save(ImageName_P, RasterImageFormat.PNG, _pageNo, 150);
|
617
|
renderer.Save(ImageName_J, RasterImageFormat.JPEG, _pageNo, 50);
|
618
|
}
|
619
|
catch (NullReferenceException)
|
620
|
{
|
621
|
|
622
|
}
|
623
|
catch(Exception)
|
624
|
{
|
625
|
|
626
|
}
|
627
|
finally
|
628
|
{
|
629
|
|
630
|
}
|
631
|
++cnt;
|
632
|
this.SetCurrentPage(cnt, IConverterPDF.ConverterStatus.Crop);
|
633
|
|
634
|
//GC.Collect(2);
|
635
|
//GC.WaitForPendingFinalizers();
|
636
|
|
637
|
System.Drawing.Image image = System.Drawing.Image.FromFile(ImageName_P);
|
638
|
|
639
|
DOCPAGE entity = new DOCPAGE()
|
640
|
{
|
641
|
ID = GuidGenerator.shortGuid(),
|
642
|
PAGE_WIDTH = image.Width.ToString(),
|
643
|
PAGE_HEIGHT = image.Height.ToString(),
|
644
|
PAGE_NUMBER = _pageNo,
|
645
|
PAGE_ANGLE = 0
|
646
|
};
|
647
|
pageSet.Add(entity);
|
648
|
}
|
649
|
});
|
650
|
if (doc.GetPageCount() == pageSet.Count())
|
651
|
{
|
652
|
SetState(IConverterPDF.ConverterStatus.Success);
|
653
|
|
654
|
//데이터 베이스 입력 위치 변경
|
655
|
var _infotoDBResult = SetPageInfoToDB(); // 데이터베이스 입력
|
656
|
|
657
|
if (_infotoDBResult != null)
|
658
|
SendError(0, "SetPageInfoToDB()", _infotoDBResult, true);
|
659
|
}
|
660
|
});
|
661
|
|
662
|
//for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())
|
663
|
// {
|
664
|
// int a = doc.GetPageCount();
|
665
|
|
666
|
// PageIterator itr1 = doc.GetPageIterator();
|
667
|
// itr.Next();
|
668
|
|
669
|
// Page _page = itr.Current();
|
670
|
// int _pageNo = itr.GetPageNumber();
|
671
|
|
672
|
// using (PDFDraw draw = new PDFDraw())
|
673
|
// {
|
674
|
// int widthData = (int)_page.GetPageWidth();
|
675
|
// int heightData = (int)_page.GetPageHeight();
|
676
|
|
677
|
// draw.SetDPI(150);
|
678
|
// //draw.SetAntiAliasing(true);
|
679
|
// //draw.SetImageSmoothing(true);
|
680
|
// //draw.SetThinLineAdjustment(true, true);
|
681
|
|
682
|
// draw.Export(_page, System.IO.Path.GetDirectoryName(this.DownloadFilePath) + "\\" + _pageNo + ".png", "PNG");
|
683
|
// draw.SetDPI(50);
|
684
|
// draw.Export(_page, System.IO.Path.GetDirectoryName(this.DownloadFilePath) + "\\" + _pageNo + ".jpg", "JPEG");
|
685
|
|
686
|
// //BitmapFrame frameToCache_P = BitmapFrame.Create(draw.GetBitmapSource(_page), null, destination_PNG, null);
|
687
|
// //BitmapEncoder encoder_P = new PngBitmapEncoder();
|
688
|
// //encoder_P.Frames.Add(frameToCache_P);
|
689
|
// //string ImageName_P = System.IO.Path.GetDirectoryName(this.DownloadFilePath) + "\\" + _pageNo + ".png";
|
690
|
|
691
|
// //draw.SetDPI(50);
|
692
|
// //BitmapFrame frameToCache_J = BitmapFrame.Create(draw.GetBitmapSource(_page), null, destination_JPG, null);
|
693
|
// //BitmapEncoder encoder_J = new JpegBitmapEncoder();
|
694
|
// //encoder_J.Frames.Add(frameToCache_J);
|
695
|
// //string ImageName_J = System.IO.Path.GetDirectoryName(this.DownloadFilePath) + "\\" + _pageNo + ".jpg";
|
696
|
|
697
|
|
698
|
// //using (FileStream stream = new FileStream(ImageName_P, FileMode.Create))
|
699
|
// //{
|
700
|
// // encoder_P.Save(stream);
|
701
|
// //}
|
702
|
|
703
|
// //using (FileStream stream = new FileStream(ImageName_J, FileMode.Create))
|
704
|
// //{
|
705
|
// // encoder_J.Save(stream);
|
706
|
// //}
|
707
|
|
708
|
// //System.Drawing.Image image = System.Drawing.Image.FromFile(ImageName_P);
|
709
|
|
710
|
// //encoder_P.Frames.Clear();
|
711
|
// //encoder_J.Frames.Clear();
|
712
|
// //destination_PNG = null;
|
713
|
// //destination_JPG = null;
|
714
|
|
715
|
// //DOCPAGE entity = new DOCPAGE()
|
716
|
// //{
|
717
|
// // ID = GuidGenerator.shortGuid(),
|
718
|
// // PAGE_WIDTH = image.Width.ToString(),
|
719
|
// // PAGE_HEIGHT = image.Height.ToString(),
|
720
|
// // PAGE_NUMBER = _pageNo,
|
721
|
// // PAGE_ANGLE = 0
|
722
|
// //};
|
723
|
// //pageSet.Add(entity);
|
724
|
|
725
|
// ++cnt;
|
726
|
// this.SetCurrentPage(cnt, IConverterPDF.ConverterStatus.Crop);
|
727
|
|
728
|
// GC.Collect(2);
|
729
|
// GC.WaitForPendingFinalizers();
|
730
|
// }
|
731
|
|
732
|
// //Parallel.For(1, renderer.GetPageCount() + 1, po, (num) =>
|
733
|
// //{
|
734
|
// // //renderer.Save(System.IO.Path.GetDirectoryName(this.DownloadFilePath) + "\\" + num + ".png", Bytescout.PDFRenderer.RasterImageFormat.PNG, num - 1, 150);
|
735
|
// // //System.Drawing.Image img = renderer.GetImage(1, 150);
|
736
|
|
737
|
// // //img.Save(System.IO.Path.GetDirectoryName(this.DownloadFilePath) + "\\" + num + ".png");
|
738
|
|
739
|
|
740
|
// // renderer.Save(System.IO.Path.GetDirectoryName(this.DownloadFilePath) + "\\" + num + ".jpg", Bytescout.PDFRenderer.RasterImageFormat.PNG, num - 1, 50);
|
741
|
|
742
|
// // System.Drawing.Image getimg = System.Drawing.Image.FromFile(System.IO.Path.GetDirectoryName(this.DownloadFilePath) + "\\" + num + ".png");
|
743
|
|
744
|
// // //Uri uri = new Uri(ConverterItem.DOCUMENT_URL.Replace(ConverterItem.DOCUMENT_URL.Split('/').Last(), num + ".png"));
|
745
|
// // //iTextSharp.text.Image getimg = iTextSharp.text.Image.GetInstance(uri);
|
746
|
|
747
|
// // DOCPAGE entity = new DOCPAGE()
|
748
|
// // {
|
749
|
// // ID = GuidGenerator.shortGuid(),
|
750
|
// // PAGE_WIDTH = getimg.Width.ToString(),
|
751
|
// // PAGE_HEIGHT = getimg.Height.ToString(),
|
752
|
// // PAGE_NUMBER = num,
|
753
|
// // PAGE_ANGLE = 0
|
754
|
// // };
|
755
|
// // pageSet.Add(entity);
|
756
|
|
757
|
// // ++cnt;
|
758
|
// // this.SetCurrentPage(cnt, IConverterPDF.ConverterStatus.Crop);
|
759
|
// //});
|
760
|
|
761
|
// if (renderer.GetPageCount() == pageSet.Count())
|
762
|
// {
|
763
|
// SetState(IConverterPDF.ConverterStatus.Success);
|
764
|
|
765
|
// //데이터 베이스 입력 위치 변경
|
766
|
// var _infotoDBResult = SetPageInfoToDB(); // 데이터베이스 입력
|
767
|
|
768
|
// if (_infotoDBResult != null)
|
769
|
// SendError(0, "SetPageInfoToDB()", _infotoDBResult, true);
|
770
|
|
771
|
// //codecs_.Dispose();
|
772
|
// }
|
773
|
//}
|
774
|
|
775
|
}
|
776
|
|
777
|
[HandleProcessCorruptedStateExceptions]
|
778
|
private void createImg(string DownloadFilePath, string FilePath)
|
779
|
{
|
780
|
//라이선스 체크
|
781
|
Support.SetLicense();
|
782
|
|
783
|
//RasterCodecs codecs = new RasterCodecs();
|
784
|
ImageFileSaver saver = new ImageFileSaver();
|
785
|
|
786
|
//this.codecs_ = new RasterCodecs();
|
787
|
this.saver_ = new ImageFileSaver();
|
788
|
this.saver_.FileName = DownloadFilePath;
|
789
|
try
|
790
|
{
|
791
|
this.saver_.pageSet = new EntityCollection<DOCPAGE>();
|
792
|
this.Raster();
|
793
|
}
|
794
|
catch (Exception ex)
|
795
|
{
|
796
|
}
|
797
|
}
|
798
|
|
799
|
private string SetPageInfoToDB()
|
800
|
{
|
801
|
try
|
802
|
{
|
803
|
#region DB에 페이지 정보 입력
|
804
|
|
805
|
using (DeepView_Entity deepViewEntity = new DeepView_Entity(ConnectStringBuilder.DeepViewConnectionString(ConverterItem.PROJECT_NO).ToString()))
|
806
|
{
|
807
|
var _docinfo = deepViewEntity
|
808
|
.DOCINFO.Where(info => info.ORIGINAL_FILE == ConverterItem.DOCUMENT_URL && info.DOCUMENT_ID == ConverterItem.DOCUMENT_ID && info.PAGE_COUNT == pageCount);
|
809
|
|
810
|
//기존 DocInfo와 동일한 경우
|
811
|
if (_docinfo.Count() > 0)
|
812
|
{
|
813
|
System.Diagnostics.Stopwatch _watch = new System.Diagnostics.Stopwatch();
|
814
|
_watch.Start();
|
815
|
|
816
|
var _doc = _docinfo.First();
|
817
|
_doc.DOCUMENT_ID = ConverterItem.DOCUMENT_ID;
|
818
|
_doc.PAGE_COUNT = pageCount;
|
819
|
|
820
|
pageSet.AsParallel().ForAll(p => p.DOCINFO_ID = _doc.ID);
|
821
|
|
822
|
var _exceptDocInfo = (from newinfo in pageSet
|
823
|
select new { newinfo.PAGE_NUMBER, newinfo.DOCINFO_ID, newinfo.PAGE_WIDTH, newinfo.PAGE_HEIGHT }).Except(from oldinfo in _doc.DOCPAGE select new { oldinfo.PAGE_NUMBER, oldinfo.DOCINFO_ID, oldinfo.PAGE_WIDTH, oldinfo.PAGE_HEIGHT });
|
824
|
|
825
|
|
826
|
var _delDocInfo = (from oldinfo in _doc.DOCPAGE
|
827
|
select new { oldinfo.PAGE_NUMBER, oldinfo.DOCINFO_ID, oldinfo.PAGE_WIDTH, oldinfo.PAGE_HEIGHT }).Except
|
828
|
(from newinfo in pageSet
|
829
|
select new { newinfo.PAGE_NUMBER, newinfo.DOCINFO_ID, newinfo.PAGE_WIDTH, newinfo.PAGE_HEIGHT }).ToList();
|
830
|
|
831
|
|
832
|
foreach (var item in _delDocInfo)
|
833
|
{
|
834
|
var _delitem = _doc.DOCPAGE.Where(p => p.PAGE_NUMBER == item.PAGE_NUMBER).First();
|
835
|
deepViewEntity.DOCPAGE.DeleteObject(_delitem);
|
836
|
}
|
837
|
deepViewEntity.SaveChanges();
|
838
|
|
839
|
|
840
|
foreach (var exceptitem in _exceptDocInfo)
|
841
|
{
|
842
|
var _lstPage = deepViewEntity.DOCPAGE.Where(page => page.PAGE_NUMBER == exceptitem.PAGE_NUMBER
|
843
|
&& page.DOCINFO_ID == exceptitem.DOCINFO_ID);
|
844
|
|
845
|
if (_lstPage.Count() > 0)
|
846
|
{
|
847
|
var _page = _lstPage.First();
|
848
|
_page.PAGE_HEIGHT = exceptitem.PAGE_HEIGHT;
|
849
|
_page.PAGE_WIDTH = exceptitem.PAGE_WIDTH;
|
850
|
}
|
851
|
else
|
852
|
{
|
853
|
_doc.DOCPAGE.Add(pageSet.Where(p => p.PAGE_NUMBER == exceptitem.PAGE_NUMBER).First());
|
854
|
}
|
855
|
}
|
856
|
|
857
|
deepViewEntity.SaveChanges();
|
858
|
|
859
|
_watch.Stop();
|
860
|
System.Diagnostics.Debug.WriteLine(_watch.ElapsedMilliseconds);
|
861
|
}
|
862
|
else
|
863
|
{
|
864
|
//DocInfo가 있을때 지우고 만들기
|
865
|
var docinfo_ = deepViewEntity.DOCINFO.Where(info => info.DOCUMENT_ID == ConverterItem.DOCUMENT_ID);
|
866
|
|
867
|
if (docinfo_.Count() > 0)
|
868
|
{
|
869
|
var _doc = docinfo_.First();
|
870
|
|
871
|
var _delDocInfo = (from oldinfo in _doc.DOCPAGE
|
872
|
select new { oldinfo.PAGE_NUMBER, oldinfo.DOCINFO_ID, oldinfo.PAGE_WIDTH, oldinfo.PAGE_HEIGHT }).Except
|
873
|
(from newinfo in pageSet
|
874
|
select new { newinfo.PAGE_NUMBER, newinfo.DOCINFO_ID, newinfo.PAGE_WIDTH, newinfo.PAGE_HEIGHT }).ToList();
|
875
|
|
876
|
foreach (var item in _delDocInfo)
|
877
|
{
|
878
|
var _delitem = _doc.DOCPAGE.Where(p => p.PAGE_NUMBER == item.PAGE_NUMBER).First();
|
879
|
deepViewEntity.DOCPAGE.DeleteObject(_delitem);
|
880
|
}
|
881
|
deepViewEntity.DOCINFO.DeleteObject(_doc);
|
882
|
|
883
|
deepViewEntity.SaveChanges();
|
884
|
}
|
885
|
|
886
|
Guid guid = GuidGenerator.GetUniqueGuid();
|
887
|
DOCINFO instace = new DOCINFO
|
888
|
{
|
889
|
ID = guid.ToString(),
|
890
|
PROJECT_NO = ConverterItem.PROJECT_NO,
|
891
|
ORIGINAL_FILE = ConverterItem.DOCUMENT_URL,
|
892
|
DOCUMENT_ID = ConverterItem.DOCUMENT_ID,
|
893
|
PAGE_COUNT = pageCount,
|
894
|
DOCPAGE = pageSet
|
895
|
};
|
896
|
//DocInfo ADD에러
|
897
|
deepViewEntity.DOCINFO.AddObject(instace);
|
898
|
}
|
899
|
deepViewEntity.SaveChanges();
|
900
|
}
|
901
|
}
|
902
|
catch (Exception e)
|
903
|
{
|
904
|
return e.ToString();
|
905
|
}
|
906
|
|
907
|
#endregion
|
908
|
|
909
|
return null;
|
910
|
}
|
911
|
|
912
|
#region inheritance Method
|
913
|
public void Dispose()
|
914
|
{
|
915
|
if (_fileFactory != null)
|
916
|
{
|
917
|
_fileFactory.Dispose();
|
918
|
_fileFactory = null;
|
919
|
}
|
920
|
|
921
|
GC.Collect();
|
922
|
GC.WaitForPendingFinalizers();
|
923
|
}
|
924
|
#endregion
|
925
|
}
|
926
|
}
|
927
|
|