hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ 321b1f22
이력 | 보기 | 이력해설 | 다운로드 (9.39 KB)
1 |
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 |
pdftron.PDFNet.Initialize("daelim.co.kr(Doftech Corp):CPU:2::W:AMC(20120315):EF6E886F25A414FFB5F8C1F2999CF2DA33DC6C5164315BAF7011B87AF0FA"); |
59 |
using (PDFDoc doc = new PDFDoc(FilePath)) |
60 |
{ |
61 |
|
62 |
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 |
draw.SetAntiAliasing(false); |
87 |
draw.SetDPI(fDpix); |
88 |
Bitmap newBmp_ = draw.GetBitmap(doc.GetPage(i)); |
89 |
|
90 |
using (MemoryStream _savestream = new MemoryStream()) |
91 |
{ |
92 |
try |
93 |
{ |
94 |
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 |
} |
106 |
catch(Exception ex) |
107 |
{ |
108 |
|
109 |
} |
110 |
|
111 |
} |
112 |
GC.Collect(); |
113 |
GC.WaitForPendingFinalizers(); |
114 |
} |
115 |
} |
116 |
catch (Exception ex) |
117 |
{ |
118 |
// return false; |
119 |
} |
120 |
} |
121 |
if(doc.GetPageCount() > 0) |
122 |
{ |
123 |
MessageBox.Show("변환완료"); |
124 |
} |
125 |
#endregion |
126 |
return true; |
127 |
} |
128 |
} |
129 |
|
130 |
private void Btn_Convert_Click(object sender, EventArgs e) |
131 |
{ |
132 |
|
133 |
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 |
/// 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 |
DefaultEncoderParameters.Param[1] = new EncoderParameter(ColorDepthEncoder, 8L); |
155 |
|
156 |
#region 이미지 만들기 |
157 |
for (int i = 1; i < doc.GetPageCount() + 1; i++) |
158 |
{ |
159 |
try |
160 |
{ |
161 |
using (pdftron.PDF.PDFDraw draw = new pdftron.PDF.PDFDraw()) |
162 |
{ |
163 |
float fDpix = 600; |
164 |
float fDpiy = 600; |
165 |
try |
166 |
{ |
167 |
fDpix = float.Parse(Combo_DPI.Text); |
168 |
fDpiy = float.Parse(Combo_DPI.Text); |
169 |
} |
170 |
catch |
171 |
{ } |
172 |
ElementBuilder bld = new ElementBuilder(); |
173 |
ElementWriter writer = new ElementWriter(); |
174 |
var rotation = doc.GetPage(i).GetRotation(); |
175 |
draw.SetAntiAliasing(false); |
176 |
draw.SetDPI(fDpix); |
177 |
Bitmap newBmp_ = draw.GetBitmap(doc.GetPage(i)); |
178 |
|
179 |
using (MemoryStream _savestream = new MemoryStream()) |
180 |
{ |
181 |
try |
182 |
{ |
183 |
newBmp_.Save(_savestream, DefaultImageCodecInfo, DefaultEncoderParameters); |
184 |
newBmp_ = new Bitmap(_savestream); |
185 |
ObjSet objset = new ObjSet(); |
186 |
Obj jbig2_hint = objset.CreateName("png"); |
187 |
string pagePath = sPathNonExtension; |
188 |
if (i > 1) |
189 |
{ |
190 |
pagePath = sPathNonExtension + "_" + i; |
191 |
} |
192 |
newBmp_.Save(pagePath + "_" + fDpix + ".png"); |
193 |
newBmp_.Dispose(); |
194 |
} |
195 |
catch (Exception ex) |
196 |
{ |
197 |
|
198 |
} |
199 |
|
200 |
} |
201 |
GC.Collect(); |
202 |
GC.WaitForPendingFinalizers(); |
203 |
} |
204 |
} |
205 |
catch (Exception ex) |
206 |
{ |
207 |
// return false; |
208 |
} |
209 |
} |
210 |
if (doc.GetPageCount() > 0) |
211 |
{ |
212 |
MessageBox.Show("변환완료"); |
213 |
} |
214 |
#endregion |
215 |
} |
216 |
|
217 |
|
218 |
} |
219 |
else |
220 |
{ |
221 |
MessageBox.Show("PDF 경로와 DPI를 확인해주세요"); |
222 |
} |
223 |
|
224 |
} |
225 |
} |
226 |
} |