프로젝트

일반

사용자정보

개정판 481c3fb9

ID481c3fb94414628bac889042d9fe81e8b6cf91c3
상위 99e79f6c
하위 136851cb

김동진이(가) 약 6년 전에 추가함

issue #000 pdf export black image modify

차이점 보기:

KCOM/Controls/PrintControl.xaml.cs
21 21
using IKCOM;
22 22
using iTextSharp.text.pdf;
23 23
using Microsoft.Office.Core;
24
using System.Net;
24 25

  
25 26
namespace KCOM.Control
26 27
{
......
130 131
        System.Drawing.Image Printimg; //프린트 이미지
131 132
        System.Drawing.Image p_img; //프린트 이미지
132 133
        iTextSharp.text.Image Export_img;
133
        Dictionary<int,System.Drawing.Image> Printimg_List; //프린트 이미지
134
        Dictionary<int, System.Drawing.Image> Printimg_List; //프린트 이미지
134 135

  
135 136
        //RasterCodecs codecs;
136 137
        int currentPage;
......
169 170
        #endregion
170 171

  
171 172
        #region 생성자
172
        
173

  
173 174
        public PrintControl(string TileSourceUri, string ProjectNo, DOCINFO DocInfo, List<IKCOM.MarkupInfoItem> markupInfo, int CurrentPageNo, string slip, string title, string fileurl, bool isPrint)
174 175
        {
175 176
            InitializeComponent();
176
            
177

  
177 178
            this.IsEnabled = true;
178 179

  
179 180
            //Layer Control을 통째로 가져와야 하는건가
......
278 279
                {
279 280
                    this.CurrentPageNo = (int)this.sliderPages.Value;
280 281
                    PageChanged(this.CurrentPageNo);
281
                    
282
                    if(_ButtonName == "Current")
282

  
283
                    if (_ButtonName == "Current")
283 284
                    {
284 285
                        stPageNo.Value = CurrentPageNo;
285 286
                        edPageNo.Value = CurrentPageNo;
286
                    }                    
287
                    }
287 288

  
288 289
                    //CheckCommentPages();
289 290
                }
......
408 409
        {
409 410
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
410 411
            {
411
                Load load = new Load();
412
                
413
                _definePages.Back_Image = new ImageBrush();
414
                var defaultBitmapImage = new BitmapImage(new Uri(_DefaultTileUri + PageNo + ".png"));
412
                try
413
                {
414
                    Load load = new Load();
415 415

  
416
                printCanvas.Children.Clear();
416
                    _definePages.Back_Image = new ImageBrush();
417
                    //var defaultBitmapImage = new BitmapImage(new Uri(_DefaultTileUri + PageNo + ".png"));
418
                    var buffer = new WebClient().DownloadData(_DefaultTileUri + PageNo + ".png");
419
                    var bitmap = new BitmapImage();
420
                    using (var stream = new MemoryStream(buffer))
421
                    {
422
                        bitmap.BeginInit();
423
                        bitmap.CacheOption = BitmapCacheOption.OnLoad;
424
                        bitmap.StreamSource = stream;
425
                        bitmap.EndInit();
417 426

  
418
                ImageBrush background = new ImageBrush(defaultBitmapImage);
419
                printCanvas.Background = background;
427
                        printCanvas.Children.Clear();
420 428

  
421
                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(new Uri(_DefaultTileUri + PageNo + ".png"));
422
                printCanvas.Width = image.Width;
423
                printCanvas.Height = image.Height;
429
                        ImageBrush background = new ImageBrush(bitmap);
430
                        printCanvas.Background = background;
424 431

  
425
                foreach (var info in gridViewMarkup.SelectedItems.Cast<IKCOM.MarkupInfoItem>())
426
                {
427
                    load.User_Id = info.UserID;
428
                    load.document_id = _DocInfo.DOCUMENT_ID;
429
                    load.Page_No = PageNo;
430
                    load.DisplayColor = info.DisplayColor;
431
                    load.Markupinfoid = info.MarkupInfoID;
432
                    load.Markup_Load(printCanvas);
433
                }
432
                        iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(new Uri(_DefaultTileUri + PageNo + ".png"));
433
                        printCanvas.Width = image.Width;
434
                        printCanvas.Height = image.Height;
435

  
436
                        foreach (var info in gridViewMarkup.SelectedItems.Cast<IKCOM.MarkupInfoItem>())
437
                        {
438
                            load.User_Id = info.UserID;
439
                            load.document_id = _DocInfo.DOCUMENT_ID;
440
                            load.Page_No = PageNo;
441
                            load.DisplayColor = info.DisplayColor;
442
                            load.Markupinfoid = info.MarkupInfoID;
443
                            load.Markup_Load(printCanvas);
444
                        }
445

  
446
                        printCanvas.UpdateLayout();
447

  
448
                        if (Flag)
449
                        {
434 450

  
435
                printCanvas.UpdateLayout();
451
                            //MemoryStream ms = new MemoryStream();
452
                            //BmpBitmapEncoder bbe = new BmpBitmapEncoder();
453
                            //bbe.Frames.Add(BitmapFrame.Create(new Uri(_DefaultTileUri + PageNo + ".png")));
454
                            //bbe.Save(ms);
436 455

  
437
                if(Flag)
456
                            //System.Drawing.Image.FromFile()
457
                            Printimg = System.Drawing.Image.FromStream(stream);
458
                            Export export = new Export();
459
                            p_img = export.Exporting(printCanvas, Printimg);
460
                            Printimg_List.Add(Printimg_List.Count() + 1, p_img);
461
                        }
462
                    }
463

  
464
                }
465
                catch (Exception ex)
438 466
                {
439
                    MemoryStream ms = new MemoryStream();
440
                    BmpBitmapEncoder bbe = new BmpBitmapEncoder();
441
                    bbe.Frames.Add(BitmapFrame.Create(new Uri(_DefaultTileUri + PageNo + ".png")));
442
                    bbe.Save(ms);
443

  
444
                    Printimg = System.Drawing.Image.FromStream(ms);
445
                    Export export = new Export();
446
                    p_img = export.Exporting(printCanvas, Printimg);
447
                    Printimg_List.Add(Printimg_List.Count() + 1, p_img);
467
                    Logger.sendResLog("PrintControl.PageChanged", ex.Message, 0);
448 468
                }
449 469
            }));
