hytos / DTI_PID / PDF_TO_IMAGE / ConvertImage.cs @ 3f871948
이력 | 보기 | 이력해설 | 다운로드 (5.88 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 | |||
59 | using (PDFDoc doc = new PDFDoc(FilePath)) |
||
60 | { |
||
61 | // pdftron.PDFNet.Initialize("daelim.co.kr(Doftech Corp):CPU:2::W:AMC(20120315):EF6E886F25A414FFB5F8C1F2999CF2DA33DC6C5164315BAF7011B87AF0FA"); |
||
62 | Bitmap newBmp_; |
||
63 | ImageCodecInfo DefaultImageCodecInfo = GetEncoderInfo("image/png"); |
||
64 | EncoderParameters DefaultEncoderParameters = new EncoderParameters(2); |
||
65 | System.Drawing.Imaging.Encoder QualityEncoder = System.Drawing.Imaging.Encoder.Quality; |
||
66 | System.Drawing.Imaging.Encoder ColorDepthEncoder = System.Drawing.Imaging.Encoder.ColorDepth; |
||
67 | DefaultEncoderParameters.Param[0] = new EncoderParameter(QualityEncoder, 100L); |
||
68 | DefaultEncoderParameters.Param[1] = new EncoderParameter(ColorDepthEncoder, 8L); |
||
69 | |||
70 | #region 이미지 만들기 |
||
71 | for (int i = 1; i < doc.GetPageCount() + 1; i++) |
||
72 | { |
||
73 | try |
||
74 | { |
||
75 | using (pdftron.PDF.PDFDraw draw = new pdftron.PDF.PDFDraw()) |
||
76 | { |
||
77 | float fDpix = 600; |
||
78 | float fDpiy = 600; |
||
79 | //int iWidth = 10000; |
||
80 | //int iHeight = 10000; |
||
81 | try |
||
82 | {fDpix = float.Parse(Combo_DPI.Text); |
||
83 | fDpiy = float.Parse(Combo_DPI.Text);} |
||
84 | catch |
||
85 | {} |
||
86 | //try |
||
87 | //{iWidth = int.Parse(Properties.Settings.Default._Width); |
||
88 | //iHeight = int.Parse(Properties.Settings.Default._Height); |
||
89 | //}catch{ } |
||
90 | ElementBuilder bld = new ElementBuilder(); |
||
91 | ElementWriter writer = new ElementWriter(); |
||
92 | //var widthData = (int)doc.GetPage(i).GetPageWidth(); |
||
93 | //int heightData = (int)doc.GetPage(i).GetPageHeight(); |
||
94 | var rotation = doc.GetPage(i).GetRotation(); |
||
95 | // draw.SetImageSize((int)(widthData * 2), (int)(heightData * 2), true); |
||
96 | // draw.SetImageSize(iWidth, iHeight, true); |
||
97 | draw.SetAntiAliasing(true); |
||
98 | draw.SetDPI(fDpix); |
||
99 | newBmp_ = draw.GetBitmap(doc.GetPage(i)); |
||
100 | |||
101 | using (MemoryStream _savestream = new MemoryStream()) |
||
102 | { |
||
103 | newBmp_.Save(_savestream, DefaultImageCodecInfo, DefaultEncoderParameters); |
||
104 | newBmp_ = new Bitmap(_savestream); |
||
105 | ObjSet objset = new ObjSet(); |
||
106 | Obj jbig2_hint = objset.CreateName("png"); |
||
107 | string pagePath = sPathNonExtension; |
||
108 | if (i > 1) |
||
109 | { |
||
110 | pagePath = sPathNonExtension + "\\" + i; |
||
111 | } |
||
112 | newBmp_.Save(pagePath + "_" + fDpix +".png"); |
||
113 | newBmp_.Dispose(); |
||
114 | } |
||
115 | GC.Collect(); |
||
116 | GC.WaitForPendingFinalizers(); |
||
117 | } |
||
118 | } |
||
119 | catch (Exception ex) |
||
120 | { |
||
121 | return false; |
||
122 | } |
||
123 | } |
||
124 | #endregion |
||
125 | return true; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | private void Btn_Convert_Click(object sender, EventArgs e) |
||
130 | { |
||
131 | |||
132 | int iDpi = 0; |
||
133 | if(Txt_PDFPath.Text != "" && int.TryParse(Combo_DPI.Text,out iDpi) == true) |
||
134 | { |
||
135 | Properties.Settings.Default._Dpi = Combo_DPI.Text; |
||
136 | Properties.Settings.Default._Height = Txt_Height.Text; |
||
137 | Properties.Settings.Default._Width = Txt_Width.Text; |
||
138 | Properties.Settings.Default.Save(); |
||
139 | ConvertPdfToImage(Txt_PDFPath.Text); |
||
140 | } |
||
141 | else |
||
142 | { |
||
143 | MessageBox.Show("PDF 경로와 DPI를 확인해주세요"); |
||
144 | } |
||
145 | |||
146 | } |
||
147 | } |
||
148 | } |