프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ 224535bb

이력 | 보기 | 이력해설 | 다운로드 (11.3 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
            OFD.Multiselect = true;
42
            if (OFD.ShowDialog() == DialogResult.OK)
43
            {
44
                if (OFD.FileNames.Length > 1)
45
                {
46
                    if (MessageBox.Show("여러 파일을 하나로 합치고 진행하시겠습니까?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
47
                    {
48
                        SaveFileDialog saveFileDialog = new SaveFileDialog();
49
                        saveFileDialog.Filter = "PDF Files (.PDF)|*.PDF";
50
                        if (saveFileDialog.ShowDialog() == DialogResult.OK)
51
                        {
52
                            int iScale = 0;
53
                            double iLength = 0;
54
                            bool bResult = true;
55

    
56
                            if (Chk_Scale.Checked)
57
                                iScale = Convert.ToInt32(Combo_Scale.SelectedItem.ToString().Split(new char[] { ' ' })[1]);
58
                            else
59
                            {
60
                                if (!double.TryParse(Txt_Length.Text, out iLength) || iLength < 300)
61
                                    bResult = false;
62

    
63
                            }
64

    
65
                            if (bResult)
66
                            {
67
                                splashScreenManager1.ShowWaitForm();
68
                                splashScreenManager1.SetWaitFormCaption("Convert Pdf File to Image Files");
69

    
70
                                PdfDocumentProcessor pdp = new PdfDocumentProcessor();
71
                                pdp.CreateEmptyDocument();
72
                                foreach (var fileName in OFD.FileNames)
73
                                {
74
                                    PdfDocumentProcessor selectFile = new PdfDocumentProcessor();
75
                                    selectFile.LoadDocument(fileName);
76
                                    foreach (var page in selectFile.Document.Pages)
77
                                        pdp.Document.Pages.Add(page);
78
                                    selectFile.CloseDocument();
79
                                    selectFile.Dispose();
80
                                    selectFile = null;
81
                                }
82

    
83
                                pdp.SaveDocument(saveFileDialog.FileName);
84

    
85
                                string sPathNonExtension = saveFileDialog.FileName.Replace(Path.GetExtension(saveFileDialog.FileName), "");
86

    
87

    
88
                                try
89
                                {
90
                                    int iPage = pdp.Document.Pages.Count;
91
                                    string sPage = iPage.ToString();
92
                                    for (int i = 0; i < pdp.Document.Pages.Count; i++)
93
                                    {
94
                                        int iPercent = i * 100 / iPage;
95
                                        splashScreenManager1.SetWaitFormDescription(i + " / " + sPage + " Pages ( " + iPercent + "% )");
96
                                        PdfPage page = pdp.Document.Pages[i];
97

    
98
                                        double dWidth = page.CropBox.Width;
99
                                        double dHeight = page.CropBox.Height;
100

    
101
                                        if (Chk_Scale.Checked)
102
                                        {
103
                                            dWidth = dWidth * iScale;
104
                                            dHeight = dHeight * iScale;
105
                                        }
106
                                        else
107
                                        {
108
                                            if (Combo_LengthType.SelectedItem.ToString() == "Width")
109
                                            {
110
                                                double dScale = dWidth / iLength;
111
                                                dWidth = iLength;
112
                                                dHeight = dHeight / dScale;
113
                                            }
114
                                            else
115
                                            {
116
                                                double dScale = dHeight / iLength;
117
                                                dHeight = iLength;
118
                                                dWidth = dWidth / dScale;
119
                                            }
120
                                        }
121

    
122
                                        double dMax = Math.Max(dWidth, dHeight);
123

    
124
                                        using (Bitmap bitmap = pdp.CreateBitmap(i + 1, (int)dMax))
125
                                        {
126
                                            bitmap.Save(sPathNonExtension + "_Page" + (i + 1) + ".png");
127
                                        }
128

    
129
                                        iPercent = (i + 1) * 100 / iPage;
130
                                        splashScreenManager1.SetWaitFormDescription((i + 1) + " / " + sPage + " Pages ( " + iPercent + "% )");
131
                                    }
132

    
133
                                    MessageBox.Show("변환완료");
134
                                }
135
                                catch (Exception ex)
136
                                {
137
                                    MessageBox.Show("변환 이미지의 사이즈를 줄여주십시오");
138
                                }
139

    
140
                                pdp.CloseDocument();
141
                                splashScreenManager1.CloseWaitForm();
142
                            }
143
                            else
144
                            {
145
                                MessageBox.Show("PDF 경로와 Scale 또는 Width or Height를 확인해주세요");
146
                            }
147
                        }
148
                    }
149
                }
150
                else
151
                {
152
                    string pdfpath = OFD.FileName;
153
                    Txt_PDFPath.Text = pdfpath;
154
                }
155
            }
156
        }
157

    
158
        private void Chk_Scale_CheckedChanged(object sender, EventArgs e)
159
        {
160
            SacleCheckedChange();
161
        }
162

    
163
        private void SacleCheckedChange()
164
        {
165
            if (Chk_Scale.Checked)
166
            {
167
                Txt_Length.Enabled = false;
168
                Combo_LengthType.Enabled = false;
169
                Combo_Scale.Enabled = true;
170
            }
171
            else
172
            {
173
                Txt_Length.Enabled = true;
174
                Combo_LengthType.Enabled = true;
175
                Combo_Scale.Enabled = false;
176
            }
177
        }
178

    
179

    
180
        private void Btn_Convert_Click(object sender, EventArgs e)
181
        {
182
            int iScale = 0;
183
            double iLength = 0;
184
            bool bResult = true;
185
            string sFilePath = Txt_PDFPath.Text;
186

    
187
            if (Chk_Scale.Checked)
188
                iScale = Convert.ToInt32(Combo_Scale.SelectedItem.ToString().Split(new char[] { ' ' })[1]);
189
            else
190
            {
191
                if (!double.TryParse(Txt_Length.Text, out iLength) || iLength < 300)
192
                    bResult = false;
193
                    
194
            }
195

    
196
            if (!File.Exists(sFilePath))
197
                bResult = false;
198

    
199

    
200
            if (bResult)
201
            {
202
                string sPathNonExtension = Txt_PDFPath.Text.Replace(Path.GetExtension(Txt_PDFPath.Text), "");
203
                PdfDocumentProcessor pdp = new PdfDocumentProcessor();
204
                pdp.LoadDocument(sFilePath);
205

    
206
                splashScreenManager1.ShowWaitForm();
207
                splashScreenManager1.SetWaitFormCaption("Convert Pdf File to Image Files");
208
                try
209
                {
210
                    int iPage = pdp.Document.Pages.Count;
211
                    string sPage = iPage.ToString();
212
                    for (int i = 0; i < pdp.Document.Pages.Count; i++)
213
                    {
214
                        int iPercent = i * 100 / iPage;
215
                        splashScreenManager1.SetWaitFormDescription(i + " / " + sPage + " Pages ( " + iPercent + "% )");
216
                        PdfPage page = pdp.Document.Pages[i];
217

    
218
                        double dWidth = page.CropBox.Width;
219
                        double dHeight = page.CropBox.Height;
220

    
221
                        if (Chk_Scale.Checked)
222
                        {
223
                            dWidth = dWidth * iScale;
224
                            dHeight = dHeight * iScale;
225
                        }
226
                        else
227
                        {
228
                            if (Combo_LengthType.SelectedItem.ToString() == "Width")
229
                            {
230
                                double dScale = dWidth / iLength;
231
                                dWidth = iLength;
232
                                dHeight = dHeight / dScale;
233
                            }
234
                            else
235
                            {
236
                                double dScale = dHeight / iLength;
237
                                dHeight = iLength;
238
                                dWidth = dWidth / dScale;
239
                            }
240
                        }
241

    
242
                        double dMax = Math.Max(dWidth, dHeight);
243

    
244
                        using (Bitmap bitmap = pdp.CreateBitmap(i + 1, (int)dMax))
245
                        {
246
                            bitmap.Save(sPathNonExtension + "_Page" + (i + 1) + ".png");
247
                        }
248

    
249
                        iPercent = (i + 1) * 100 / iPage;
250
                        splashScreenManager1.SetWaitFormDescription((i + 1) + " / " + sPage + " Pages ( " + iPercent + "% )");
251
                    }
252

    
253
                    MessageBox.Show("변환완료");
254
                }
255
                catch (Exception ex)
256
                {
257
                    MessageBox.Show("변환 이미지의 사이즈를 줄여주십시오");
258
                }
259

    
260
                pdp.CloseDocument();
261
                splashScreenManager1.CloseWaitForm();
262
            }
263
            else
264
            {
265
                MessageBox.Show("PDF 경로와 Scale 또는 Width or Height를 확인해주세요");
266
            }
267
        }
268

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