450 470
        }
471
        public static MemoryStream ByteBufferFromImage(BitmapImage imageSource)
472
        {
473
            var ms = new MemoryStream();
474
            var pngEncoder = new PngBitmapEncoder();
475
            pngEncoder.Frames.Add(BitmapFrame.Create(imageSource));
476
            pngEncoder.Save(ms);
451 477

  
478
            return ms;
479
        }
452 480
        //Print 버튼 클릭 이벤트
453 481
        #region Method - 명령
454 482
        void PrintMethod(object sender, RoutedEventArgs e)
455 483
        {
484
            try
485
            {
486
                if (this.printIndy.IsBusy == true)
487
                    return;
456 488

  
457
            if (this.printIndy.IsBusy == true)
458
                return;
489
                System.Threading.Tasks.Task.Factory.StartNew(() =>
490
                {
491
                    Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
492
                    {
493
                        this.printIndy.IsBusy = true;
494
                        printIndy.BusyContent = "Printing. . .";
495
                    }));
496
                });
459 497

  
460
            System.Threading.Tasks.Task.Factory.StartNew(() =>
461
            {
462
                Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
498

  
499
                _lstPrintPageNo = PrintPageCreate();
500

  
501
                if (_lstPrintPageNo.Count == 0)
463 502
                {
464
                    this.printIndy.IsBusy = true;
465
                    printIndy.BusyContent = "Printing. . .";
466
                }));
467
            });
503
                    RadWindow.Alert(new DialogParameters
504
                    {
505
                        Owner = Application.Current.MainWindow,
506
                        Theme = new VisualStudio2013Theme(),
507
                        Header = "안내",
508
                        Content = "페이지가 지정되지 않았습니다."
509
                    });
510
                    this.printIndy.IsBusy = false;
511
                    return;
512
                }
513
                int cnt = 0;
514
                List<System.Threading.Tasks.Task> tasks = new List<System.Threading.Tasks.Task>();
468 515

  
516
                Printimg_List = new Dictionary<int, System.Drawing.Image>();
469 517

  
470
            _lstPrintPageNo = PrintPageCreate();
518
                foreach (int PageNo in _lstPrintPageNo)
519
                {
520
                    cnt++;
521
                    sliderPages.Value = PageNo;
522
                    PageChanged(PageNo, true);
523
                    //PageChanged(cnt, true);
524
                }
525
                PrintName = cbPrint.SelectedItem.ToString();
471 526

  
472
            if (_lstPrintPageNo.Count == 0)
