hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ 7edc8b5f
이력 | 보기 | 이력해설 | 다운로드 (10.3 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 |
OpenFileDialog OFD = new OpenFileDialog() { Filter = "PDF Files (.PDF)|*.PDF" }; |
41 |
if (OFD.ShowDialog() == DialogResult.OK) |
42 |
{ |
43 |
string pdfpath = OFD.FileName; |
44 |
|
45 |
Txt_PDFPath.Text = pdfpath; |
46 |
} |
47 |
} |
48 |
|
49 |
private void Chk_Scale_CheckedChanged(object sender, EventArgs e) |
50 |
{ |
51 |
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 |
{ |
64 |
Txt_Length.Enabled = true; |
65 |
Combo_LengthType.Enabled = true; |
66 |
Combo_Scale.Enabled = false; |
67 |
} |
68 |
} |
69 |
|
70 |
|
71 |
private void Btn_Convert_Click(object sender, EventArgs e) |
72 |
{ |
73 |
int iScale = 0; |
74 |
double iLength = 0; |
75 |
bool bResult = true; |
76 |
string sFilePath = Txt_PDFPath.Text; |
77 |
|
78 |
if (Chk_Scale.Checked) |
79 |
iScale = Convert.ToInt32(Combo_Scale.SelectedItem.ToString().Split(new char[] { ' ' })[1]); |
80 |
else |
81 |
{ |
82 |
if (!double.TryParse(Txt_Length.Text, out iLength) || iLength < 300) |
83 |
bResult = false; |
84 |
|
85 |
} |
86 |
|
87 |
if (!File.Exists(sFilePath)) |
88 |
bResult = false; |
89 |
|
90 |
|
91 |
if (bResult) |
92 |
{ |
93 |
string sPathNonExtension = Txt_PDFPath.Text.Replace(Path.GetExtension(Txt_PDFPath.Text), ""); |
94 |
PdfDocumentProcessor pdp = new PdfDocumentProcessor(); |
95 |
pdp.LoadDocument(sFilePath); |
96 |
|
97 |
try |
98 |
{ |
99 |
for (int i = 0; i < pdp.Document.Pages.Count; i++) |
100 |
{ |
101 |
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 |
{ |
108 |
dWidth = dWidth * iScale; |
109 |
dHeight = dHeight * iScale; |
110 |
} |
111 |
else |
112 |
{ |
113 |
if (Combo_LengthType.SelectedItem.ToString() == "Width") |
114 |
{ |
115 |
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 |
} |
125 |
} |
126 |
|
127 |
double dMax = Math.Max(dWidth, dHeight); |
128 |
|
129 |
using (Bitmap bitmap = pdp.CreateBitmap(i + 1, (int)dMax)) |
130 |
{ |
131 |
bitmap.Save(sPathNonExtension + "_Page" + (i + 1) + ".png"); |
132 |
} |
133 |
} |
134 |
|
135 |
MessageBox.Show("변환완료"); |
136 |
} |
137 |
catch (Exception ex) |
138 |
{ |
139 |
MessageBox.Show("변환 이미지의 사이즈를 줄여주십시오"); |
140 |
} |
141 |
|
142 |
pdp.CloseDocument(); |
143 |
} |
144 |
else |
145 |
{ |
146 |
MessageBox.Show("PDF 경로와 Scale 또는 Width or Height를 확인해주세요"); |
147 |
} |
148 |
|
149 |
|
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 |
} |
253 |
|
254 |
|
255 |
} |
256 |
} |