hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ d45df999
이력 | 보기 | 이력해설 | 다운로드 (10.7 KB)
1 | 4601d8de | Gyusu | using pdftron.PDF; |
---|---|---|---|
2 | using pdftron.SDF; |
||
3 | using System; |
||
4 | using System.Collections.Generic; |
||
5 | using System.ComponentModel; |
||
6 | using System.Data; |
||
7 | using System.Drawing; |
||
8 | using System.Drawing.Imaging; |
||
9 | using System.IO; |
||
10 | using System.Linq; |
||
11 | using System.Text; |
||
12 | using System.Threading.Tasks; |
||
13 | using System.Windows.Forms; |
||
14 | |||
15 | namespace PDF_TO_IMAGE |
||
16 | { |
||
17 | public partial class ConvertImage : Form |
||
18 | { |
||
19 | public ConvertImage() |
||
20 | { |
||
21 | InitializeComponent(); |
||
22 | Combo_DPI.Text = Properties.Settings.Default._Dpi; |
||
23 | Txt_Height.Text = Properties.Settings.Default._Height; |
||
24 | Txt_Width.Text = Properties.Settings.Default._Width; |
||
25 | } |
||
26 | |||
27 | private void Btn_LoadPDF_Click(object sender, EventArgs e) |
||
28 | { |
||
29 | OpenFileDialog OFD = new OpenFileDialog(); |
||
30 | OFD.Filter = "PDF Files (.PDF)|*.PDF"; |
||
31 | if (OFD.ShowDialog() == DialogResult.OK) |
||
32 | { |
||
33 | string pdfpath = OFD.FileName; |
||
34 | |||
35 | Txt_PDFPath.Text = pdfpath; |
||
36 | |||
37 | } |
||
38 | } |
||
39 | private static ImageCodecInfo GetEncoderInfo(String mimeType) |
||
40 | { |
||
41 | int j; |
||
42 | ImageCodecInfo[] encoders; |
||
43 | encoders = ImageCodecInfo.GetImageEncoders(); |
||
44 | for (j = 0; j < encoders.Length; ++j) |
||
45 | { |
||
46 | if (encoders[j].MimeType == mimeType) |
||
47 | return encoders[j]; |
||
48 | } |
||
49 | return null; |
||
50 | } |
||
51 | |||
52 | private bool ConvertPdfToImage(string FilePath) |
||
53 | |||
54 | { |
||
55 | string sPathNonExtension = FilePath.Replace(Path.GetExtension(FilePath), ""); |
||
56 | |||
57 | |||
58 | fcc0783d | Gyusu | pdftron.PDFNet.Initialize("daelim.co.kr(Doftech Corp):CPU:2::W:AMC(20120315):EF6E886F25A414FFB5F8C1F2999CF2DA33DC6C5164315BAF7011B87AF0FA"); |
59 | 4601d8de | Gyusu | using (PDFDoc doc = new PDFDoc(FilePath)) |
60 | { |
||
61 | 66be3f2d | Gyusu | |
62 | 4601d8de | Gyusu | ImageCodecInfo DefaultImageCodecInfo = GetEncoderInfo("image/png"); |
63 | EncoderParameters DefaultEncoderParameters = new EncoderParameters(2); |
||
64 | System.Drawing.Imaging.Encoder QualityEncoder = System.Drawing.Imaging.Encoder.Quality; |
||
65 | System.Drawing.Imaging.Encoder ColorDepthEncoder = System.Drawing.Imaging.Encoder.ColorDepth; |
||
66 | DefaultEncoderParameters.Param[0] = new EncoderParameter(QualityEncoder, 100L); |
||
67 | DefaultEncoderParameters.Param[1] = new EncoderParameter(ColorDepthEncoder, 8L); |
||
68 | |||
69 | #region 이미지 만들기 |
||
70 | for (int i = 1; i < doc.GetPageCount() + 1; i++) |
||
71 | { |
||
72 | try |
||
73 | { |
||
74 | using (pdftron.PDF.PDFDraw draw = new pdftron.PDF.PDFDraw()) |
||
75 | { |
||
76 | float fDpix = 600; |
||
77 | float fDpiy = 600; |
||
78 | try |
||
79 | {fDpix = float.Parse(Combo_DPI.Text); |
||
80 | fDpiy = float.Parse(Combo_DPI.Text);} |
||
81 | catch |
||
82 | {} |
||
83 | ElementBuilder bld = new ElementBuilder(); |
||
84 | ElementWriter writer = new ElementWriter(); |
||
85 | var rotation = doc.GetPage(i).GetRotation(); |
||
86 | 66be3f2d | Gyusu | draw.SetAntiAliasing(false); |
87 | 4601d8de | Gyusu | draw.SetDPI(fDpix); |
88 | 66be3f2d | Gyusu | Bitmap newBmp_ = draw.GetBitmap(doc.GetPage(i)); |
89 | |||
90 | 4601d8de | Gyusu | using (MemoryStream _savestream = new MemoryStream()) |
91 | { |
||
92 | 66be3f2d | Gyusu | try |
93 | 4601d8de | Gyusu | { |
94 | 66be3f2d | Gyusu | newBmp_.Save(_savestream, DefaultImageCodecInfo, DefaultEncoderParameters); |
95 | newBmp_ = new Bitmap(_savestream); |
||
96 | ObjSet objset = new ObjSet(); |
||
97 | Obj jbig2_hint = objset.CreateName("png"); |
||
98 | string pagePath = sPathNonExtension; |
||
99 | if (i > 1) |
||
100 | { |
||
101 | pagePath = sPathNonExtension + "_" + i; |
||
102 | } |
||
103 | newBmp_.Save(pagePath + "_" + fDpix + ".png"); |
||
104 | newBmp_.Dispose(); |
||
105 | 4601d8de | Gyusu | } |
106 | 66be3f2d | Gyusu | catch(Exception ex) |
107 | { |
||
108 | |||
109 | } |
||
110 | |||
111 | 4601d8de | Gyusu | } |
112 | GC.Collect(); |
||
113 | GC.WaitForPendingFinalizers(); |
||
114 | } |
||
115 | } |
||
116 | catch (Exception ex) |
||
117 | { |
||
118 | 66be3f2d | Gyusu | // return false; |
119 | 4601d8de | Gyusu | } |
120 | } |
||
121 | fcc0783d | Gyusu | if(doc.GetPageCount() > 0) |
122 | { |
||
123 | MessageBox.Show("변환완료"); |
||
124 | } |
||
125 | #endregion |
||
126 | return true; |
||
127 | 4601d8de | Gyusu | } |
128 | } |
||
129 | |||
130 | private void Btn_Convert_Click(object sender, EventArgs e) |
||
131 | { |
||
132 | 37656a07 | esham21 | |
133 | 4601d8de | Gyusu | int iDpi = 0; |
134 | if(Txt_PDFPath.Text != "" && int.TryParse(Combo_DPI.Text,out iDpi) == true) |
||
135 | { |
||
136 | Properties.Settings.Default._Dpi = Combo_DPI.Text; |
||
137 | Properties.Settings.Default._Height = Txt_Height.Text; |
||
138 | Properties.Settings.Default._Width = Txt_Width.Text; |
||
139 | Properties.Settings.Default.Save(); |
||
140 | 66be3f2d | Gyusu | /// ConvertPdfToImage(Txt_PDFPath.Text); |
141 | /// |
||
142 | string sPathNonExtension = Txt_PDFPath.Text.Replace(Path.GetExtension(Txt_PDFPath.Text), ""); |
||
143 | |||
144 | |||
145 | pdftron.PDFNet.Initialize("daelim.co.kr(Doftech Corp):CPU:2::W:AMC(20120315):EF6E886F25A414FFB5F8C1F2999CF2DA33DC6C5164315BAF7011B87AF0FA"); |
||
146 | using (PDFDoc doc = new PDFDoc(Txt_PDFPath.Text)) |
||
147 | { |
||
148 | |||
149 | ImageCodecInfo DefaultImageCodecInfo = GetEncoderInfo("image/png"); |
||
150 | EncoderParameters DefaultEncoderParameters = new EncoderParameters(2); |
||
151 | System.Drawing.Imaging.Encoder QualityEncoder = System.Drawing.Imaging.Encoder.Quality; |
||
152 | System.Drawing.Imaging.Encoder ColorDepthEncoder = System.Drawing.Imaging.Encoder.ColorDepth; |
||
153 | DefaultEncoderParameters.Param[0] = new EncoderParameter(QualityEncoder, 100L); |
||
154 | 76f465d0 | Gyusu | DefaultEncoderParameters.Param[1] = new EncoderParameter(ColorDepthEncoder, 1L); |
155 | 66be3f2d | Gyusu | |
156 | #region 이미지 만들기 |
||
157 | for (int i = 1; i < doc.GetPageCount() + 1; i++) |
||
158 | { |
||
159 | 76f465d0 | Gyusu | string sFileName = ""; |
160 | string pagePath = sPathNonExtension; |
||
161 | if (i > 1) |
||
162 | { |
||
163 | pagePath = sPathNonExtension + "_" + i; |
||
164 | } |
||
165 | 66be3f2d | Gyusu | try |
166 | { |
||
167 | using (pdftron.PDF.PDFDraw draw = new pdftron.PDF.PDFDraw()) |
||
168 | { |
||
169 | ElementBuilder bld = new ElementBuilder(); |
||
170 | ElementWriter writer = new ElementWriter(); |
||
171 | 76f465d0 | Gyusu | if (Chk_DPI.Checked) |
172 | { |
||
173 | float fDpix = 600; |
||
174 | float fDpiy = 600; |
||
175 | try |
||
176 | { |
||
177 | fDpix = float.Parse(Combo_DPI.Text); |
||
178 | fDpiy = float.Parse(Combo_DPI.Text); |
||
179 | } |
||
180 | catch |
||
181 | { } |
||
182 | |||
183 | var rotation = doc.GetPage(i).GetRotation(); |
||
184 | |||
185 | draw.SetDPI(fDpix); |
||
186 | sFileName = pagePath + "_" + fDpix + ".jpeg"; |
||
187 | } |
||
188 | else |
||
189 | 37656a07 | esham21 | { |
190 | var widthData =int.Parse(Txt_Width.Text); |
||
191 | 76f465d0 | Gyusu | int heightData = int.Parse(Txt_Height.Text); |
192 | |||
193 | var rotation = doc.GetPage(i).GetRotation(); |
||
194 | draw.SetImageSize(widthData, heightData, true); |
||
195 | sFileName = pagePath + "_" + widthData + "_" + heightData + ".jpeg"; |
||
196 | } |
||
197 | 66be3f2d | Gyusu | draw.SetAntiAliasing(false); |
198 | 76f465d0 | Gyusu | draw.SetImageSmoothing(false); |
199 | 66be3f2d | Gyusu | Bitmap newBmp_ = draw.GetBitmap(doc.GetPage(i)); |
200 | |||
201 | using (MemoryStream _savestream = new MemoryStream()) |
||
202 | { |
||
203 | try |
||
204 | { |
||
205 | newBmp_.Save(_savestream, DefaultImageCodecInfo, DefaultEncoderParameters); |
||
206 | 76f465d0 | Gyusu | using (Bitmap bmpImage = new Bitmap(_savestream)) |
207 | 66be3f2d | Gyusu | { |
208 | 76f465d0 | Gyusu | ObjSet objset = new ObjSet(); |
209 | Obj jbig2_hint = objset.CreateName("jpeg"); |
||
210 | bmpImage.Save(sFileName); |
||
211 | bmpImage.Dispose(); |
||
212 | // Do something with the Bitmap object |
||
213 | 66be3f2d | Gyusu | } |
214 | 76f465d0 | Gyusu | // newBmp_ = new Bitmap(_savestream); |
215 | |||
216 | 66be3f2d | Gyusu | } |
217 | catch (Exception ex) |
||
218 | { |
||
219 | 37656a07 | esham21 | MessageBox.Show("err" + "\n" + ex.ToString() + "\n" + ex.Message); |
220 | 66be3f2d | Gyusu | } |
221 | |||
222 | } |
||
223 | GC.Collect(); |
||
224 | GC.WaitForPendingFinalizers(); |
||
225 | } |
||
226 | } |
||
227 | catch (Exception ex) |
||
228 | { |
||
229 | 37656a07 | esham21 | MessageBox.Show("err2" + "\n" + ex.ToString() + "\n" + ex.Message); |
230 | 66be3f2d | Gyusu | // return false; |
231 | } |
||
232 | } |
||
233 | if (doc.GetPageCount() > 0) |
||
234 | { |
||
235 | MessageBox.Show("변환완료"); |
||
236 | } |
||
237 | #endregion |
||
238 | } |
||
239 | |||
240 | |||
241 | 4601d8de | Gyusu | } |
242 | else |
||
243 | { |
||
244 | MessageBox.Show("PDF 경로와 DPI를 확인해주세요"); |
||
245 | } |
||
246 | |||
247 | } |
||
248 | } |
||
249 | } |