프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / Controls / PrintControl.xaml.cs @ 77cdac33

이력 | 보기 | 이력해설 | 다운로드 (40.7 KB)

1 787a4489 KangIngu
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Windows;
5
using System.Windows.Controls;
6
using System.Windows.Media;
7
using Telerik.Windows.Controls;
8
using System.Collections.ObjectModel;
9
using System.Drawing.Printing;
10
using System.Windows.Media.Imaging;
11
using KCOMDataModel.DataModel;
12
using Microsoft.Win32;
13
using System.IO;
14
using System.Timers;
15
//using IKCOM.Common;
16
using KCOM.Event;
17
using iTextSharp;
18
using iTextSharp.text;
19
//using Leadtools.Windows.Controls;
20
using IKCOM;
21
using iTextSharp.text.pdf;
22
using Microsoft.Office.Core;
23 481c3fb9 djkim
using System.Net;
24 86bff326 taeseongkim
using Image = System.Windows.Controls.Image;
25 e3a15e22 djkim
26 787a4489 KangIngu
namespace KCOM.Control
27
{
28
    public class DefinedPages
29
    {
30 86bff326 taeseongkim
        public DOCINFO DocumentInfo { get; set; }
31
32 787a4489 KangIngu
        public int PagesCount { get; set; }
33
        public string vpSlip { get; set; }
34
        public string vpTitle { get; set; }
35
        public string fileUrl { get; set; }
36
        public ImageBrush Back_Image { get; set; }
37
38 86bff326 taeseongkim
        public double CurrentValue { get; set; }
39
40 787a4489 KangIngu
        string _DefinedPagesStrings;
41
        /// <summary>
42
        /// 사용자 정의 페이지를 입력시 오류를 방지하기 위해 적용
43
        /// </summary>
44
        public string DefinedPagesStrings
45
        {
46
            get { return _DefinedPagesStrings; }
47
            set
48
            {
49
                if (string.IsNullOrWhiteSpace(value)) return;
50
51
                var _definedPages = value.Replace('-', ',').Split(',');
52
                List<char> _NotEx = new List<char>();
53
54
                foreach (var item in _definedPages)
55
                {
56
                    bool _isNum = true;
57
58
                    foreach (var chr in item)
59
                    {
60
                        if (!char.IsNumber(chr))
61
                        {
62
                            _NotEx.Add(chr);
63
                            _isNum = false;
64
                        }
65
                    }
66
67
                    if (_isNum)
68
                    {
69
                        if (Convert.ToInt32(item) > PagesCount)
70
                            throw new Exception(string.Format("Max Page Number is '{0}'!!.", PagesCount));
71
                    }
72
                }
73
74
                if (_NotEx.Count() > 0)
75
                {
76
                    string _notString = "";
77
                    _NotEx.ForEach(s => _notString += s.ToString());
78
                    throw new Exception(string.Format("'{0}' Can not be added.", _notString));
79
                }
80
81
82
                try
83
                {
84
85
                    string _notString2 = "";
86
                    _NotEx.ForEach(s => _notString2 += s.ToString());
87
                    _DefinedPagesStrings = value;
88
                }
89
                catch (Exception)
90
                {
91
                    throw new Exception(string.Format("Can not be added."));
92
                }
93
94
            }
95
        }
96
    }
97
98
    /// <summary>
99
    /// Interaction logic for Print.xaml
100
    /// </summary>
101
    public partial class PrintControl : UserControl
102
    {
103
        #region Data
104
105
        string ProjectNo = null; //프로젝트 넘버
106
        string _DefaultTileUri = null; //기본 타일 경로
107
        int _StartPageNo; //시작페이지 
108
        DOCINFO _DocInfo; //문서 정보
109
        //DocInfo _DocInfo; //문서 정보
110
        bool currentPagePrint = false; //현재 페이지 수
111
        int PageCount { get; set; } //총 페이지수 
112
        DefinedPages _definePages = null; //인쇄 설정 범위 지정
113
        public bool IsPrint { get; set; }
114
115
        PrintDocument printDocument = new PrintDocument(); //프린터도큐먼트 생성
116
117
        //LayerControl LayerControl = null; // 레이어 컨트롤
118
        ObservableCollection<IKCOM.MarkupInfoItem> _markupInfo = new ObservableCollection<IKCOM.MarkupInfoItem>(); //마크업 정보
119
        List<MarkupPageItem> _PageMarkupList = new List<MarkupPageItem>();
120
        List<SetColorMarkupItem> _LoadMakupList = new List<SetColorMarkupItem>();
121
        List<int> _lstPrintPageNo = new List<int>();
122
123
        System.Windows.Threading.DispatcherTimer tm = new System.Windows.Threading.DispatcherTimer();
124
        bool _initializeComponentFinished; //이벤트
125
        bool _IsDagSlider = false; //드래그 상태인가
126 ca49aa5b taeseongkim
        bool IsExportOrPrint = false; 
127
128 787a4489 KangIngu
        List<DisplayColorInfo> colorList = new List<DisplayColorInfo>();
129
        DefinedPages DocumentInfo { get; set; } //문서정보 정의        
130
        //PdfSharp.Pdf.PdfDocument document { get; set; } // pdfsharp 인데 아직 왜 넣었는지 모름 
131
        delegate void PrintEventHandler(); //프린트 핸들러
132
        SaveFileDialog SaveDig = new SaveFileDialog(); //파일 세이브 다이얼로그
133
        //SaveFileDialog SaveFile { get; set; } //저장할 때 사용
134
135
        //RasterImageViewer _viewer; //이미지 뷰어
136 53880c83 ljiyeon
        //System.Windows.Controls.Image backimg; //백그라운드 이미지
137 787a4489 KangIngu
        System.Drawing.Image Printimg; //프린트 이미지
138
        System.Drawing.Image p_img; //프린트 이미지
139
        iTextSharp.text.Image Export_img;
140 481c3fb9 djkim
        Dictionary<int, System.Drawing.Image> Printimg_List; //프린트 이미지
141 77cdac33 taeseongkim
        Dictionary<int, string> PrintimgPath_List; //프린트 이미지 파일 경로
142
        string TempImageDir = null;
143 787a4489 KangIngu
144
        //RasterCodecs codecs;
145
        int currentPage;
146
        public string PrintName;
147
        public string Type;
148
149
        public System.Timers.Timer timm = new System.Timers.Timer();
150
151
152
        #endregion
153
154
        #region Static Property Defines
155
        public static readonly DependencyProperty CurrentPageNoProperty =
156
                   DependencyProperty.Register("CurrentPageNo", typeof(int), typeof(PrintControl),
157
                   new PropertyMetadata(new PropertyChangedCallback(CurrentPageNoPropertyChanged)));
158
        #endregion
159
160
        #region Property
161
        public int CurrentPageNo
162
        {
163
            get { return (int)GetValue(CurrentPageNoProperty); }
164
            set { SetValue(CurrentPageNoProperty, value); }
165
        }
166
        #endregion
167
168
        #region PropertyChangeMethod
169
        private static void CurrentPageNoPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
170
        {
171
            var printPage = (PrintControl)d;
172
            var newValue = (int)e.NewValue;
173
174
            printPage.sliderPages.Value = newValue;
175
            printPage.SetLoadMakupList(newValue);
176
177
        }
178
        #endregion
179
180
        #region 생성자
181 481c3fb9 djkim
182 787a4489 KangIngu
        public PrintControl(string TileSourceUri, string ProjectNo, DOCINFO DocInfo, List<IKCOM.MarkupInfoItem> markupInfo, int CurrentPageNo, string slip, string title, string fileurl, bool isPrint)
183
        {
184
            InitializeComponent();
185 481c3fb9 djkim
186 787a4489 KangIngu
            this.IsEnabled = true;
187
188
            //Layer Control을 통째로 가져와야 하는건가
189
            this._DocInfo = DocInfo;
190
            this._DefaultTileUri = TileSourceUri;
191
            this.ProjectNo = ProjectNo;
192
            this._StartPageNo = CurrentPageNo;
193
194
            markupInfo.ForEach(info => this._markupInfo.Add(info));
195
196
            foreach (var info in _markupInfo)
197
            {
198
                if (info.Consolidate == 1)
199
                {
200
                    info.UserName = "Consolidation";
201
                }
202
                info.MarkupList.ForEach(makup =>
203
                {
204
                    var _pageMarkup = _PageMarkupList.Where(item => item.PageNumber == makup.PageNumber);
205
                    var _SetMarkupItem = new SetColorMarkupItem { markupID = makup.ID, DisplayColor = info.DisplayColor };
206
207
                    if (_pageMarkup.Count() > 0)
208
                        _pageMarkup.First().DisplayColorItems.Add(_SetMarkupItem);
209
                    else
210
                        _PageMarkupList.Add(new MarkupPageItem
211
                        {
212
                            PageNumber = makup.PageNumber,
213
                            DisplayColorItems = new List<SetColorMarkupItem> { _SetMarkupItem }
214
                        });
215
                });
216
217
                colorList.Add(new DisplayColorInfo
218
                {
219
                    UserID = info.UserID,
220
                    DisplayColor = info.DisplayColor,
221
                    Department = info.Depatment,
222
                    UserName = info.UserName,
223
                });
224
            }
225
226
            gridViewMarkup.ItemsSource = this._markupInfo;
227
            SetLoadMakupList(this._StartPageNo);
228
            if (_LoadMakupList.Count() == 0)
229
                chkOnlyMarkup.IsChecked = false;
230
231
            this.CurrentPageNo = _StartPageNo;
232 86bff326 taeseongkim
233
            _definePages = new DefinedPages { DefinedPagesStrings = "", DocumentInfo = this._DocInfo
234
                            , PagesCount = Convert.ToInt32(this._DocInfo.PAGE_COUNT), vpSlip = slip, vpTitle = title, fileUrl = fileurl };
235
236 787a4489 KangIngu
            DocumentInfo = _definePages;
237
            this.DataContext = _definePages;
238
239
            CheckCommentPages();
240
241
            //초기화면 Comment Check된 상태로 보여주기
242
            foreach (var item in _markupInfo)
243
            {
244
                gridViewMarkup.SelectedItems.Add(item);
245
            }
246
247 77cdac33 taeseongkim
            PageChangeAsync(Common.ViewerDataModel.Instance.NewCancellationToken(), _StartPageNo,false).ConfigureAwait(true);
248 787a4489 KangIngu
249
            sliderPages.Maximum = Convert.ToDouble(this._DocInfo.PAGE_COUNT);
250
            this.stPageNo.Maximum = Convert.ToDouble(this._DocInfo.PAGE_COUNT);
251
            this.edPageNo.Maximum = Convert.ToDouble(this._DocInfo.PAGE_COUNT);
252
            this.IsPrint = isPrint;
253
254
            if (!IsPrint)
255
            {
256
                GetPrint(false);
257
                btnWholeExport.Visibility = Visibility.Visible;
258
                btnWholePrint.Visibility = Visibility.Collapsed;
259
                Selected_Print.Header = "Export Type";
260
            }
261
            else
262
            {
263
                //PrintList 가져오기
264
                GetPrint(true);
265
                btnWholePrint.Visibility = Visibility.Visible;
266
                btnWholeExport.Visibility = Visibility.Collapsed;
267
            }
268
269
            _initializeComponentFinished = true;
270
271
            //timm.Interval = 10;
272
            //timm.Elapsed += new System.Timers.ElapsedEventHandler(_Timer_Elapsed);
273
            //timm.Enabled = true;
274
        }
275
276
        #endregion
277
278
        #region Control Event
279 86bff326 taeseongkim
        private async void sliderPages_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
280 787a4489 KangIngu
        {
281
            if (_initializeComponentFinished)
282
            {
283
                if (!_IsDagSlider)
284
                {
285
                    this.CurrentPageNo = (int)this.sliderPages.Value;
286 24c5e56c taeseongkim
287 ca49aa5b taeseongkim
                    if (!IsExportOrPrint)
288
                    {
289 77cdac33 taeseongkim
                        await PageChangeAsync(Common.ViewerDataModel.Instance.NewCancellationToken(), this.CurrentPageNo, false);
290 ca49aa5b taeseongkim
                    }
291 90865212 taeseongkim
                    //await App.PageStorage.GetPageUriAsync(this.CurrentPageNo);
292 53deabaf taeseongkim
                
293 481c3fb9 djkim
                    if (_ButtonName == "Current")
294 114015fd ljiyeon
                    {
295
                        stPageNo.Value = CurrentPageNo;
296
                        edPageNo.Value = CurrentPageNo;
297 481c3fb9 djkim
                    }
298 114015fd ljiyeon
299 787a4489 KangIngu
                    //CheckCommentPages();
300
                }
301
            }
302
        }
303
304 86bff326 taeseongkim
        private async void sliderPages_DragCompleted(object sender, RadDragCompletedEventArgs e)
305 787a4489 KangIngu
        {
306
            _IsDagSlider = false;
307
            this.CurrentPageNo = (int)this.sliderPages.Value;
308
            if (_initializeComponentFinished)
309
            {
310 90865212 taeseongkim
               
311 787a4489 KangIngu
            }
312
        }
313
314
        private void sliderPages_DragStarted(object sender, RadDragStartedEventArgs e)
315
        {
316
            _IsDagSlider = true;
317
        }
318
319
        private void chkOnlyMarkup_Checked(object sender, RoutedEventArgs e)
320
        {
321
            if (_initializeComponentFinished)      //GridRangePages
322
            {
323
                CheckCommentPages();
324
            }
325
        }
326 114015fd ljiyeon
        string _ButtonName;
327 787a4489 KangIngu
        private void PageSelect_Checked(object sender, RoutedEventArgs e)
328
        {
329
            if (!_initializeComponentFinished) return;
330
331 114015fd ljiyeon
            _ButtonName = (sender as RadRadioButton).Tag.ToString();
332 787a4489 KangIngu
            CommentPageList.ItemsSource = null;
333
334
            switch (_ButtonName)
335
            {
336
                case "Current":
337
                    stPageNo.Value = CurrentPageNo;
338
                    edPageNo.Value = CurrentPageNo;
339
340
                    GridCurrentPage.Visibility = Visibility.Visible;
341
                    GridDefinePages.Visibility = Visibility.Collapsed;
342
                    GridRangePages.Visibility = Visibility.Collapsed;
343
                    GridAllPages.Visibility = Visibility.Collapsed;
344
345
                    break;
346
                case "RangePrint":
347
                    GridCurrentPage.Visibility = Visibility.Collapsed;
348
                    GridDefinePages.Visibility = Visibility.Visible;
349
                    GridRangePages.Visibility = Visibility.Collapsed;
350
                    GridAllPages.Visibility = Visibility.Collapsed;
351
                    break;
352
                case "UserDefined":
353
                    GridCurrentPage.Visibility = Visibility.Collapsed;
354
                    GridDefinePages.Visibility = Visibility.Collapsed;
355
                    GridRangePages.Visibility = Visibility.Visible;
356
                    GridAllPages.Visibility = Visibility.Collapsed;
357
                    break;
358
                case "AllPrint":
359
                    stPageNo.Value = 1;
360
                    edPageNo.Value = Convert.ToDouble(_DocInfo.PAGE_COUNT);
361
                    GridCurrentPage.Visibility = Visibility.Collapsed;
362
                    GridDefinePages.Visibility = Visibility.Collapsed;
363
                    GridRangePages.Visibility = Visibility.Collapsed;
364
                    GridAllPages.Visibility = Visibility.Visible;
365
                    break;
366
            }
367
368
            CheckCommentPages();
369
        }
370
371
        private void PageNo_ValueChanged(object sender, RadRangeBaseValueChangedEventArgs e)
372
        {
373
            if (_initializeComponentFinished)      //GridRangePages
374
            {
375
                if (stPageNo.Value > edPageNo.Value)
376
                {
377
                    if (sender.Equals(stPageNo))
378
                        edPageNo.Value = stPageNo.Value;
379
                    else
380
                        stPageNo.Value = edPageNo.Value;
381
                }
382
383
                //뷰어잠시제외(강인구)
384
                //this.LayoutRoot.Dispatcher.BeginInvoke(delegate()
385
                //{
386
                CheckCommentPages();
387
                //});
388
            }
389
        }
390
391
        private void txtDefinedPages_TextChanged(object sender, TextChangedEventArgs e)
392
        {
393
            CheckCommentPages();
394
        }
395
396
        private void btnClearDefinedPages_Click(object sender, RoutedEventArgs e)
397
        {
398
            txtDefinedPages.Text = "";
399
        }
400
401
        private void CommentPageList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
402
        {
403
            if (_initializeComponentFinished)
404
                if (CommentPageList.SelectedItem != null)
405
                    this.CurrentPageNo = (CommentPageList.SelectedItem as MarkupPageItem).PageNumber;
406
        }
407
408 86bff326 taeseongkim
        private async void gridViewMarkup_SelectionChanged(object sender, SelectionChangeEventArgs e)
409 787a4489 KangIngu
        {
410
            if (_initializeComponentFinished)
411
            {
412 77cdac33 taeseongkim
                await PageChangeAsync(Common.ViewerDataModel.Instance.NewCancellationToken(),this.CurrentPageNo, false);
413 787a4489 KangIngu
            }
414
        }
415
416
        #endregion
417
418 77cdac33 taeseongkim
        public async System.Threading.Tasks.Task<bool> PageChangeAsync(System.Threading.CancellationToken cts, int PageNo,bool saveFile, bool Flag = false)
419 787a4489 KangIngu
        {
420 53deabaf taeseongkim
            System.Diagnostics.Debug.WriteLine("PageChangeAsync " + PageNo);
421 86bff326 taeseongkim
            bool result = false;
422
            //Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
423
            //{
424
            try
425 787a4489 KangIngu
            {
426 86bff326 taeseongkim
                Load load = new Load();
427
428
                _definePages.Back_Image = new ImageBrush();
429
                //var defaultBitmapImage = new BitmapImage(new Uri(_DefaultTileUri + PageNo + ".png"));
430
431
                var pages = _definePages.DocumentInfo.DOCPAGE.Where(x => x.PAGE_NUMBER == PageNo);
432
433
                if(pages.Count() > 0)
434 481c3fb9 djkim
                {
435 86bff326 taeseongkim
                    var currentPage = pages.First();
436 787a4489 KangIngu
437 86bff326 taeseongkim
                    //var buffer = await new WebClient().DownloadDataTaskAsync(_DefaultTileUri + PageNo + ".png");
438
439
                    //var bitmap = new BitmapImage();
440
                    //using (var stream = new MemoryStream(buffer))
441
                    //{
442 90865212 taeseongkim
                    //bitmap.BeginInit();
443
                    //bitmap.CacheOption = BitmapCacheOption.OnLoad;
444
                    //bitmap.StreamSource = stream;
445
                    //bitmap.EndInit();
446
447
                        
448 43a743e4 taeseongkim
                    var uri = await App.PageStorage.GetPageUriAsync(cts, currentPage.PAGE_NUMBER);
449 90865212 taeseongkim
                    //Image bitmap = new Image();
450
                    //bitmap.Stretch = Stretch.Fill;
451
                    //bitmap.Source = bitmapframe;
452
                    //bitmap.Width = Convert.ToDouble(currentPage.PAGE_WIDTH);
453
                    //bitmap.Height = Convert.ToDouble(currentPage.PAGE_HEIGHT);
454 89971d17 taeseongkim
455
                    if (uri.Scheme == Uri.UriSchemeFile)
456
                    {
457
                        var cloneUri = new Uri(System.IO.Path.Combine(System.IO.Path.GetTempPath(),System.IO.Path.GetRandomFileName()),UriKind.Absolute);
458
                        File.Copy(uri.LocalPath, cloneUri.LocalPath);
459
460
                        uri = cloneUri;
461
                    }
462
463 00143658 taeseongkim
                    printCanvas.Children.Clear();
464
                    printCanvas.Width = Convert.ToDouble(currentPage.PAGE_WIDTH);
465
                    printCanvas.Height = Convert.ToDouble(currentPage.PAGE_HEIGHT);
466 89971d17 taeseongkim
                 
467
                    printCanvas.Background = new ImageBrush{ ImageSource = new BitmapImage(uri)};
468 90865212 taeseongkim
469 00143658 taeseongkim
                    //printCanvas.RenderTransformOrigin = new Point(0.5, 0.5);
470
                    //printCanvas.RenderTransform = new RotateTransform(currentPage.PAGE_ANGLE);
471
                    //ImageBrush background = new ImageBrush(bitmap);
472
                    //printCanvas.Background = background;
473
                    //printCanvas.Background = new SolidColorBrush(Colors.Transparent);
474 787a4489 KangIngu
475 481c3fb9 djkim
476 00143658 taeseongkim
                    foreach (var info in gridViewMarkup.SelectedItems.Cast<IKCOM.MarkupInfoItem>())
477
                    {
478
                        load.User_Id = info.UserID;
479
                        load.document_id = _DocInfo.DOCUMENT_ID;
480
                        load.Page_No = PageNo;
481
                        load.DisplayColor = info.DisplayColor;
482
                        load.Markupinfoid = info.MarkupInfoID;
483 4f017ed3 taeseongkim
                        var IsLoad = await load.Markup_LoadAsync(printCanvas, 0);
484 00143658 taeseongkim
                    }
485 90865212 taeseongkim
                    //await Dispatcher.InvokeAsync(() => {
486
                    //    printCanvas.UpdateLayout();
487
                    //});
488 481c3fb9 djkim
489 d04e8ee9 taeseongkim
                    if (Flag)
490 481c3fb9 djkim
                        {
491 d04e8ee9 taeseongkim
                        //MemoryStream ms = new MemoryStream();
492
                        //BmpBitmapEncoder bbe = new BmpBitmapEncoder();
493
                        //bbe.Frames.Add(BitmapFrame.Create(new Uri(_DefaultTileUri + PageNo + ".png")));
494
                        //bbe.Save(ms);
495
496
                        //System.Drawing.Image.FromFile()
497
                        //Printimg = System.Drawing.Image.FromStream(stream);
498 53deabaf taeseongkim
                        //await System.Threading.Tasks.Task.Run(() =>
499
                        //{
500
                           await Dispatcher.InvokeAsync(() =>
501 d04e8ee9 taeseongkim
                            {
502
                                Export export = new Export();
503 77cdac33 taeseongkim
504
                                if (saveFile)
505
                                {
506
                                    var filepath = Path.Combine(TempImageDir, $"{PrintimgPath_List.Count() + 1}.png");
507
508
                                    if (export.ExportingFile(PrintView, printCanvas.Width, printCanvas.Height, filepath))
509
                                    {
510
                                        PrintimgPath_List.Add(PrintimgPath_List.Count() + 1, filepath);
511
                                    }
512
                                    else
513
                                    {
514
                                        throw new Exception($"Export Image Save File Error :{filepath}");
515
                                    }
516
                                }
517
                                else
518
                                {
519
                                    p_img = export.Exporting(PrintView, printCanvas.Width, printCanvas.Height);
520
                                    Printimg_List.Add(Printimg_List.Count() + 1, p_img);
521
                                }
522
523 43a743e4 taeseongkim
                            },System.Windows.Threading.DispatcherPriority.SystemIdle);
524 53deabaf taeseongkim
                        //});
525 d04e8ee9 taeseongkim
                    }
526 481c3fb9 djkim
527 86bff326 taeseongkim
                        result = true;
528
                    //}
529
                } 
530
            }
531
            catch (Exception ex)
532
            {
533 664ea2e1 taeseongkim
                //Logger.sendResLog("PrintControl.PageChanged", ex.Message, 0);
534 86bff326 taeseongkim
            }
535
536
            return result;
537
            //}));
538 787a4489 KangIngu
        }
539 89971d17 taeseongkim
540
541 481c3fb9 djkim
        public static MemoryStream ByteBufferFromImage(BitmapImage imageSource)
542
        {
543
            var ms = new MemoryStream();
544
            var pngEncoder = new PngBitmapEncoder();
545
            pngEncoder.Frames.Add(BitmapFrame.Create(imageSource));
546
            pngEncoder.Save(ms);
547 787a4489 KangIngu
548 481c3fb9 djkim
            return ms;
549
        }
550 787a4489 KangIngu
        //Print 버튼 클릭 이벤트
551
        #region Method - 명령
552 86bff326 taeseongkim
        async void PrintMethod(object sender, RoutedEventArgs e)
553 787a4489 KangIngu
        {
554 481c3fb9 djkim
            try
555
            {
556 24c5e56c taeseongkim
                var token = Common.ViewerDataModel.Instance.NewCancellationToken();
557
558 481c3fb9 djkim
                if (this.printIndy.IsBusy == true)
559
                    return;
560 787a4489 KangIngu
561 00143658 taeseongkim
                (this.Parent as RadWindow).CanClose = false;
562
563 86bff326 taeseongkim
                await this.Dispatcher.InvokeAsync(() => { 
564 481c3fb9 djkim
                        this.printIndy.IsBusy = true;
565
                        printIndy.BusyContent = "Printing. . .";
566
                });
567 787a4489 KangIngu
568 481c3fb9 djkim
569
                _lstPrintPageNo = PrintPageCreate();
570
571
                if (_lstPrintPageNo.Count == 0)
572 787a4489 KangIngu
                {
573 481c3fb9 djkim
                    RadWindow.Alert(new DialogParameters
574
                    {
575
                        Owner = Application.Current.MainWindow,
576
                        Theme = new VisualStudio2013Theme(),
577 eeb0a39c taeseongkim
                        Header = "Info",
578
                        Content = "Not specified the page."
579 481c3fb9 djkim
                    });
580 9d5b4bc2 taeseongkim
                    sliderPages.Value = 1;
581 481c3fb9 djkim
                    this.printIndy.IsBusy = false;
582
                    return;
583
                }
584 86bff326 taeseongkim
585 481c3fb9 djkim
                int cnt = 0;
586 787a4489 KangIngu
587 ca49aa5b taeseongkim
                IsExportOrPrint = true;
588
589 481c3fb9 djkim
                Printimg_List = new Dictionary<int, System.Drawing.Image>();
590 77cdac33 taeseongkim
                PrintimgPath_List = new Dictionary<int, string>();
591
                TempImageDir = Path.Combine(Path.GetTempPath(), "Markus", Common.Commons.shortGuid());
592
593
                if (Directory.Exists(TempImageDir))
594
                {
595
                    Directory.Delete(TempImageDir, true);
596
                }
597
598
                Directory.CreateDirectory(TempImageDir);
599 787a4489 KangIngu
600 481c3fb9 djkim
                foreach (int PageNo in _lstPrintPageNo)
601
                {
602
                    cnt++;
603
                    sliderPages.Value = PageNo;
604 77cdac33 taeseongkim
                    await PageChangeAsync(token,PageNo, true, true);
605 481c3fb9 djkim
                    //PageChanged(cnt, true);
606
                }
607
                PrintName = cbPrint.SelectedItem.ToString();
608 787a4489 KangIngu
609 86bff326 taeseongkim
                await this.Dispatcher.InvokeAsync(() => {
610 481c3fb9 djkim
                    if (cnt == _lstPrintPageNo.Count)
611
                    {
612
                        Printing();
613
                    }
614 787a4489 KangIngu
                });
615 ca49aa5b taeseongkim
616
                IsExportOrPrint = false;
617 787a4489 KangIngu
            }
618 481c3fb9 djkim
            catch (Exception ex)
619 787a4489 KangIngu
            {
620 664ea2e1 taeseongkim
                //Logger.sendResLog("PrintMethod", ex.Message, 0);
621 787a4489 KangIngu
            }
622 00143658 taeseongkim
            finally
623
            {
624
                (this.Parent as RadWindow).CanClose = true;
625
            }
626 787a4489 KangIngu
627
        }
628
629
        //Export 버튼 클릭 이벤트
630 86bff326 taeseongkim
        async void ExportMethod(object sender, RoutedEventArgs e)
631 787a4489 KangIngu
        {
632 481c3fb9 djkim
            try
633
            {
634
                if (this.printIndy.IsBusy == true)
635
                    return;
636 787a4489 KangIngu
637 24c5e56c taeseongkim
                var token = Common.ViewerDataModel.Instance.NewCancellationToken();
638
639 00143658 taeseongkim
                (this.Parent as RadWindow).CanClose = false;
640
641 481c3fb9 djkim
                this.printIndy.IsBusy = true;
642
                printIndy.BusyContent = "Exporting. . .";
643 787a4489 KangIngu
644 481c3fb9 djkim
                Export export = new Export();
645
                _lstPrintPageNo = PrintPageCreate();
646 787a4489 KangIngu
647 481c3fb9 djkim
                if (_lstPrintPageNo.Count == 0)
648 787a4489 KangIngu
                {
649 9d5b4bc2 taeseongkim
                    sliderPages.Value = 1;
650 481c3fb9 djkim
                    this.printIndy.IsBusy = false;
651
                    RadWindow.Alert(new DialogParameters
652 787a4489 KangIngu
                    {
653 481c3fb9 djkim
                        Owner = Application.Current.MainWindow,
654
                        Theme = new VisualStudio2013Theme(),
655 eeb0a39c taeseongkim
                        Header = "Info",
656
                        Content = "Not specified the page.",
657 481c3fb9 djkim
                    });
658
                    return;
659
                }
660 787a4489 KangIngu
661 481c3fb9 djkim
                //FileDialogFilter filterImage = new FileDialogFilter("Image Files", "*.jpg", "*.png");
662 787a4489 KangIngu
663
                switch (cbPrint.SelectedIndex)
664
                {
665
                    case (0):
666
                        {
667 481c3fb9 djkim
                            SaveDig.Filter = "PDF file format|*.pdf";
668
                            break;
669
                        }
670
                    case (1):
671
                        {
672 77cdac33 taeseongkim
                            SaveDig.Filter = "Image Files (*.jpg, *.Jpeg)|*.jpg;*.Jpeg";
673 481c3fb9 djkim
                            break;
674
                        }
675
                }
676
677
                SaveDig.Title = "Save an PDF File";
678
                if(SaveDig.ShowDialog().Value)
679
                {
680 ca49aa5b taeseongkim
                    IsExportOrPrint = true;
681 77cdac33 taeseongkim
682
                    PrintimgPath_List = new Dictionary<int, string>();
683
                    TempImageDir = Path.Combine(Path.GetTempPath(), "Markus", Common.Commons.shortGuid());
684
685
                    if (Directory.Exists(TempImageDir))
686
                    {
687
                        Directory.Delete(TempImageDir, true);
688
                    }
689
690
                    Directory.CreateDirectory(TempImageDir);
691 481c3fb9 djkim
692
                    foreach (int PageNo in _lstPrintPageNo)
693
                    {
694 d04e8ee9 taeseongkim
                        await Dispatcher.InvokeAsync(() =>
695
                        {
696
                            sliderPages.Value = PageNo;
697
                        });
698 53deabaf taeseongkim
699 77cdac33 taeseongkim
                        await PageChangeAsync(token, PageNo, true, true);
700 43a743e4 taeseongkim
701 53deabaf taeseongkim
                        System.Diagnostics.Debug.WriteLine("Export Page : " + PageNo);
702 481c3fb9 djkim
                    }
703
704
                    //using (FileStream fs = new FileStream(@"D:\Temp\Text.pdf", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
705
706
                    if (SaveDig.FileName != "")
707
                    {
708
                        switch (cbPrint.SelectedIndex)
709
                        {
710
                            case (0):
711 787a4489 KangIngu
                                {
712 77cdac33 taeseongkim
                                    using (FileStream fs = new FileStream(SaveDig.FileName, FileMode.Create))
713 787a4489 KangIngu
                                    {
714 481c3fb9 djkim
                                        using (Document doc = new Document())
715 787a4489 KangIngu
                                        {
716 77cdac33 taeseongkim
                                            PdfWriter writer = PdfWriter.GetInstance(doc, fs);
717 481c3fb9 djkim
718 77cdac33 taeseongkim
                                            doc.Open();
719 481c3fb9 djkim
720 77cdac33 taeseongkim
                                            float height = doc.PageSize.Height;
721
                                            float width = doc.PageSize.Width;
722 481c3fb9 djkim
723 77cdac33 taeseongkim
                                            foreach (var item in PrintimgPath_List)
724
                                            {
725
                                                System.Drawing.Image image = System.Drawing.Image.FromFile(item.Value);
726
                                                Export_img = iTextSharp.text.Image.GetInstance(image,System.Drawing.Imaging.ImageFormat.Jpeg);
727 481c3fb9 djkim
728 77cdac33 taeseongkim
                                                if (Export_img.Width > Export_img.Height)
729
                                                {
730
                                                    doc.SetPageSize(new Rectangle(height, width));
731
                                                    Export_img.ScaleToFit(height, width);
732
                                                }
733
                                                else
734
                                                {
735
                                                    doc.SetPageSize(new Rectangle(width, height));
736
                                                    Export_img.ScaleToFit(width, height);
737
                                                }
738 00143658 taeseongkim
739 77cdac33 taeseongkim
                                                Export_img.SetAbsolutePosition(0, 0);
740 00143658 taeseongkim
741 77cdac33 taeseongkim
                                                doc.NewPage();
742
                                                doc.Add(Export_img);
743 481c3fb9 djkim
                                            }
744 787a4489 KangIngu
                                        }
745
                                    }
746 481c3fb9 djkim
                                    break;
747 787a4489 KangIngu
                                }
748 481c3fb9 djkim
                            case (1):
749 787a4489 KangIngu
                                {
750 77cdac33 taeseongkim
                                    foreach (var item in PrintimgPath_List)
751 481c3fb9 djkim
                                    {
752 77cdac33 taeseongkim
                                        File.Copy(item.Value, SaveDig.FileName.Replace(".jpg", "_" + item.Key.ToString()) + ".jpg");
753 481c3fb9 djkim
                                    }
754
                                    break;
755 787a4489 KangIngu
                                }
756
                        }
757 481c3fb9 djkim
                    }
758
                    else
759
                    {
760 9d5b4bc2 taeseongkim
                        sliderPages.Value = 1;
761 481c3fb9 djkim
                        this.printIndy.IsBusy = false;
762
                        return;
763
                    }
764 86bff326 taeseongkim
765
                    await this.Dispatcher.InvokeAsync(() =>
766 481c3fb9 djkim
                    {
767 9d5b4bc2 taeseongkim
                        sliderPages.Value = 1;
768 481c3fb9 djkim
                        printIndy.IsBusy = false;
769 86bff326 taeseongkim
                    });
770
771 77cdac33 taeseongkim
                    await PageChangeAsync(token,_StartPageNo, false);
772 787a4489 KangIngu
773 ca49aa5b taeseongkim
                    IsExportOrPrint = false;
774
775 481c3fb9 djkim
                    (Application.Current.MainWindow as MainWindow).dzMainMenu.DialogMessage_Alert("Success", "Alert");
776
                }else
777
                {
778
                    Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
779
                    {
780 9d5b4bc2 taeseongkim
                        sliderPages.Value = 1;
781 481c3fb9 djkim
                        printIndy.IsBusy = false;
782
                    }));
783 787a4489 KangIngu
784 ca49aa5b taeseongkim
                    //(Application.Current.MainWindow as MainWindow).dzMainMenu.DialogMessage_Alert("Cancel", "Alert");
785 481c3fb9 djkim
                }                
786
            }
787
            catch (Exception ex)
788 787a4489 KangIngu
            {
789 9d5b4bc2 taeseongkim
                sliderPages.Value = 1;
790 86bff326 taeseongkim
                printIndy.IsBusy = false;
791
792 664ea2e1 taeseongkim
                //Logger.sendResLog("Export Method", ex.Message, 0);
793 481c3fb9 djkim
                //(Application.Current.MainWindow as MainWindow).dzMainMenu.DialogMessage_Alert("Alert", "관리자 확인이 필요합니다. 다시 시도해 주세요.\n"+ex.Message);
794
            }
795 00143658 taeseongkim
            finally
796
            {
797 77cdac33 taeseongkim
                if(Directory.Exists(TempImageDir))
798
                    Directory.Delete(TempImageDir, true);
799 00143658 taeseongkim
800 77cdac33 taeseongkim
                GC.Collect();
801
                GC.WaitForPendingFinalizers();
802
                GC.Collect();
803 787a4489 KangIngu
804 77cdac33 taeseongkim
                (this.Parent as RadWindow).CanClose = true;
805
            }
806 787a4489 KangIngu
        }
