프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ 321b1f22

이력 | 보기 | 이력해설 | 다운로드 (9.39 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
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 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
                    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 4601d8de Gyusu
            }
219
            else
220
            {
221
                MessageBox.Show("PDF 경로와 DPI를 확인해주세요");
222
            }
223
224
        }
225
    }
226
}
클립보드 이미지 추가 (최대 크기: 500 MB)