프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ 5cd39521

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

1 391aa70f gaqhf
using System;
2 4601d8de Gyusu
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 391aa70f gaqhf
using DevExpress.Pdf;
13 4601d8de Gyusu
14
namespace PDF_TO_IMAGE
15
{
16
    public partial class ConvertImage : Form
17
    {
18
        public ConvertImage()
19
        {
20
            InitializeComponent();
21 391aa70f gaqhf
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 4601d8de Gyusu
        }
37
38
        private void Btn_LoadPDF_Click(object sender, EventArgs e)
39
        {
40 79a518e3 gaqhf
            FolderBrowserDialog dia = new FolderBrowserDialog();
41
            if (dia.ShowDialog() == DialogResult.OK)
42
            {
43
                Txt_PDFPath.Text = dia.SelectedPath;
44
            }
45
46
            return;
47 6547b5f1 humkyung
            OpenFileDialog OFD = new OpenFileDialog() { Filter = "PDF Files (.PDF)|*.PDF" };
48 224535bb gaqhf
            OFD.Multiselect = true;
49 4601d8de Gyusu
            if (OFD.ShowDialog() == DialogResult.OK)
50
            {
51 224535bb gaqhf
                if (OFD.FileNames.Length > 1)
52
                {
53
                    if (MessageBox.Show("여러 파일을 하나로 합치고 진행하시겠습니까?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
54
                    {
55
                        SaveFileDialog saveFileDialog = new SaveFileDialog();
56
                        saveFileDialog.Filter = "PDF Files (.PDF)|*.PDF";
57
                        if (saveFileDialog.ShowDialog() == DialogResult.OK)
58
                        {
59
                            int iScale = 0;
60
                            double iLength = 0;
61
                            bool bResult = true;
62
63
                            if (Chk_Scale.Checked)
64
                                iScale = Convert.ToInt32(Combo_Scale.SelectedItem.ToString().Split(new char[] { ' ' })[1]);
65
                            else
66
                            {
67
                                if (!double.TryParse(Txt_Length.Text, out iLength) || iLength < 300)
68
                                    bResult = false;
69
70
                            }
71
72
                            if (bResult)
73
                            {
74
                                splashScreenManager1.ShowWaitForm();
75
                                splashScreenManager1.SetWaitFormCaption("Convert Pdf File to Image Files");
76
77
                                PdfDocumentProcessor pdp = new PdfDocumentProcessor();
78
                                pdp.CreateEmptyDocument();
79
                                foreach (var fileName in OFD.FileNames)
80
                                {
81
                                    PdfDocumentProcessor selectFile = new PdfDocumentProcessor();
82
                                    selectFile.LoadDocument(fileName);
83
                                    foreach (var page in selectFile.Document.Pages)
84
                                        pdp.Document.Pages.Add(page);
85
                                    selectFile.CloseDocument();
86
                                    selectFile.Dispose();
87
                                    selectFile = null;
88
                                }
89
90
                                pdp.SaveDocument(saveFileDialog.FileName);
91
92
                                string sPathNonExtension = saveFileDialog.FileName.Replace(Path.GetExtension(saveFileDialog.FileName), "");
93
94
95
                                try
96
                                {
97
                                    int iPage = pdp.Document.Pages.Count;
98
                                    string sPage = iPage.ToString();
99
                                    for (int i = 0; i < pdp.Document.Pages.Count; i++)
100
                                    {
101
                                        int iPercent = i * 100 / iPage;
102
                                        splashScreenManager1.SetWaitFormDescription(i + " / " + sPage + " Pages ( " + iPercent + "% )");
103
                                        PdfPage page = pdp.Document.Pages[i];
104
105
                                        double dWidth = page.CropBox.Width;
106
                                        double dHeight = page.CropBox.Height;
107 4601d8de Gyusu
108 224535bb gaqhf
                                        if (Chk_Scale.Checked)
109
                                        {
110
                                            dWidth = dWidth * iScale;
111
                                            dHeight = dHeight * iScale;
112
                                        }
113
                                        else
114
                                        {
115
                                            if (Combo_LengthType.SelectedItem.ToString() == "Width")
116
                                            {
117
                                                double dScale = dWidth / iLength;
118
                                                dWidth = iLength;
119
                                                dHeight = dHeight / dScale;
120
                                            }
121
                                            else
122
                                            {
123
                                                double dScale = dHeight / iLength;
124
                                                dHeight = iLength;
125
                                                dWidth = dWidth / dScale;
126
                                            }
127
                                        }
128
129
                                        double dMax = Math.Max(dWidth, dHeight);
130
131
                                        using (Bitmap bitmap = pdp.CreateBitmap(i + 1, (int)dMax))
132
                                        {
133
                                            bitmap.Save(sPathNonExtension + "_Page" + (i + 1) + ".png");
134
                                        }
135
136
                                        iPercent = (i + 1) * 100 / iPage;
137
                                        splashScreenManager1.SetWaitFormDescription((i + 1) + " / " + sPage + " Pages ( " + iPercent + "% )");
138
                                    }
139
140
                                    MessageBox.Show("변환완료");
141
                                }
142
                                catch (Exception ex)
143
                                {
144
                                    MessageBox.Show("변환 이미지의 사이즈를 줄여주십시오");
145
                                }
146
147
                                pdp.CloseDocument();
148
                                splashScreenManager1.CloseWaitForm();
149
                            }
150
                            else
151
                            {
152
                                MessageBox.Show("PDF 경로와 Scale 또는 Width or Height를 확인해주세요");
153
                            }
154
                        }
155
                    }
156
                }
157
                else
158
                {
159
                    string pdfpath = OFD.FileName;
160
                    Txt_PDFPath.Text = pdfpath;
161
                }
162 4601d8de Gyusu
            }
163
        }
164 391aa70f gaqhf
165
        private void Chk_Scale_CheckedChanged(object sender, EventArgs e)
166 4601d8de Gyusu
        {
167 391aa70f gaqhf
            SacleCheckedChange();
168
        }
169
170
        private void SacleCheckedChange()
171
        {
172
            if (Chk_Scale.Checked)
173
            {
174
                Txt_Length.Enabled = false;
175
                Combo_LengthType.Enabled = false;
176
                Combo_Scale.Enabled = true;
177
            }
178
            else
179 4601d8de Gyusu
            {
180 391aa70f gaqhf
                Txt_Length.Enabled = true;
181
                Combo_LengthType.Enabled = true;
182
                Combo_Scale.Enabled = false;
183 4601d8de Gyusu
            }
184
        }
185
186
187 391aa70f gaqhf
        private void Btn_Convert_Click(object sender, EventArgs e)
188 4601d8de Gyusu
        {
189 391aa70f gaqhf
            int iScale = 0;
190
            double iLength = 0;
191
            bool bResult = true;
192
            string sFilePath = Txt_PDFPath.Text;
193 4601d8de Gyusu
194 391aa70f gaqhf
            if (Chk_Scale.Checked)
195
                iScale = Convert.ToInt32(Combo_Scale.SelectedItem.ToString().Split(new char[] { ' ' })[1]);
196
            else
197 4601d8de Gyusu
            {
198 391aa70f gaqhf
                if (!double.TryParse(Txt_Length.Text, out iLength) || iLength < 300)
199
                    bResult = false;
200
201 79a518e3 gaqhf
            }
202 391aa70f gaqhf
203
            if (bResult)
204 4601d8de Gyusu
            {
205 79a518e3 gaqhf
                DirectoryInfo directory = new DirectoryInfo(Txt_PDFPath.Text);
206
                if (directory.Exists)
207 66be3f2d Gyusu
                {
208 79a518e3 gaqhf
                    bool result = true;
209
                    foreach (var item in directory.GetFiles("*.pdf",SearchOption.TopDirectoryOnly))
210 66be3f2d Gyusu
                    {
211 79a518e3 gaqhf
                        string sPathNonExtension = item.FullName.Replace(Path.GetExtension(item.FullName), "");
212
                        PdfDocumentProcessor pdp = new PdfDocumentProcessor();
213
                        pdp.LoadDocument(item.FullName);
214 391aa70f gaqhf
215 79a518e3 gaqhf
                        splashScreenManager1.ShowWaitForm();
216
                        splashScreenManager1.SetWaitFormCaption("Convert Pdf File to Image Files");
217
                        try
218 9774d01e gaqhf
                        {
219 79a518e3 gaqhf
                            int iPage = pdp.Document.Pages.Count;
220
                            string sPage = iPage.ToString();
221
                            for (int i = 0; i < pdp.Document.Pages.Count; i++)
222 66be3f2d Gyusu
                            {
223 79a518e3 gaqhf
                                int iPercent = i * 100 / iPage;
224
                                splashScreenManager1.SetWaitFormDescription(i + " / " + sPage + " Pages ( " + iPercent + "% )");
225
                                PdfPage page = pdp.Document.Pages[i];
226
227
                                double dWidth = page.CropBox.Width;
228
                                double dHeight = page.CropBox.Height;
229 391aa70f gaqhf
230 79a518e3 gaqhf
                                if (page.Rotate == 90 || page.Rotate == 270)
231
                                {
232
                                    double tempValue = dWidth;
233
                                    dWidth = dHeight;
234
                                    dHeight = tempValue;
235
                                }
236 391aa70f gaqhf
237 79a518e3 gaqhf
                                if (Chk_Scale.Checked)
238
                                {
239
                                    dWidth = dWidth * iScale;
240
                                    dHeight = dHeight * iScale;
241
                                }
242
                                else
243
                                {
244
                                    if (Combo_LengthType.SelectedItem.ToString() == "Width")
245
                                    {
246
                                        double dScale = dWidth / iLength;
247
                                        dWidth = iLength;
248
                                        dHeight = dHeight / dScale;
249
                                    }
250
                                    else
251
                                    {
252
                                        double dScale = dHeight / iLength;
253
                                        dHeight = iLength;
254
                                        dWidth = dWidth / dScale;
255
                                    }
256
                                }
257
258
                                double dMax = Math.Max(dWidth, dHeight);
259
260
                                using (Bitmap bitmap = pdp.CreateBitmap(i + 1, (int)dMax))
261
                                {
262 c33fbd9b gaqhf
                                    if (pdp.Document.Pages.Count != 1)
263
                                        bitmap.Save(sPathNonExtension + "_Page" + (i + 1) + ".png");
264
                                    else
265
                                        bitmap.Save(sPathNonExtension + ".png");
266 79a518e3 gaqhf
                                }
267
268
                                iPercent = (i + 1) * 100 / iPage;
269
                                splashScreenManager1.SetWaitFormDescription((i + 1) + " / " + sPage + " Pages ( " + iPercent + "% )");
270
                            }
271
                        }
272
                        catch (Exception ex)
273 66be3f2d Gyusu
                        {
274 79a518e3 gaqhf
                            result = false;
275
                            MessageBox.Show("변환 이미지의 사이즈를 줄여주십시오");
276
                            break;
277 66be3f2d Gyusu
                        }
278 9f04ece0 gaqhf
279 79a518e3 gaqhf
                        pdp.CloseDocument();
280
                        splashScreenManager1.CloseWaitForm();
281 66be3f2d Gyusu
                    }
282 391aa70f gaqhf
283 79a518e3 gaqhf
                    if (result)
284
                        MessageBox.Show("변환완료");
285 391aa70f gaqhf
                }
286 79a518e3 gaqhf
                else
287 391aa70f gaqhf
                {
288 79a518e3 gaqhf
                    MessageBox.Show("PDF 경로와 Scale 또는 Width or Height를 확인해주세요");
289 66be3f2d Gyusu
                }
290 4601d8de Gyusu
            }
291
            else
292
            {
293 391aa70f gaqhf
                MessageBox.Show("PDF 경로와 Scale 또는 Width or Height를 확인해주세요");
294 4601d8de Gyusu
            }
295
        }
296 391aa70f gaqhf
297
        
298 4601d8de Gyusu
    }
299
}
클립보드 이미지 추가 (최대 크기: 500 MB)