473
            {
474
                RadWindow.Alert(new DialogParameters
527
                System.Threading.Tasks.Task.Factory.StartNew(() =>
475 528
                {
476
                    Owner = Application.Current.MainWindow,
477
                    Theme = new VisualStudio2013Theme(),
478
                    Header = "안내",
479
                    Content = "페이지가 지정되지 않았습니다."
529
                    if (cnt == _lstPrintPageNo.Count)
530
                    {
531
                        Printing();
532
                    }
480 533
                });
481
                this.printIndy.IsBusy = false;
482
                return;
483 534
            }
484
            int cnt = 0;
485
            List<System.Threading.Tasks.Task> tasks = new List<System.Threading.Tasks.Task>();
486

  
487
            Printimg_List = new Dictionary<int, System.Drawing.Image>();
488

  
489
            foreach (int PageNo in _lstPrintPageNo)
535
            catch (Exception ex)
490 536
            {
491
                cnt++;
492
                sliderPages.Value = PageNo;                
493
                PageChanged(PageNo, true);
494
                //PageChanged(cnt, true);
537
                Logger.sendResLog("PrintMethod", ex.Message, 0);
495 538
            }
496
            PrintName = cbPrint.SelectedItem.ToString();
497 539

  
498
            System.Threading.Tasks.Task.Factory.StartNew(() =>
499
            {
500
                if (cnt == _lstPrintPageNo.Count)
501
                {
502
                    Printing();
503
                }
504
            });
505 540
        }
506 541

  
507 542
        //Export 버튼 클릭 이벤트
508 543
        void ExportMethod(object sender, RoutedEventArgs e)
509 544
        {
510
            if (this.printIndy.IsBusy == true)
511
                return;
545
            try
546
            {
547
                if (this.printIndy.IsBusy == true)
548
                    return;
512 549

  
513
            this.printIndy.IsBusy = true;
514
            printIndy.BusyContent = "Exporting. . .";
550
                this.printIndy.IsBusy = true;
551
                printIndy.BusyContent = "Exporting. . .";
515 552

  
516
            Export export = new Export();
517
            _lstPrintPageNo = PrintPageCreate();
553
                Export export = new Export();
554
                _lstPrintPageNo = PrintPageCreate();
518 555

  
519
            if (_lstPrintPageNo.Count == 0)
520
            {
521
                this.printIndy.IsBusy = false;
522
                RadWindow.Alert(new DialogParameters
556
                if (_lstPrintPageNo.Count == 0)
523 557
                {
524
                    Owner = Application.Current.MainWindow,
525
                    Theme = new VisualStudio2013Theme(),
526
                    Header = "안내",
527
                    Content = "페이지가 지정되지 않았습니다.",
528
                });
529
                return;
530
            }
531

  
532
            //FileDialogFilter filterImage = new FileDialogFilter("Image Files", "*.jpg", "*.png");
533

  
534
            switch (cbPrint.SelectedIndex)
535
            {
536
                case (0):
558
                    this.printIndy.IsBusy = false;
559
                    RadWindow.Alert(new DialogParameters
537 560
                    {
538
                        SaveDig.Filter = "PDF file format|*.pdf";
539
                        break;
540
                    }
541
                case (1):
542
                    {
543
                        SaveDig.Filter = "Image Files (*.jpg, *.Jpeg)|*.jpg;*.Jpeg|Image Files (*.png)|*.png";
544
                        break;
545
                    }
546
            }
547

  
548
            SaveDig.Title = "Save an PDF File";
549
            SaveDig.ShowDialog();
550
            Printimg_List = new Dictionary<int, System.Drawing.Image>();
551

  
552
            foreach (int PageNo in _lstPrintPageNo)
553
            {
554
                sliderPages.Value = PageNo;
555
                PageChanged(PageNo, true);
556
            }
561
                        Owner = Application.Current.MainWindow,
562
                        Theme = new VisualStudio2013Theme(),
563
                        Header = "안내",
564
                        Content = "페이지가 지정되지 않았습니다.",
565
                    });
566
                    return;
567
                }
557 568

  
558
            //using (FileStream fs = new FileStream(@"D:\Temp\Text.pdf", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
569
                //FileDialogFilter filterImage = new FileDialogFilter("Image Files", "*.jpg", "*.png");
559 570

  
560
            if (SaveDig.FileName != "")