807 77cdac33 taeseongkim
808 787a4489 KangIngu
        #endregion
809
810
        #region Method - 처리
811
        void CheckCommentPages()
812
        {
813
            List<MarkupPageItem> _pages = new List<MarkupPageItem>();
814
            DoubleCollection _Ticks = new DoubleCollection();
815
816
817
            if (GridCurrentPage.Visibility == System.Windows.Visibility.Visible)
818
            {
819
820
                _pages = (from commPage in this._PageMarkupList
821
                          where commPage.PageNumber == CurrentPageNo
822
                          orderby commPage.PageNumber
823
                          select commPage).ToList();
824
            }
825
            else if (GridRangePages.Visibility == System.Windows.Visibility.Visible)
826
            {
827
                stPageNo.Value = stPageNo.Value == null ? 1 : stPageNo.Value;
828
                edPageNo.Value = edPageNo.Value == null ? 1 : edPageNo.Value;
829
830
                int _stPage = (int)stPageNo.Value;
831
                int _edPage = (int)edPageNo.Value;
832
833
                _pages = (from commPage in this._PageMarkupList
834
                          where commPage.PageNumber >= _stPage && commPage.PageNumber <= _edPage
835
                          orderby commPage.PageNumber
836
                          select commPage).ToList();
837
            }
838
            else if (GridDefinePages.Visibility == System.Windows.Visibility.Visible)
839
            {
840
                stPageNo.Value = stPageNo.Value == null ? 1 : stPageNo.Value;
841
                edPageNo.Value = edPageNo.Value == null ? 1 : edPageNo.Value;
842
843
                int _stPage = (int)stPageNo.Value;
844
                int _edPage = (int)edPageNo.Value;
845
846
                //Using DeepView.Common.RangeParser
847
                var lst = RangeParser.Parse(txtDefinedPages.Text, _stPage, _definePages.PagesCount);
848
                _pages = (from commPage in this._PageMarkupList
849
                          from compareData in lst
850
                          where commPage.PageNumber == compareData
851
                          orderby commPage.PageNumber
852
                          select commPage).ToList();
853
            }
854
            else
855
            {
856
                _pages = (from commPage in this._PageMarkupList
857
                          orderby commPage.PageNumber
858
                          select commPage).ToList();
859
            }
860
            CommentPageList.ItemsSource = _pages.ToList();
861
        }
