hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ fe6060ea
이력 | 보기 | 이력해설 | 다운로드 (12.9 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 |
FolderBrowserDialog dia = new FolderBrowserDialog(); |
41 |
if (dia.ShowDialog() == DialogResult.OK) |
42 |
{ |
43 |
Txt_PDFPath.Text = dia.SelectedPath; |
44 |
} |
45 |
|
46 |
return; |
47 |
OpenFileDialog OFD = new OpenFileDialog() { Filter = "PDF Files (.PDF)|*.PDF" }; |
48 |
OFD.Multiselect = true; |
49 |
if (OFD.ShowDialog() == DialogResult.OK) |
50 |
{ |
51 |
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 |
|
108 |
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 |
} |
163 |
} |
164 |
|
165 |
private void Chk_Scale_CheckedChanged(object sender, EventArgs e) |
166 |
{ |
167 |
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 |
{ |
180 |
Txt_Length.Enabled = true; |
181 |
Combo_LengthType.Enabled = true; |
182 |
Combo_Scale.Enabled = false; |
183 |
} |
184 |
} |
185 |
|
186 |
|
187 |
private void Btn_Convert_Click(object sender, EventArgs e) |
188 |
{ |
189 |
int iScale = 0; |
190 |
double iLength = 0; |
191 |
bool bResult = true; |
192 |
string sFilePath = Txt_PDFPath.Text; |
193 |
|
194 |
if (Chk_Scale.Checked) |
195 |
iScale = Convert.ToInt32(Combo_Scale.SelectedItem.ToString().Split(new char[] { ' ' })[1]); |
196 |
else |
197 |
{ |
198 |
if (!double.TryParse(Txt_Length.Text, out iLength) || iLength < 300) |
199 |
bResult = false; |
200 |
|
201 |
} |
202 |
|
203 |
if (bResult) |
204 |
{ |
205 |
DirectoryInfo directory = new DirectoryInfo(Txt_PDFPath.Text); |
206 |
if (directory.Exists) |
207 |
{ |
208 |
bool result = true; |
209 |
foreach (var item in directory.GetFiles("*.pdf",SearchOption.TopDirectoryOnly)) |
210 |
{ |
211 |
string sPathNonExtension = item.FullName.Replace(Path.GetExtension(item.FullName), ""); |
212 |
PdfDocumentProcessor pdp = new PdfDocumentProcessor(); |
213 |
pdp.LoadDocument(item.FullName); |
214 |
|
215 |
splashScreenManager1.ShowWaitForm(); |
216 |
splashScreenManager1.SetWaitFormCaption("Convert Pdf File to Image Files"); |
217 |
try |
218 |
{ |
219 |
int iPage = pdp.Document.Pages.Count; |
220 |
string sPage = iPage.ToString(); |
221 |
for (int i = 0; i < pdp.Document.Pages.Count; i++) |
222 |
{ |
223 |
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 |
|
230 |
if (page.Rotate == 90 || page.Rotate == 270) |
231 |
{ |
232 |
double tempValue = dWidth; |
233 |
dWidth = dHeight; |
234 |
dHeight = tempValue; |
235 |
} |
236 |
|
237 |
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 |
if (pdp.Document.Pages.Count != 1) |
263 |
bitmap.Save(sPathNonExtension + "_Page" + (i + 1) + ".png"); |
264 |
else |
265 |
bitmap.Save(sPathNonExtension + ".png"); |
266 |
} |
267 |
|
268 |
iPercent = (i + 1) * 100 / iPage; |
269 |
splashScreenManager1.SetWaitFormDescription((i + 1) + " / " + sPage + " Pages ( " + iPercent + "% )"); |
270 |
} |
271 |
} |
272 |
catch (Exception ex) |
273 |
{ |
274 |
result = false; |
275 |
MessageBox.Show("변환 이미지의 사이즈를 줄여주십시오"); |
276 |
break; |
277 |
} |
278 |
|
279 |
pdp.CloseDocument(); |
280 |
splashScreenManager1.CloseWaitForm(); |
281 |
} |
282 |
|
283 |
if (result) |
284 |
MessageBox.Show("변환완료"); |
285 |
} |
286 |
else |
287 |
{ |
288 |
MessageBox.Show("PDF 경로와 Scale 또는 Width or Height를 확인해주세요"); |
289 |
} |
290 |
} |
291 |
else |
292 |
{ |
293 |
MessageBox.Show("PDF 경로와 Scale 또는 Width or Height를 확인해주세요"); |
294 |
} |
295 |
} |
296 |
|
297 |
|
298 |
} |
299 |
} |