561
            {
562 571
                switch (cbPrint.SelectedIndex)
563 572
                {
564 573
                    case (0):
565 574
                        {
566
                            using (FileStream fs = new FileStream(SaveDig.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
567
                            {
568
                                using (Document doc = new Document())
575
                            SaveDig.Filter = "PDF file format|*.pdf";
576
                            break;
577
                        }
578
                    case (1):
579
                        {
580
                            SaveDig.Filter = "Image Files (*.jpg, *.Jpeg)|*.jpg;*.Jpeg|Image Files (*.png)|*.png";
581
                            break;
582
                        }
583
                }
584

  
585
                SaveDig.Title = "Save an PDF File";
586
                if(SaveDig.ShowDialog().Value)
587
                {
588
                    Printimg_List = new Dictionary<int, System.Drawing.Image>();
589

  
590
                    foreach (int PageNo in _lstPrintPageNo)
591
                    {
592
                        sliderPages.Value = PageNo;
593
                        PageChanged(PageNo, true);
594
                    }
595

  
596
                    //using (FileStream fs = new FileStream(@"D:\Temp\Text.pdf", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
597

  
598
                    if (SaveDig.FileName != "")
599
                    {
600
                        switch (cbPrint.SelectedIndex)
601
                        {
602
                            case (0):
569 603
                                {
570
                                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
604
                                    using (FileStream fs = new FileStream(SaveDig.FileName, FileMode.Create, FileAccess.Write, FileShare.None))
571 605
                                    {
572
                                        doc.Open();
573
                                        float height = doc.PageSize.Height;
574
                                        float width = doc.PageSize.Width;
575

  
576
                                        foreach (var item in Printimg_List)
606
                                        using (Document doc = new Document())
577 607
                                        {
578
                                            
579
                                             Export_img = iTextSharp.text.Image.GetInstance(item.Value, System.Drawing.Imaging.ImageFormat.Png);
580

  
581
                                            
582
                                            if (Export_img.Width > Export_img.Height)
583
                                            {
584
                                                doc.SetPageSize(new Rectangle(height, width));
585
                                                Export_img.ScaleToFit(height, width);
586
                                            }
587
                                            else
608
                                            using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
588 609
                                            {
589
                                                doc.SetPageSize(new Rectangle(width, height));
590
                                                Export_img.ScaleToFit(width, height);
591
                                            }
592
                                            
593
                                            Export_img.SetAbsolutePosition(0, 0);
610
                                                doc.Open();
611
                                                float height = doc.PageSize.Height;
612
                                                float width = doc.PageSize.Width;
613

  
614
                                                foreach (var item in Printimg_List)
615
                                                {
616

  
617
                                                    Export_img = iTextSharp.text.Image.GetInstance(item.Value, System.Drawing.Imaging.ImageFormat.Png);
594 618

  
595
                                            doc.NewPage();
596
                                            doc.Add(Export_img);
619

  
620
                                                    if (Export_img.Width > Export_img.Height)
621
                                                    {
622
                                                        doc.SetPageSize(new Rectangle(height, width));
623
                                                        Export_img.ScaleToFit(height, width);
624
                                                    }
625
                                                    else
626
                                                    {
627
                                                        doc.SetPageSize(new Rectangle(width, height));
628
                                                        Export_img.ScaleToFit(width, height);
629
                                                    }
630

  
631
                                                    Export_img.SetAbsolutePosition(0, 0);
632

  
633
                                                    doc.NewPage();
634
                                                    doc.Add(Export_img);
635
                                                }
636
                                                doc.Close();
637
                                            }
597 638
                                        }
598
                                        doc.Close();
599 639
                                    }
640
                                    break;
600 641
                                }
601
                            }
602
                            break;
603
                        }
604
                    case (1):
605
                        {
606
                            foreach (var item in Printimg_List)
607
                            {
608
                                switch(SaveDig.FilterIndex)
642
                            case (1):
609 643
                                {
610
                                    case (1):
611
                                        {
612
                                            //(item.Value as System.Drawing.Image).Save(SaveDig.FileName + item.Key, System.Drawing.Imaging.ImageFormat.Jpeg);
613
                                            (item.Value as System.Drawing.Image).Save(SaveDig.FileName.Replace(".jpg", "_" + item.Key.ToString()) + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
614
                                        }
615
                                        break;
616
                                    case (2):
644
                                    foreach (var item in Printimg_List)
645
                                    {
646
                                        switch (SaveDig.FilterIndex)
617 647
                                        {
618
                                            //(item.Value as System.Drawing.Image).Save(SaveDig.FileName, System.Drawing.Imaging.ImageFormat.Png);
619
                                            (item.Value as System.Drawing.Image).Save(SaveDig.FileName.Replace(".png", "_" + item.Key.ToString()) + ".png", System.Drawing.Imaging.ImageFormat.Png);
648
                                            case (1):
649
                                                {
650
                                                    //(item.Value as System.Drawing.Image).Save(SaveDig.FileName + item.Key, System.Drawing.Imaging.ImageFormat.Jpeg);
651
                                                    (item.Value as System.Drawing.Image).Save(SaveDig.FileName.Replace(".jpg", "_" + item.Key.ToString()) + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
652
                                                }
653
                                                break;
654
                                            case (2):
655
                                                {
656
                                                    //(item.Value as System.Drawing.Image).Save(SaveDig.FileName, System.Drawing.Imaging.ImageFormat.Png);
657
                                                    (item.Value as System.Drawing.Image).Save(SaveDig.FileName.Replace(".png", "_" + item.Key.ToString()) + ".png", System.Drawing.Imaging.ImageFormat.Png);
658
                                                }
659
                                                break;
620 660
                                        }
621
                                        break;
661
                                    }
662
                                    break;
622 663
                                }
623
                            }
624
                            break;
625 664
                        }
626
                }
627
            }
628
            else
629
            {
630
                this.printIndy.IsBusy = false;
631
                return;
632
            }
665
                    }
666
                    else
667
                    {
668
                        this.printIndy.IsBusy = false;
669
                        return;
670
                    }
671
                    Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
672
                    {
673
                        printIndy.IsBusy = false;
674
                    }));
675
                    PageChanged(_StartPageNo);
633 676

  
634
         
677
                    (Application.Current.MainWindow as MainWindow).dzMainMenu.DialogMessage_Alert("Success", "Alert");
678
                }else