862
863
        void SetLoadMakupList(int PageNo)
864
        {
865
            _LoadMakupList.Clear();
866
            var _markupList = _PageMarkupList.Where(page => page.PageNumber == PageNo);
867
868
            if (_markupList.Count() > 0)
869
                _LoadMakupList = _markupList.First().DisplayColorItems.ToList();
870
        }
871
872
        #region 프린트 리스트 가져오기
873
        public void GetPrint(bool flag)
874
        {
875
            if (flag)
876
            {
877
                foreach (string printer in PrinterSettings.InstalledPrinters)
878
                {
879
                    this.cbPrint.Items.Add(printer);
880
                }
881
                printDocument = new PrintDocument();
882
                this.cbPrint.SelectedItem = printDocument.PrinterSettings.PrinterName;
883
            }
884
            else
885
            {
886
                this.cbPrint.Items.Add("PDF");
887
                this.cbPrint.Items.Add("IMAGE");
888
889
                this.cbPrint.SelectedItem = "PDF";
890
            }
891
        }
892
        #endregion
893
894
        #region 프린트 실행
895 86bff326 taeseongkim
        public async void Printing()
896 787a4489 KangIngu
        {
897 24c5e56c taeseongkim
            var token = Common.ViewerDataModel.Instance.NewCancellationToken();
898
899 77cdac33 taeseongkim
            await PageChangeAsync(token,1, false);
900 86bff326 taeseongkim
901 787a4489 KangIngu
            if (PrinterSettings.InstalledPrinters != null && PrinterSettings.InstalledPrinters.Count > 0)
902
            {
903 481c3fb9 djkim
                printDocument = new PrintDocument();
904 408e4218 ljiyeon
                printDocument.OriginAtMargins = true;
905 787a4489 KangIngu
                printDocument.BeginPrint += new System.Drawing.Printing.PrintEventHandler(printDocument_BeginPrint);
906
                printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument_PrintPage);
907
                printDocument.EndPrint += new System.Drawing.Printing.PrintEventHandler(printDocument_EndPrint);
908
            }
