프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ 156dae38

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

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

    
14
namespace PDF_TO_IMAGE
15
{
16
    public partial class ConvertImage : Form
17
    {
18
        public ConvertImage()
19
        {
20
            InitializeComponent();
21

    
22
            Combo_Scale.SelectedIndex = Properties.Settings.Default._Scale;
23
            Combo_LengthType.SelectedIndex = Properties.Settings.Default._LengthType;
24
            Txt_Length.Text = Properties.Settings.Default._Length;
25
            Chk_Scale.Checked = Properties.Settings.Default._UseScale;
26
            SacleCheckedChange();
27
        }
28

    
29
        private void ConvertImage_FormClosing(object sender, FormClosingEventArgs e)
30
        {
31
            Properties.Settings.Default._Scale = Combo_Scale.SelectedIndex;
32
            Properties.Settings.Default._LengthType = Combo_LengthType.SelectedIndex;
33
            Properties.Settings.Default._Length = Txt_Length.Text;
34
            Properties.Settings.Default._UseScale = Chk_Scale.Checked;
35
            Properties.Settings.Default.Save();
36
        }
37

    
38
        private void Btn_LoadPDF_Click(object sender, EventArgs e)
39
        {
40
            OpenFileDialog OFD = new OpenFileDialog() { Filter = "PDF Files (.PDF)|*.PDF" };
41
            if (OFD.ShowDialog() == DialogResult.OK)
42
            {
43
                string pdfpath = OFD.FileName;
44

    
45
                Txt_PDFPath.Text = pdfpath;
46
            }
47
        }
48

    
49
        private void Chk_Scale_CheckedChanged(object sender, EventArgs e)
50
        {
51
            SacleCheckedChange();
52
        }
53

    
54
        private void SacleCheckedChange()
55
        {
56
            if (Chk_Scale.Checked)
57
            {
58
                Txt_Length.Enabled = false;
59
                Combo_LengthType.Enabled = false;
60
                Combo_Scale.Enabled = true;
61
            }
62
            else
63
            {
64
                Txt_Length.Enabled = true;
65
                Combo_LengthType.Enabled = true;
66
                Combo_Scale.Enabled = false;
67
            }
68
        }
69

    
70

    
71
        private void Btn_Convert_Click(object sender, EventArgs e)
72
        {
73
            int iScale = 0;
74
            double iLength = 0;
75
            bool bResult = true;
76
            string sFilePath = Txt_PDFPath.Text;
77

    
78
            if (Chk_Scale.Checked)
79
                iScale = Convert.ToInt32(Combo_Scale.SelectedItem.ToString().Split(new char[] { ' ' })[1]);
80
            else
81
            {
82
                if (!double.TryParse(Txt_Length.Text, out iLength) || iLength < 300)
83
                    bResult = false;
84
                    
85
            }
86

    
87
            if (!File.Exists(sFilePath))
88
                bResult = false;
89

    
90

    
91
            if (bResult)
92
            {
93
                string sPathNonExtension = Txt_PDFPath.Text.Replace(Path.GetExtension(Txt_PDFPath.Text), "");
94
                PdfDocumentProcessor pdp = new PdfDocumentProcessor();
95
                pdp.LoadDocument(sFilePath);
96

    
97
                splashScreenManager1.ShowWaitForm();
98
                splashScreenManager1.SetWaitFormCaption("Convert Pdf File to Image Files");
99
                try
100
                {
101
                    int iPage = pdp.Document.Pages.Count;
102
                    string sPage = iPage.ToString();
103
                    for (int i = 0; i < pdp.Document.Pages.Count; i++)
104
                    {
105
                        int iPercent = i * 100 / iPage;
106
                        splashScreenManager1.SetWaitFormDescription(i + " / " + sPage + " Pages ( " + iPercent + "% )");
107
                        PdfPage page = pdp.Document.Pages[i];
108

    
109
                        double dWidth = page.CropBox.Width;
110
                        double dHeight = page.CropBox.Height;
111

    
112
                        if (Chk_Scale.Checked)
113
                        {
114
                            dWidth = dWidth * iScale;
115
                            dHeight = dHeight * iScale;
116
                        }
117
                        else
118
                        {
119
                            if (Combo_LengthType.SelectedItem.ToString() == "Width")
120
                            {
121
                                double dScale = dWidth / iLength;
122
                                dWidth = iLength;
123
                                dHeight = dHeight / dScale;
124
                            }
125
                            else
126
                            {
127
                                double dScale = dHeight / iLength;
128
                                dHeight = iLength;
129
                                dWidth = dWidth / dScale;
130
                            }
131
                        }
132

    
133
                        double dMax = Math.Max(dWidth, dHeight);
134

    
135
                        using (Bitmap bitmap = pdp.CreateBitmap(i + 1, (int)dMax))
136
                        {
137
                            bitmap.Save(sPathNonExtension + "_Page" + (i + 1) + ".png");
138
                        }
139

    
140
                        iPercent = (i + 1) * 100 / iPage;
141
                        splashScreenManager1.SetWaitFormDescription((i + 1) + " / " + sPage + " Pages ( " + iPercent + "% )");
142
                    }
143

    
144
                    MessageBox.Show("변환완료");
145
                }
146
                catch (Exception ex)
147
                {
148
                    MessageBox.Show("변환 이미지의 사이즈를 줄여주십시오");
149
                }
150

    
151
                pdp.CloseDocument();
152
                splashScreenManager1.CloseWaitForm();
153
            }
154
            else
155
            {
156
                MessageBox.Show("PDF 경로와 Scale 또는 Width or Height를 확인해주세요");
157
            }
158

    
159
                
160

    
161

    
162
                //Properties.Settings.Default._Dpi = Combo_Scale.Text;
163
                //Properties.Settings.Default._Height = Txt_Length.Text;
164
                //Properties.Settings.Default._Width = Txt_Width.Text;
165
                //Properties.Settings.Default.Save();
166
                /////   ConvertPdfToImage(Txt_PDFPath.Text);
167
                /////   
168
                
169

    
170

    
171
                //pdftron.PDFNet.Initialize("daelim.co.kr(Doftech Corp):CPU:2::W:AMC(20120315):EF6E886F25A414FFB5F8C1F2999CF2DA33DC6C5164315BAF7011B87AF0FA");
172
                //using (PDFDoc doc = new PDFDoc(Txt_PDFPath.Text))
173
                //{
174
                //    ImageCodecInfo DefaultImageCodecInfo = GetEncoderInfo("image/png");
175
                //    EncoderParameters DefaultEncoderParameters = new EncoderParameters(2);
176
                //    System.Drawing.Imaging.Encoder QualityEncoder = System.Drawing.Imaging.Encoder.Quality;
177
                //    System.Drawing.Imaging.Encoder ColorDepthEncoder = System.Drawing.Imaging.Encoder.ColorDepth;
178
                //    DefaultEncoderParameters.Param[0] = new EncoderParameter(QualityEncoder, 100L);
179
                //    DefaultEncoderParameters.Param[1] = new EncoderParameter(ColorDepthEncoder, 1L);
180

    
181
                //    #region 이미지 만들기
182
                //    for (int i = 1; i < doc.GetPageCount() + 1; i++)
183
                //    {
184
                //        string sFileName = "";
185
                //        string pagePath = sPathNonExtension;
186
                //        if (i > 1)
187
                //        {
188
                //            pagePath = sPathNonExtension + "_" + i;
189
                //        }
190
                //        try
191
                //        {
192
                //            using (pdftron.PDF.PDFDraw draw = new pdftron.PDF.PDFDraw())
193
                //            {
194
                //                ElementBuilder bld = new ElementBuilder();
195
                //                ElementWriter writer = new ElementWriter();
196
                //                if (Chk_Scale.Checked)
197
                //                {
198
                //                    float fDpix = 600;
199
                //                    float fDpiy = 600;
200
                //                    try
201
                //                    {
202
                //                        fDpix = float.Parse(Combo_Scale.Text);
203
                //                        fDpiy = float.Parse(Combo_Scale.Text);
204
                //                    }
205
                //                    catch
206
                //                    { }
207
         
208
                //                    var rotation = doc.GetPage(i).GetRotation();
209
                
210
                //                    draw.SetDPI(fDpix);
211
                //                    sFileName = pagePath + "_" + fDpix + ".jpeg";
212
                //                }
213
                //                else
214
                //                {    
215
                //                    var widthData =int.Parse(Txt_Width.Text);
216
                //                    int heightData = int.Parse(Txt_Length.Text);
217
                  
218
                //                    var rotation = doc.GetPage(i).GetRotation();
219
                //                    draw.SetImageSize(widthData, heightData, true);
220
                //                    sFileName = pagePath + "_" + widthData + "_" + heightData + ".png";
221
                //                }
222
                //                draw.SetAntiAliasing(false);
223
                //                draw.SetImageSmoothing(false);
224
                //                Bitmap newBmp_ = draw.GetBitmap(doc.GetPage(i));
225

    
226
                //                using (MemoryStream _savestream = new MemoryStream())
227
                //                {
228
                //                    try
229
                //                    {
230
                //                        newBmp_.Save(_savestream, DefaultImageCodecInfo, DefaultEncoderParameters);
231
                //                        using (Bitmap bmpImage = new Bitmap(_savestream))
232
                //                        {
233
                //                            ObjSet objset = new ObjSet();
234
                //                            Obj jbig2_hint = objset.CreateName("png");
235
                //                            bmpImage.Save(sFileName, ImageFormat.Png);
236
                //                            bmpImage.Dispose();
237
                //                            // Do something with the Bitmap object
238
                //                        }
239
                //                    }
240
                //                    catch (Exception ex)
241
                //                    {
242
                //                        MessageBox.Show("err" + "\n" + ex.ToString() + "\n" + ex.Message);
243
                //                    }
244

    
245
                //                }
246
                //                GC.Collect();
247
                //                GC.WaitForPendingFinalizers();
248
                //            }
249
                //        }
250
                //        catch (Exception ex)
251
                //        {
252
                //            MessageBox.Show("err2" + "\n" + ex.ToString() + "\n" + ex.Message);
253
                //            // return false;
254
                //        }
255
                //    }
256
                //    if (doc.GetPageCount() > 0)
257
                //    {
258
                //        MessageBox.Show("변환완료");
259
                //    }
260
                //    #endregion
261
                //}
262
        }
263

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