679
                {
680
                    Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
681
                    {
682
                        printIndy.IsBusy = false;
683
                    }));
635 684

  
636
            //docWriter.EndDocument();
637
            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate
685
                    (Application.Current.MainWindow as MainWindow).dzMainMenu.DialogMessage_Alert("Cancel", "Alert");
686
                }                
687
            }
688
            catch (Exception ex)
638 689
            {
639
                printIndy.IsBusy = false;
640
            }));
641
            PageChanged(_StartPageNo);
690
                Logger.sendResLog("Export Method", ex.Message, 0);
691
                //(Application.Current.MainWindow as MainWindow).dzMainMenu.DialogMessage_Alert("Alert", "관리자 확인이 필요합니다. 다시 시도해 주세요.\n"+ex.Message);
692
            }
642 693

  
643
            (Application.Current.MainWindow as MainWindow).dzMainMenu.DialogMessage_Alert("Success", "Alert");
644 694
        }
645 695
        #endregion
646 696

  
......
734 784
            PageChanged(1);
735 785
            if (PrinterSettings.InstalledPrinters != null && PrinterSettings.InstalledPrinters.Count > 0)
736 786
            {
737
                printDocument = new PrintDocument();                
787
                printDocument = new PrintDocument();
738 788
                printDocument.OriginAtMargins = true;
739 789
                printDocument.BeginPrint += new System.Drawing.Printing.PrintEventHandler(printDocument_BeginPrint);
740 790
                printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument_PrintPage);
......
751 801
                printDocument.PrinterSettings.FromPage = 1;
752 802
                printDocument.PrinterSettings.ToPage = _lstPrintPageNo.Count();
753 803
                printDocument.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
754
                printDocument.PrinterSettings.PrinterName = PrintName;              
755
                dlg.Document = printDocument;                
804
                printDocument.PrinterSettings.PrinterName = PrintName;
805
                dlg.Document = printDocument;
756 806
                dlg.WindowState = System.Windows.Forms.FormWindowState.Maximized;
757 807
                dlg.ShowDialog();
758 808
            }
......
775 825
        {
776 826
            //Printimg_List = new Dictionary<int, System.Drawing.Image>();
777 827
            // This demo only loads one page at a time, so no need to re-set the print page number
778
            PrintDocument document = sender as PrintDocument;            
828
            PrintDocument document = sender as PrintDocument;
779 829
            currentPage = document.PrinterSettings.FromPage;
780 830
        }
781
                   
831

  
782 832
        private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
783 833
        {
784 834
            //e.Graphics.DrawImage(Printimg_List.Where(info => info.Key == currentPage).FirstOrDefault().Value, 0, 0);
......
787 837
            {
788 838
                p_img = Printimg_List.Where(data => data.Key == currentPage).FirstOrDefault().Value;
789 839

  
790
                if(p_img.Width > p_img.Height)
840
                if (p_img.Width > p_img.Height)
791 841
                {
792 842
                    p_img.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
793 843
                }
......
813 863
                //e.Graphics.DrawImage(p_img, e.MarginBounds);
814 864
                //e.Graphics.DrawImage(p_img, 0, 0);
815 865
            }));
816
            
866

  
817 867
            //다음 페이지
818 868
            currentPage++;
819
            
869

  
820 870
            ////인쇄를 계속 할지 말지 확인
821 871
            if (currentPage <= printDocument.PrinterSettings.ToPage)
822 872
                e.HasMorePages = true;

내보내기 Unified diff

클립보드 이미지 추가 (최대 크기: 500 MB)