909
            else
910
                printDocument = null;
911
912
            #region 인쇄 미리보기 테스트
913
            using (System.Windows.Forms.PrintPreviewDialog dlg = new System.Windows.Forms.PrintPreviewDialog())
914
            {
915
                printDocument.PrinterSettings.MinimumPage = 1;
916
                printDocument.PrinterSettings.MaximumPage = _lstPrintPageNo.Count();
917
                printDocument.PrinterSettings.FromPage = 1;
918 408e4218 ljiyeon
                printDocument.PrinterSettings.ToPage = _lstPrintPageNo.Count();
919 787a4489 KangIngu
                printDocument.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
920 481c3fb9 djkim
                printDocument.PrinterSettings.PrinterName = PrintName;
921
                dlg.Document = printDocument;
922 787a4489 KangIngu
                dlg.WindowState = System.Windows.Forms.FormWindowState.Maximized;
923
                dlg.ShowDialog();
924
            }
925
            #endregion
926
927
            #region 인쇄
928
            //printDocument.PrinterSettings.MinimumPage = 1;
929
            //printDocument.PrinterSettings.MaximumPage = _lstPrintPageNo.Count();
930
            //printDocument.PrinterSettings.FromPage = 1;
931
            //printDocument.PrinterSettings.ToPage = _lstPrintPageNo.Count();
