프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ 47a4970e

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

1
using pdftron.PDF;
2
using pdftron.SDF;
3
using System;
4
using System.Collections.Generic;
5
using System.ComponentModel;
6
using System.Data;
7
using System.Drawing;
8
using System.Drawing.Imaging;
9
using System.IO;
10
using System.Linq;
11
using System.Text;
12
using System.Threading.Tasks;
13
using System.Windows.Forms;
14

    
15
namespace PDF_TO_IMAGE
16
{
17
    public partial class ConvertImage : Form
18
    {
19
        public ConvertImage()
20
        {
21
            InitializeComponent();
22
            Combo_DPI.Text = Properties.Settings.Default._Dpi;
23
            Txt_Height.Text = Properties.Settings.Default._Height;
24
            Txt_Width.Text = Properties.Settings.Default._Width;
25
        }
26

    
27
        private void Btn_LoadPDF_Click(object sender, EventArgs e)
28
        {
29
            OpenFileDialog OFD = new OpenFileDialog() { Filter = "PDF Files (.PDF)|*.PDF" };
30
            if (OFD.ShowDialog() == DialogResult.OK)
31
            {
32
                string pdfpath = OFD.FileName;
33

    
34
                Txt_PDFPath.Text = pdfpath;
35

    
36
            }
37
        }
38
        private static ImageCodecInfo GetEncoderInfo(String mimeType)
39
        {
40
            int j;
41
            ImageCodecInfo[] encoders;
42
            encoders = ImageCodecInfo.GetImageEncoders();
43
            for (j = 0; j < encoders.Length; ++j)
44
            {
45
                if (encoders[j].MimeType == mimeType)
46
                    return encoders[j];
47
            }
48
            return null;
49
        }
50

    
51
        private bool ConvertPdfToImage(string FilePath)
52

    
53
        {
54
            string sPathNonExtension = FilePath.Replace(Path.GetExtension(FilePath), "");
55

    
56

    
57
            pdftron.PDFNet.Initialize("daelim.co.kr(Doftech Corp):CPU:2::W:AMC(20120315):EF6E886F25A414FFB5F8C1F2999CF2DA33DC6C5164315BAF7011B87AF0FA");
58
            using (PDFDoc doc = new PDFDoc(FilePath))
59
            {
60
              
61
                ImageCodecInfo DefaultImageCodecInfo = GetEncoderInfo("image/png");
62
                EncoderParameters DefaultEncoderParameters = new EncoderParameters(2);
63
                System.Drawing.Imaging.Encoder QualityEncoder = System.Drawing.Imaging.Encoder.Quality;
64
                System.Drawing.Imaging.Encoder ColorDepthEncoder = System.Drawing.Imaging.Encoder.ColorDepth;
65
                DefaultEncoderParameters.Param[0] = new EncoderParameter(QualityEncoder, 100L);
66
                DefaultEncoderParameters.Param[1] = new EncoderParameter(ColorDepthEncoder, 8L);
67

    
68
                #region 이미지 만들기
69
                for (int i = 1; i < doc.GetPageCount() + 1; i++)
70
                {
71
                    try
72
                    {
73
                        using (pdftron.PDF.PDFDraw draw = new pdftron.PDF.PDFDraw())
74
                        {
75
                            float fDpix = 600;
76
                            float fDpiy = 600;
77
                            try
78
                            {fDpix = float.Parse(Combo_DPI.Text);
79
                            fDpiy = float.Parse(Combo_DPI.Text);}
80
                            catch
81
                            {}
82
                            ElementBuilder bld = new ElementBuilder();
83
                            ElementWriter writer = new ElementWriter();
84
                            var rotation = doc.GetPage(i).GetRotation();
85
                            draw.SetAntiAliasing(false);
86
                            draw.SetDPI(fDpix);
87
                            Bitmap newBmp_ = draw.GetBitmap(doc.GetPage(i));
88
             
89
                            using (MemoryStream _savestream = new MemoryStream())
90
                            {
91
                                try
92
                                {
93
                                    newBmp_.Save(_savestream, DefaultImageCodecInfo, DefaultEncoderParameters);
94
                                    newBmp_ = new Bitmap(_savestream);
95
                                    ObjSet objset = new ObjSet();
96
                                    Obj jbig2_hint = objset.CreateName("png");
97
                                    string pagePath = sPathNonExtension;
98
                                    if (i > 1)
99
                                    {
100
                                        pagePath = sPathNonExtension + "_" + i;
101
                                    }
102
                                    newBmp_.Save(pagePath + "_" + fDpix + ".png");
103
                                    newBmp_.Dispose();
104
                                }
105
                                catch(Exception ex)
106
                                {
107

    
108
                                }
109
                               
110
                            }
111
                            GC.Collect();
112
                            GC.WaitForPendingFinalizers();
113
                        }
114
                    }
115
                    catch (Exception ex)
116
                    {
117
                       // return false;
118
                    }
119
                }
120
                if(doc.GetPageCount() > 0)
121
                {
122
                    MessageBox.Show("변환완료");
123
                }
124
                    #endregion
125
                    return true;
126
            }
127
        }
128

    
129
        private void Btn_Convert_Click(object sender, EventArgs e)
130
        {
131
            int iDpi = 0;
132
            if(Txt_PDFPath.Text != "" && int.TryParse(Combo_DPI.Text,out iDpi) == true)
133
            {
134
                Properties.Settings.Default._Dpi = Combo_DPI.Text;
135
                Properties.Settings.Default._Height = Txt_Height.Text;
136
                Properties.Settings.Default._Width = Txt_Width.Text;
137
                Properties.Settings.Default.Save();
138
                ///   ConvertPdfToImage(Txt_PDFPath.Text);
139
                ///   
140
                string sPathNonExtension = Txt_PDFPath.Text.Replace(Path.GetExtension(Txt_PDFPath.Text), "");
141

    
142

    
143
                pdftron.PDFNet.Initialize("daelim.co.kr(Doftech Corp):CPU:2::W:AMC(20120315):EF6E886F25A414FFB5F8C1F2999CF2DA33DC6C5164315BAF7011B87AF0FA");
144
                using (PDFDoc doc = new PDFDoc(Txt_PDFPath.Text))
145
                {
146
                    ImageCodecInfo DefaultImageCodecInfo = GetEncoderInfo("image/png");
147
                    EncoderParameters DefaultEncoderParameters = new EncoderParameters(2);
148
                    System.Drawing.Imaging.Encoder QualityEncoder = System.Drawing.Imaging.Encoder.Quality;
149
                    System.Drawing.Imaging.Encoder ColorDepthEncoder = System.Drawing.Imaging.Encoder.ColorDepth;
150
                    DefaultEncoderParameters.Param[0] = new EncoderParameter(QualityEncoder, 100L);
151
                    DefaultEncoderParameters.Param[1] = new EncoderParameter(ColorDepthEncoder, 1L);
152

    
153
                    #region 이미지 만들기
154
                    for (int i = 1; i < doc.GetPageCount() + 1; i++)
155
                    {
156
                        string sFileName = "";
157
                        string pagePath = sPathNonExtension;
158
                        if (i > 1)
159
                        {
160
                            pagePath = sPathNonExtension + "_" + i;
161
                        }
162
                        try
163
                        {
164
                            using (pdftron.PDF.PDFDraw draw = new pdftron.PDF.PDFDraw())
165
                            {
166
                                ElementBuilder bld = new ElementBuilder();
167
                                ElementWriter writer = new ElementWriter();
168
                                if (Chk_DPI.Checked)
169
                                {
170
                                    float fDpix = 600;
171
                                    float fDpiy = 600;
172
                                    try
173
                                    {
174
                                        fDpix = float.Parse(Combo_DPI.Text);
175
                                        fDpiy = float.Parse(Combo_DPI.Text);
176
                                    }
177
                                    catch
178
                                    { }
179
         
180
                                    var rotation = doc.GetPage(i).GetRotation();
181
                
182
                                    draw.SetDPI(fDpix);
183
                                    sFileName = pagePath + "_" + fDpix + ".jpeg";
184
                                }
185
                                else
186
                                {    
187
                                    var widthData =int.Parse(Txt_Width.Text);
188
                                    int heightData = int.Parse(Txt_Height.Text);
189
                  
190
                                    var rotation = doc.GetPage(i).GetRotation();
191
                                    draw.SetImageSize(widthData, heightData, true);
192
                                    sFileName = pagePath + "_" + widthData + "_" + heightData + ".png";
193
                                }
194
                                draw.SetAntiAliasing(false);
195
                                draw.SetImageSmoothing(false);
196
                                Bitmap newBmp_ = draw.GetBitmap(doc.GetPage(i));
197

    
198
                                using (MemoryStream _savestream = new MemoryStream())
199
                                {
200
                                    try
201
                                    {
202
                                        newBmp_.Save(_savestream, DefaultImageCodecInfo, DefaultEncoderParameters);
203
                                        using (Bitmap bmpImage = new Bitmap(_savestream))
204
                                        {
205
                                            ObjSet objset = new ObjSet();
206
                                            Obj jbig2_hint = objset.CreateName("png");
207
                                            bmpImage.Save(sFileName, ImageFormat.Png);
208
                                            bmpImage.Dispose();
209
                                            // Do something with the Bitmap object
210
                                        }
211
                                    }
212
                                    catch (Exception ex)
213
                                    {
214
                                        MessageBox.Show("err" + "\n" + ex.ToString() + "\n" + ex.Message);
215
                                    }
216

    
217
                                }
218
                                GC.Collect();
219
                                GC.WaitForPendingFinalizers();
220
                            }
221
                        }
222
                        catch (Exception ex)
223
                        {
224
                            MessageBox.Show("err2" + "\n" + ex.ToString() + "\n" + ex.Message);
225
                            // return false;
226
                        }
227
                    }
228
                    if (doc.GetPageCount() > 0)
229
                    {
230
                        MessageBox.Show("변환완료");
231
                    }
232
                    #endregion
233
                }
234
            }
235
            else
236
            {
237
                MessageBox.Show("PDF 경로와 DPI를 확인해주세요");
238
            }
239

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