hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ 7edc8b5f
이력 | 보기 | 이력해설 | 다운로드 (10.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 | 4601d8de | Gyusu | if (OFD.ShowDialog() == DialogResult.OK) |
42 | { |
||
43 | string pdfpath = OFD.FileName; |
||
44 | |||
45 | Txt_PDFPath.Text = pdfpath; |
||
46 | } |
||
47 | } |
||
48 | 391aa70f | gaqhf | |
49 | private void Chk_Scale_CheckedChanged(object sender, EventArgs e) |
||
50 | 4601d8de | Gyusu | { |
51 | 391aa70f | gaqhf | SacleCheckedChange(); |
52 | } |
||
53 | |||
54 | private void SacleCheckedChange() |
||
55 | { |
||
56 | if (Chk_Scale.Checked) |
||
57 | { |
||
58 | Txt_Length.Enabled = false; |
||
59 | Combo_LengthType.Enabled = false; |
||
60 | Combo_Scale.Enabled = true; |
||
61 | } |
||
62 | else |
||
63 | 4601d8de | Gyusu | { |
64 | 391aa70f | gaqhf | Txt_Length.Enabled = true; |
65 | Combo_LengthType.Enabled = true; |
||
66 | Combo_Scale.Enabled = false; |
||
67 | 4601d8de | Gyusu | } |
68 | } |
||
69 | |||
70 | |||
71 | 391aa70f | gaqhf | private void Btn_Convert_Click(object sender, EventArgs e) |
72 | 4601d8de | Gyusu | { |
73 | 391aa70f | gaqhf | int iScale = 0; |
74 | double iLength = 0; |
||
75 | bool bResult = true; |
||
76 | string sFilePath = Txt_PDFPath.Text; |
||
77 | 4601d8de | Gyusu | |
78 | 391aa70f | gaqhf | if (Chk_Scale.Checked) |
79 | iScale = Convert.ToInt32(Combo_Scale.SelectedItem.ToString().Split(new char[] { ' ' })[1]); |
||
80 | else |
||
81 | 4601d8de | Gyusu | { |
82 | 391aa70f | gaqhf | if (!double.TryParse(Txt_Length.Text, out iLength) || iLength < 300) |
83 | bResult = false; |
||
84 | |||
85 | 4601d8de | Gyusu | } |
86 | |||
87 | 391aa70f | gaqhf | if (!File.Exists(sFilePath)) |
88 | bResult = false; |
||
89 | |||
90 | |||
91 | if (bResult) |
||
92 | 4601d8de | Gyusu | { |
93 | 66be3f2d | Gyusu | string sPathNonExtension = Txt_PDFPath.Text.Replace(Path.GetExtension(Txt_PDFPath.Text), ""); |
94 | 391aa70f | gaqhf | PdfDocumentProcessor pdp = new PdfDocumentProcessor(); |
95 | pdp.LoadDocument(sFilePath); |
||
96 | 66be3f2d | Gyusu | |
97 | 391aa70f | gaqhf | try |
98 | 66be3f2d | Gyusu | { |
99 | 391aa70f | gaqhf | for (int i = 0; i < pdp.Document.Pages.Count; i++) |
100 | 66be3f2d | Gyusu | { |
101 | 391aa70f | gaqhf | PdfPage page = pdp.Document.Pages[i]; |
102 | |||
103 | double dWidth = page.CropBox.Width; |
||
104 | double dHeight = page.CropBox.Height; |
||
105 | |||
106 | if (Chk_Scale.Checked) |
||
107 | 76f465d0 | Gyusu | { |
108 | 391aa70f | gaqhf | dWidth = dWidth * iScale; |
109 | dHeight = dHeight * iScale; |
||
110 | 76f465d0 | Gyusu | } |
111 | 391aa70f | gaqhf | else |
112 | 66be3f2d | Gyusu | { |
113 | 391aa70f | gaqhf | if (Combo_LengthType.SelectedItem.ToString() == "Width") |
114 | 66be3f2d | Gyusu | { |
115 | 391aa70f | gaqhf | double dScale = dWidth / iLength; |
116 | dWidth = iLength; |
||
117 | dHeight = dHeight / dScale; |
||
118 | } |
||
119 | else |
||
120 | { |
||
121 | double dScale = dHeight / iLength; |
||
122 | dHeight = iLength; |
||
123 | dWidth = dWidth / dScale; |
||
124 | 66be3f2d | Gyusu | } |
125 | } |
||
126 | 391aa70f | gaqhf | |
127 | double dMax = Math.Max(dWidth, dHeight); |
||
128 | |||
129 | using (Bitmap bitmap = pdp.CreateBitmap(i + 1, (int)dMax)) |
||
130 | 66be3f2d | Gyusu | { |
131 | 391aa70f | gaqhf | bitmap.Save(sPathNonExtension + "_Page" + (i + 1) + ".png"); |
132 | 66be3f2d | Gyusu | } |
133 | } |
||
134 | 391aa70f | gaqhf | |
135 | MessageBox.Show("변환완료"); |
||
136 | } |
||
137 | catch (Exception ex) |
||
138 | { |
||
139 | MessageBox.Show("변환 이미지의 사이즈를 줄여주십시오"); |
||
140 | 66be3f2d | Gyusu | } |
141 | 83615cfb | gaqhf | |
142 | pdp.CloseDocument(); |
||
143 | 4601d8de | Gyusu | } |
144 | else |
||
145 | { |
||
146 | 391aa70f | gaqhf | MessageBox.Show("PDF 경로와 Scale 또는 Width or Height를 확인해주세요"); |
147 | 4601d8de | Gyusu | } |
148 | |||
149 | 391aa70f | gaqhf | |
150 | |||
151 | |||
152 | //Properties.Settings.Default._Dpi = Combo_Scale.Text; |
||
153 | //Properties.Settings.Default._Height = Txt_Length.Text; |
||
154 | //Properties.Settings.Default._Width = Txt_Width.Text; |
||
155 | //Properties.Settings.Default.Save(); |
||
156 | ///// ConvertPdfToImage(Txt_PDFPath.Text); |
||
157 | ///// |
||
158 | |||
159 | |||
160 | |||
161 | //pdftron.PDFNet.Initialize("daelim.co.kr(Doftech Corp):CPU:2::W:AMC(20120315):EF6E886F25A414FFB5F8C1F2999CF2DA33DC6C5164315BAF7011B87AF0FA"); |
||
162 | //using (PDFDoc doc = new PDFDoc(Txt_PDFPath.Text)) |
||
163 | //{ |
||
164 | // ImageCodecInfo DefaultImageCodecInfo = GetEncoderInfo("image/png"); |
||
165 | // EncoderParameters DefaultEncoderParameters = new EncoderParameters(2); |
||
166 | // System.Drawing.Imaging.Encoder QualityEncoder = System.Drawing.Imaging.Encoder.Quality; |
||
167 | // System.Drawing.Imaging.Encoder ColorDepthEncoder = System.Drawing.Imaging.Encoder.ColorDepth; |
||
168 | // DefaultEncoderParameters.Param[0] = new EncoderParameter(QualityEncoder, 100L); |
||
169 | // DefaultEncoderParameters.Param[1] = new EncoderParameter(ColorDepthEncoder, 1L); |
||
170 | |||
171 | // #region 이미지 만들기 |
||
172 | // for (int i = 1; i < doc.GetPageCount() + 1; i++) |
||
173 | // { |
||
174 | // string sFileName = ""; |
||
175 | // string pagePath = sPathNonExtension; |
||
176 | // if (i > 1) |
||
177 | // { |
||
178 | // pagePath = sPathNonExtension + "_" + i; |
||
179 | // } |
||
180 | // try |
||
181 | // { |
||
182 | // using (pdftron.PDF.PDFDraw draw = new pdftron.PDF.PDFDraw()) |
||
183 | // { |
||
184 | // ElementBuilder bld = new ElementBuilder(); |
||
185 | // ElementWriter writer = new ElementWriter(); |
||
186 | // if (Chk_Scale.Checked) |
||
187 | // { |
||
188 | // float fDpix = 600; |
||
189 | // float fDpiy = 600; |
||
190 | // try |
||
191 | // { |
||
192 | // fDpix = float.Parse(Combo_Scale.Text); |
||
193 | // fDpiy = float.Parse(Combo_Scale.Text); |
||
194 | // } |
||
195 | // catch |
||
196 | // { } |
||
197 | |||
198 | // var rotation = doc.GetPage(i).GetRotation(); |
||
199 | |||
200 | // draw.SetDPI(fDpix); |
||
201 | // sFileName = pagePath + "_" + fDpix + ".jpeg"; |
||
202 | // } |
||
203 | // else |
||
204 | // { |
||
205 | // var widthData =int.Parse(Txt_Width.Text); |
||
206 | // int heightData = int.Parse(Txt_Length.Text); |
||
207 | |||
208 | // var rotation = doc.GetPage(i).GetRotation(); |
||
209 | // draw.SetImageSize(widthData, heightData, true); |
||
210 | // sFileName = pagePath + "_" + widthData + "_" + heightData + ".png"; |
||
211 | // } |
||
212 | // draw.SetAntiAliasing(false); |
||
213 | // draw.SetImageSmoothing(false); |
||
214 | // Bitmap newBmp_ = draw.GetBitmap(doc.GetPage(i)); |
||
215 | |||
216 | // using (MemoryStream _savestream = new MemoryStream()) |
||
217 | // { |
||
218 | // try |
||
219 | // { |
||
220 | // newBmp_.Save(_savestream, DefaultImageCodecInfo, DefaultEncoderParameters); |
||
221 | // using (Bitmap bmpImage = new Bitmap(_savestream)) |
||
222 | // { |
||
223 | // ObjSet objset = new ObjSet(); |
||
224 | // Obj jbig2_hint = objset.CreateName("png"); |
||
225 | // bmpImage.Save(sFileName, ImageFormat.Png); |
||
226 | // bmpImage.Dispose(); |
||
227 | // // Do something with the Bitmap object |
||
228 | // } |
||
229 | // } |
||
230 | // catch (Exception ex) |
||
231 | // { |
||
232 | // MessageBox.Show("err" + "\n" + ex.ToString() + "\n" + ex.Message); |
||
233 | // } |
||
234 | |||
235 | // } |
||
236 | // GC.Collect(); |
||
237 | // GC.WaitForPendingFinalizers(); |
||
238 | // } |
||
239 | // } |
||
240 | // catch (Exception ex) |
||
241 | // { |
||
242 | // MessageBox.Show("err2" + "\n" + ex.ToString() + "\n" + ex.Message); |
||
243 | // // return false; |
||
244 | // } |
||
245 | // } |
||
246 | // if (doc.GetPageCount() > 0) |
||
247 | // { |
||
248 | // MessageBox.Show("변환완료"); |
||
249 | // } |
||
250 | // #endregion |
||
251 | //} |
||
252 | 4601d8de | Gyusu | } |
253 | 391aa70f | gaqhf | |
254 | |||
255 | 4601d8de | Gyusu | } |
256 | } |