hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ 224535bb
이력 | 보기 | 이력해설 | 다운로드 (11.3 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 | 6547b5f1 | humkyung | OpenFileDialog OFD = new OpenFileDialog() { Filter = "PDF Files (.PDF)|*.PDF" }; |
41 | 224535bb | gaqhf | OFD.Multiselect = true; |
42 | 4601d8de | Gyusu | if (OFD.ShowDialog() == DialogResult.OK) |
43 | { |
||
44 | 224535bb | gaqhf | 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 | 4601d8de | Gyusu | |
101 | 224535bb | gaqhf | 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 | 4601d8de | Gyusu | } |
156 | } |
||
157 | 391aa70f | gaqhf | |
158 | private void Chk_Scale_CheckedChanged(object sender, EventArgs e) |
||
159 | 4601d8de | Gyusu | { |
160 | 391aa70f | gaqhf | 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 | 4601d8de | Gyusu | { |
173 | 391aa70f | gaqhf | Txt_Length.Enabled = true; |
174 | Combo_LengthType.Enabled = true; |
||
175 | Combo_Scale.Enabled = false; |
||
176 | 4601d8de | Gyusu | } |
177 | } |
||
178 | |||
179 | |||
180 | 391aa70f | gaqhf | private void Btn_Convert_Click(object sender, EventArgs e) |
181 | 4601d8de | Gyusu | { |
182 | 391aa70f | gaqhf | int iScale = 0; |
183 | double iLength = 0; |
||
184 | bool bResult = true; |
||
185 | string sFilePath = Txt_PDFPath.Text; |
||
186 | 4601d8de | Gyusu | |
187 | 391aa70f | gaqhf | if (Chk_Scale.Checked) |
188 | iScale = Convert.ToInt32(Combo_Scale.SelectedItem.ToString().Split(new char[] { ' ' })[1]); |
||
189 | else |
||
190 | 4601d8de | Gyusu | { |
191 | 391aa70f | gaqhf | if (!double.TryParse(Txt_Length.Text, out iLength) || iLength < 300) |
192 | bResult = false; |
||
193 | |||
194 | 4601d8de | Gyusu | } |
195 | |||
196 | 391aa70f | gaqhf | if (!File.Exists(sFilePath)) |
197 | bResult = false; |
||
198 | |||
199 | |||
200 | if (bResult) |
||
201 | 4601d8de | Gyusu | { |
202 | 66be3f2d | Gyusu | string sPathNonExtension = Txt_PDFPath.Text.Replace(Path.GetExtension(Txt_PDFPath.Text), ""); |
203 | 391aa70f | gaqhf | PdfDocumentProcessor pdp = new PdfDocumentProcessor(); |
204 | pdp.LoadDocument(sFilePath); |
||
205 | 66be3f2d | Gyusu | |
206 | 50df80f6 | gaqhf | splashScreenManager1.ShowWaitForm(); |
207 | splashScreenManager1.SetWaitFormCaption("Convert Pdf File to Image Files"); |
||
208 | 391aa70f | gaqhf | try |
209 | 66be3f2d | Gyusu | { |
210 | 9f04ece0 | gaqhf | int iPage = pdp.Document.Pages.Count; |
211 | string sPage = iPage.ToString(); |
||
212 | 391aa70f | gaqhf | for (int i = 0; i < pdp.Document.Pages.Count; i++) |
213 | 66be3f2d | Gyusu | { |
214 | 9f04ece0 | gaqhf | int iPercent = i * 100 / iPage; |
215 | 50df80f6 | gaqhf | splashScreenManager1.SetWaitFormDescription(i + " / " + sPage + " Pages ( " + iPercent + "% )"); |
216 | 391aa70f | gaqhf | 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 | 76f465d0 | Gyusu | { |
223 | 391aa70f | gaqhf | dWidth = dWidth * iScale; |
224 | dHeight = dHeight * iScale; |
||
225 | 76f465d0 | Gyusu | } |
226 | 391aa70f | gaqhf | else |
227 | 66be3f2d | Gyusu | { |
228 | 391aa70f | gaqhf | if (Combo_LengthType.SelectedItem.ToString() == "Width") |
229 | 66be3f2d | Gyusu | { |
230 | 391aa70f | gaqhf | 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 | 66be3f2d | Gyusu | } |
240 | } |
||
241 | 391aa70f | gaqhf | |
242 | double dMax = Math.Max(dWidth, dHeight); |
||
243 | |||
244 | using (Bitmap bitmap = pdp.CreateBitmap(i + 1, (int)dMax)) |
||
245 | 66be3f2d | Gyusu | { |
246 | 391aa70f | gaqhf | bitmap.Save(sPathNonExtension + "_Page" + (i + 1) + ".png"); |
247 | 66be3f2d | Gyusu | } |
248 | 9f04ece0 | gaqhf | |
249 | iPercent = (i + 1) * 100 / iPage; |
||
250 | 50df80f6 | gaqhf | splashScreenManager1.SetWaitFormDescription((i + 1) + " / " + sPage + " Pages ( " + iPercent + "% )"); |
251 | 66be3f2d | Gyusu | } |
252 | 391aa70f | gaqhf | |
253 | MessageBox.Show("변환완료"); |
||
254 | } |
||
255 | catch (Exception ex) |
||
256 | { |
||
257 | MessageBox.Show("변환 이미지의 사이즈를 줄여주십시오"); |
||
258 | 66be3f2d | Gyusu | } |
259 | 83615cfb | gaqhf | |
260 | pdp.CloseDocument(); |
||
261 | 50df80f6 | gaqhf | splashScreenManager1.CloseWaitForm(); |
262 | 4601d8de | Gyusu | } |
263 | else |
||
264 | { |
||
265 | 391aa70f | gaqhf | MessageBox.Show("PDF 경로와 Scale 또는 Width or Height를 확인해주세요"); |
266 | 4601d8de | Gyusu | } |
267 | } |
||
268 | 391aa70f | gaqhf | |
269 | |||
270 | 4601d8de | Gyusu | } |
271 | } |