932
933
            //printDocument.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
934
            //printDocument.PrinterSettings.PrinterName = PrintName;
935
936
            //printDocument.Print();
937
            #endregion
938
        }
939
940
        private void printDocument_BeginPrint(object sender, PrintEventArgs e)
941
        {
942
            //Printimg_List = new Dictionary<int, System.Drawing.Image>();
943
            // This demo only loads one page at a time, so no need to re-set the print page number
944 481c3fb9 djkim
            PrintDocument document = sender as PrintDocument;
945 787a4489 KangIngu
            currentPage = document.PrinterSettings.FromPage;
946
        }
947 481c3fb9 djkim
948 787a4489 KangIngu
        private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
949
        {
950
            //e.Graphics.DrawImage(Printimg_List.Where(info => info.Key == currentPage).FirstOrDefault().Value, 0, 0);
951
952
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
953
            {
954 77cdac33 taeseongkim
                //p_img = Printimg_List.Where(data => data.Key == currentPage).FirstOrDefault().Value;
955
                p_img = System.Drawing.Image.FromFile(PrintimgPath_List.Where(data => data.Key == currentPage).FirstOrDefault().Value);
956 2024d8d1 ljiyeon
957 481c3fb9 djkim
                if (p_img.Width > p_img.Height)
958 2024d8d1 ljiyeon
                {
959
                    p_img.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
960
                }
961 408e4218 ljiyeon
962
                //iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(p_img, System.Drawing.Imaging.ImageFormat.Jpeg);
963
                System.Drawing.Image pic = p_img;
964 787a4489 KangIngu
                System.Drawing.Rectangle m = e.MarginBounds;
965
966 408e4218 ljiyeon
                //if ((double)pic.Width / (double)pic.Height > (double)m.Width / (double)m.Height) // image is wider
967
                //{
968
                //    m.Height = 1135;//(int)((double)pic.Height / (double)pic.Width * (double)m.Width) - 16;
969
                //}
970
                //else
971
                //{
972
                //    m.Width = 793;//(int)((double)pic.Width / (double)pic.Height * (double)m.Height) - 16;
973
                //}
974
                m.Width = (int)e.PageSettings.PrintableArea.Width;
975
                m.Height = (int)e.PageSettings.PrintableArea.Height;
976
                m.X = (int)e.PageSettings.HardMarginX;
977
                m.Y = (int)e.PageSettings.HardMarginY;
978
979
                e.Graphics.DrawImage(pic, m);
980 787a4489 KangIngu
                //e.Graphics.DrawImage(p_img, e.MarginBounds);
981
                //e.Graphics.DrawImage(p_img, 0, 0);
982
            }));
