hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ f5c168dd
이력 | 보기 | 이력해설 | 다운로드 (17.4 KB)
1 | bef3613f | humkyung | 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 | 3abc20b4 | esham21 | if (MessageBox.Show("Do you want to merge files into one?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) |
54 | bef3613f | humkyung | { |
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 | 3abc20b4 | esham21 | MessageBox.Show("Finished"); |
141 | bef3613f | humkyung | } |
142 | catch (Exception ex) |
||
143 | { |
||
144 | 3abc20b4 | esham21 | MessageBox.Show("Please reduce the size of the converted image"); |
145 | bef3613f | humkyung | } |
146 | |||
147 | pdp.CloseDocument(); |
||
148 | splashScreenManager1.CloseWaitForm(); |
||
149 | } |
||
150 | else |
||
151 | { |
||
152 | 3abc20b4 | esham21 | MessageBox.Show("Please check PDF path and Scale or Width or Height"); |
153 | bef3613f | humkyung | } |
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 | /// <summary> |
||
187 | /// export selected all pdf files to image files |
||
188 | /// </summary> |
||
189 | /// <param name="sender"></param> |
||
190 | /// <param name="e"></param> |
||
191 | private void Btn_Convert_Click(object sender, EventArgs e) |
||
192 | { |
||
193 | int iScale = 0; |
||
194 | double iLength = 0; |
||
195 | bool bResult = true; |
||
196 | string sFilePath = Txt_PDFPath.Text; |
||
197 | |||
198 | if (Chk_Scale.Checked) |
||
199 | iScale = Convert.ToInt32(Combo_Scale.SelectedItem.ToString().Split(new char[] { ' ' })[1]); |
||
200 | else |
||
201 | { |
||
202 | if (!double.TryParse(Txt_Length.Text, out iLength) || iLength < 300) |
||
203 | bResult = false; |
||
204 | |||
205 | } |
||
206 | |||
207 | if (System.IO.Directory.Exists(sFilePath) && bResult) |
||
208 | { |
||
209 | DirectoryInfo directory = new DirectoryInfo(Txt_PDFPath.Text); |
||
210 | if (directory.Exists) |
||
211 | { |
||
212 | bool result = true; |
||
213 | foreach (var item in directory.GetFiles("*.pdf", SearchOption.TopDirectoryOnly)) |
||
214 | { |
||
215 | string sPathWithoutExt = Path.Combine(Path.GetDirectoryName(item.FullName), Path.GetFileNameWithoutExtension(item.FullName)); |
||
216 | using (PdfDocumentProcessor pdp = new PdfDocumentProcessor()) |
||
217 | { |
||
218 | pdp.LoadDocument(item.FullName, true); |
||
219 | |||
220 | /// read all words from pdf file |
||
221 | Dictionary<int, List<PageWord>> PageWordMap = new Dictionary<int, List<PageWord>>(); |
||
222 | PdfPageWord currentWord = pdp.NextWord(); |
||
223 | while (currentWord != null) |
||
224 | { |
||
225 | PageWord word = new PageWord(currentWord); |
||
226 | if(!PageWordMap.ContainsKey(word.PageNumber)) |
||
227 | { |
||
228 | PageWordMap[word.PageNumber] = new List<PageWord>(); |
||
229 | } |
||
230 | PageWordMap[word.PageNumber].Add(word); |
||
231 | //Switch to the next word |
||
232 | currentWord = pdp.NextWord(); |
||
233 | 08811885 | humkyung | } |
234 | /// up to here |
||
235 | |||
236 | bef3613f | humkyung | splashScreenManager1.ShowWaitForm(); |
237 | splashScreenManager1.SetWaitFormCaption("Convert Pdf File to Image Files"); |
||
238 | try |
||
239 | { |
||
240 | int iPage = pdp.Document.Pages.Count; |
||
241 | string sPage = iPage.ToString(); |
||
242 | for (int i = 0; i < pdp.Document.Pages.Count; i++) |
||
243 | { |
||
244 | int iPercent = i * 100 / iPage; |
||
245 | splashScreenManager1.SetWaitFormDescription(i + " / " + sPage + " Pages ( " + iPercent + "% )"); |
||
246 | PdfPage page = pdp.Document.Pages[i]; |
||
247 | |||
248 | double dWidth = page.CropBox.Width; |
||
249 | double dHeight = page.CropBox.Height; |
||
250 | |||
251 | System.Drawing.Drawing2D.Matrix mat = new System.Drawing.Drawing2D.Matrix(); |
||
252 | mat.Rotate(page.Rotate); |
||
253 | if (page.Rotate == 90 || page.Rotate == 270) |
||
254 | { |
||
255 | double tempValue = dWidth; |
||
256 | dWidth = dHeight; |
||
257 | dHeight = tempValue; |
||
258 | } |
||
259 | |||
260 | double dScale = iScale; |
||
261 | if (Chk_Scale.Checked) |
||
262 | { |
||
263 | dWidth = dWidth * iScale; |
||
264 | dHeight = dHeight * iScale; |
||
265 | } |
||
266 | else |
||
267 | { |
||
268 | if (Combo_LengthType.SelectedItem.ToString() == "Width") |
||
269 | { |
||
270 | dScale = iLength / dWidth; |
||
271 | dWidth = iLength; |
||
272 | dHeight = dHeight * dScale; |
||
273 | } |
||
274 | else |
||
275 | { |
||
276 | dScale = iLength / dHeight; |
||
277 | dHeight = iLength; |
||
278 | dWidth = dWidth * dScale; |
||
279 | } |
||
280 | } |
||
281 | |||
282 | double dMax = Math.Max(dWidth, dHeight); |
||
283 | using (Bitmap bitmap = pdp.CreateBitmap(i + 1, (int)dMax)) |
||
284 | { |
||
285 | if (pdp.Document.Pages.Count != 1) |
||
286 | bitmap.Save(sPathWithoutExt + "_Page" + (i + 1) + ".png", System.Drawing.Imaging.ImageFormat.Png); |
||
287 | else |
||
288 | bitmap.Save(sPathWithoutExt + ".png", System.Drawing.Imaging.ImageFormat.Png); |
||
289 | } |
||
290 | |||
291 | /// save text to file |
||
292 | 08811885 | humkyung | if (PageWordMap.ContainsKey(i + 1)) |
293 | bef3613f | humkyung | { |
294 | 08811885 | humkyung | System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); |
295 | System.Xml.XmlNode xmlRoot = xmlDoc.CreateElement("Texts"); |
||
296 | foreach (PageWord word in PageWordMap[i + 1]) |
||
297 | { |
||
298 | System.Xml.XmlNode xmlText = xmlDoc.CreateElement("Text"); |
||
299 | System.Xml.XmlAttribute pos = xmlDoc.CreateAttribute("Position"); |
||
300 | PointF[] array = { new PointF((float)(word.Left * dScale), (float)(word.Top * dScale)) }; |
||
301 | mat.TransformPoints(array); |
||
302 | pos.Value = $"{System.Math.Abs(array[0].X)},{System.Math.Abs(array[0].Y)}"; |
||
303 | xmlText.Attributes.Append(pos); |
||
304 | System.Xml.XmlAttribute angle = xmlDoc.CreateAttribute("Angle"); |
||
305 | double dAngle = (word.Angle - page.Rotate) >= 0 ? (word.Angle - page.Rotate) : 360 + (word.Angle - page.Rotate); |
||
306 | angle.Value = $"{dAngle}"; |
||
307 | xmlText.Attributes.Append(angle); |
||
308 | System.Xml.XmlAttribute xmlWidth = xmlDoc.CreateAttribute("Width"); |
||
309 | xmlWidth.Value = $"{word.Width * dScale}"; |
||
310 | xmlText.Attributes.Append(xmlWidth); |
||
311 | System.Xml.XmlAttribute xmlHeight = xmlDoc.CreateAttribute("Height"); |
||
312 | xmlHeight.Value = $"{word.Height * dScale}"; |
||
313 | xmlText.Attributes.Append(xmlHeight); |
||
314 | xmlText.InnerText = word.Text; |
||
315 | |||
316 | xmlRoot.AppendChild(xmlText); |
||
317 | } |
||
318 | xmlDoc.AppendChild(xmlRoot); |
||
319 | xmlDoc.Save($"{sPathWithoutExt}_Page{i + 1}.xml"); |
||
320 | bef3613f | humkyung | } |
321 | /// up to here |
||
322 | |||
323 | iPercent = (i + 1) * 100 / iPage; |
||
324 | splashScreenManager1.SetWaitFormDescription((i + 1) + " / " + sPage + " Pages ( " + iPercent + "% )"); |
||
325 | } |
||
326 | } |
||
327 | catch (Exception ex) |
||
328 | { |
||
329 | result = false; |
||
330 | 3abc20b4 | esham21 | MessageBox.Show("Please check the error : " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
331 | bef3613f | humkyung | break; |
332 | } |
||
333 | |||
334 | pdp.CloseDocument(); |
||
335 | splashScreenManager1.CloseWaitForm(); |
||
336 | } |
||
337 | } |
||
338 | |||
339 | if (result) |
||
340 | 3abc20b4 | esham21 | this.labelStatus.Text = "Finished"; |
341 | bef3613f | humkyung | } |
342 | else |
||
343 | { |
||
344 | 3abc20b4 | esham21 | MessageBox.Show("Please check PDF path and Scale or Width or Height"); |
345 | bef3613f | humkyung | } |
346 | } |
||
347 | else |
||
348 | { |
||
349 | 3abc20b4 | esham21 | MessageBox.Show("Please check PDF path and Scale or Width or Height"); |
350 | bef3613f | humkyung | } |
351 | } |
||
352 | } |
||
353 | } |