songjun
2024-09-04 cc908053e0b5075fbfd27350b6da4b39bca9e550
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Reporting.WinForms;
using Microsoft.Reporting;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
 
namespace BatchService.Framework.Utility
{
    public class EMFPrinter : IDisposable
    {
        public enum PrintType
        {
            PDF,
            Image,
            Print,
        }
        private LocalReport _report = new LocalReport();
        private IList<Stream> _streams = new List<Stream>();
        private int _emf_Width = 25;
        private int _emf_Height = 24; 
        private int _emf_Top = 0;
        private int _emf_Left = 0;
        private int _emf_Bottom = 0;
        private int _emf_Right = 0;
 
        public EMFPrinter(string RdlcPath)
            : this(RdlcPath, 25, 24, 0, 0, 0, 0)
        {
 
        }
 
        public EMFPrinter(string RdlcPath, int EMFWidth, int EMFHeight,
            int EMFTop, int EMFLeft, int EMFBottom, int EMFRight)
        {
            _report.ReportPath = RdlcPath;
            _report.EnableExternalImages = true;
            _report.EnableHyperlinks = true;
            _emf_Width = EMFWidth;
            _emf_Height = EMFHeight;
            _emf_Top = EMFTop;
            _emf_Left = EMFLeft;
            _emf_Bottom = EMFBottom;
            _emf_Right = EMFRight;
        }
 
        public void AddParameter(string name, string value)
        {
            ReportParameter para = new ReportParameter(name);
            para.Values.Add(value);
            _report.SetParameters(para);
        }
 
        public void AddDataSource(string tableName, DataTable tableInfo)
        {
            ReportDataSource source = new ReportDataSource(tableName, tableInfo);
            _report.DataSources.Add(source);
        }
 
        private Stream CreateStream(string name,
      string fileNameExtension, Encoding encoding,
      string mimeType, bool willSeek)
        {
            Stream stream = new MemoryStream();
            _streams.Add(stream);
            return stream;
        }
 
        public byte[] Print(PrintType printType)
        {
            _report.Refresh(); 
            string deviceInfo = string.Format( 
              "<DeviceInfo>" + 
              "  <OutputFormat>EMF</OutputFormat>" +
              "  <PageWidth>{0}cm</PageWidth>" +
              "  <PageHeight>{1}cm</PageHeight>" +
              "  <MarginTop>{2}cm</MarginTop>" +
              "  <MarginLeft>{3}cm</MarginLeft>" +
              "  <MarginRight>{4}cm</MarginRight>" +
              "  <MarginBottom>{5}cm</MarginBottom>" +
              "</DeviceInfo>",
              _emf_Width,
              _emf_Height,
              _emf_Top,
              _emf_Left,
              _emf_Right,
              _emf_Bottom); 
            Warning[] warnings;
            _report.Render(printType.ToString(), deviceInfo, CreateStream, out warnings);
            foreach (Stream stream in _streams)
            {
                stream.Position = 0;
            }
            if (_streams == null || _streams.Count == 0)
            {
                return null;
            }
            else
            {
                return _streams[0].ToByteArray();
            }
        }
 
        public void Dispose()
        {
            if (_streams.Count > 0)
            {
                foreach (Stream stream in _streams)
                    stream.Close();
                _streams.Clear();
            }
        }
 
 
        void IDisposable.Dispose()
        {
            _report.Dispose();
            GC.SuppressFinalize(this);
        }
    }
}