983 481c3fb9 djkim
984 787a4489 KangIngu
            //다음 페이지
985
            currentPage++;
986 481c3fb9 djkim
987 787a4489 KangIngu
            ////인쇄를 계속 할지 말지 확인
988
            if (currentPage <= printDocument.PrinterSettings.ToPage)
989
                e.HasMorePages = true;
990
            else
991
                e.HasMorePages = false;
992
993
        }
994
995
        private void printDocument_EndPrint(object sender, PrintEventArgs e)
996
        {
997
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
998
            {
999 77cdac33 taeseongkim
                if (Directory.Exists(TempImageDir))
1000
                    Directory.Delete(TempImageDir, true);
1001
1002 9d5b4bc2 taeseongkim
                sliderPages.Value = 1;
1003 787a4489 KangIngu
                printIndy.IsBusy = false;
1004 77cdac33 taeseongkim
1005 787a4489 KangIngu
            }));
1006
        }
1007
        #endregion
1008
1009
        #region Selected Pages Check
1010
        List<int> PrintPageCreate()
1011
        {
1012
            List<int> Result = new List<int>();
1013
1014
            if (GridCurrentPage.Visibility == System.Windows.Visibility.Visible || currentPagePrint)
1015
            {
1016
                Result.Add(CurrentPageNo);
1017
            }
1018
            else if (GridRangePages.Visibility == System.Windows.Visibility.Visible)
1019
            {
1020
                for (int i = Convert.ToInt32(stPageNo.Value); i <= Convert.ToInt32(edPageNo.Value); i++)
1021
                {
1022
                    Result.Add(i);
1023
                }
1024
            }
1025
            else if (GridDefinePages.Visibility == System.Windows.Visibility.Visible)
1026
            {
1027
                int _stPage = (int)stPageNo.Value;
1028
                int _edPage = (int)edPageNo.Value;
1029
1030
                var lst = RangeParser.Parse(txtDefinedPages.Text, _stPage, _definePages.PagesCount);
1031
                Result.AddRange(lst);
1032
            }
1033
            else
1034
            {
1035
                for (int i = 1; i <= _definePages.PagesCount; i++)
1036
                {
1037
                    Result.Add(i);
1038
                }
1039
            }
1040
            if (currentPagePrint)
1041
            {
1042
                return Result;
1043
            }
1044
1045
            if (chkOnlyMarkup.IsChecked.Value)
1046
            {
1047
                var _result = from result in Result
1048
                              where _PageMarkupList.Where(page => page.PageNumber == result).Count() > 0
1049
                              select result;
1050
                Result = _result.ToList();
1051
            }
1052
            return Result;
1053
        }
1054
        #endregion
1055
1056
        #endregion
1057
    }
1058
}
클립보드 이미지 추가 (최대 크기: 500 MB)