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
|
Bitmap newBmp_;
|
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
|
//int iWidth = 10000;
|
79
|
//int iHeight = 10000;
|
80
|
try
|
81
|
{fDpix = float.Parse(Combo_DPI.Text);
|
82
|
fDpiy = float.Parse(Combo_DPI.Text);}
|
83
|
catch
|
84
|
{}
|
85
|
//try
|
86
|
//{iWidth = int.Parse(Properties.Settings.Default._Width);
|
87
|
//iHeight = int.Parse(Properties.Settings.Default._Height);
|
88
|
//}catch{ }
|
89
|
ElementBuilder bld = new ElementBuilder();
|
90
|
ElementWriter writer = new ElementWriter();
|
91
|
//var widthData = (int)doc.GetPage(i).GetPageWidth();
|
92
|
//int heightData = (int)doc.GetPage(i).GetPageHeight();
|
93
|
var rotation = doc.GetPage(i).GetRotation();
|
94
|
// draw.SetImageSize((int)(widthData * 2), (int)(heightData * 2), true);
|
95
|
// draw.SetImageSize(iWidth, iHeight, true);
|
96
|
draw.SetAntiAliasing(true);
|
97
|
draw.SetDPI(fDpix);
|
98
|
newBmp_ = draw.GetBitmap(doc.GetPage(i));
|
99
|
|
100
|
using (MemoryStream _savestream = new MemoryStream())
|
101
|
{
|
102
|
newBmp_.Save(_savestream, DefaultImageCodecInfo, DefaultEncoderParameters);
|
103
|
newBmp_ = new Bitmap(_savestream);
|
104
|
ObjSet objset = new ObjSet();
|
105
|
Obj jbig2_hint = objset.CreateName("png");
|
106
|
string pagePath = sPathNonExtension;
|
107
|
if (i > 1)
|
108
|
{
|
109
|
pagePath = sPathNonExtension + "_" + i;
|
110
|
}
|
111
|
newBmp_.Save(pagePath + "_" + fDpix +".png");
|
112
|
newBmp_.Dispose();
|
113
|
}
|
114
|
GC.Collect();
|
115
|
GC.WaitForPendingFinalizers();
|
116
|
}
|
117
|
}
|
118
|
catch (Exception ex)
|
119
|
{
|
120
|
return false;
|
121
|
}
|
122
|
}
|
123
|
if(doc.GetPageCount() > 0)
|
124
|
{
|
125
|
MessageBox.Show("변환완료");
|
126
|
}
|
127
|
|
128
|
#endregion
|
129
|
return true;
|
130
|
}
|
131
|
}
|
132
|
|
133
|
private void Btn_Convert_Click(object sender, EventArgs e)
|
134
|
{
|
135
|
|
136
|
int iDpi = 0;
|
137
|
if(Txt_PDFPath.Text != "" && int.TryParse(Combo_DPI.Text,out iDpi) == true)
|
138
|
{
|
139
|
Properties.Settings.Default._Dpi = Combo_DPI.Text;
|
140
|
Properties.Settings.Default._Height = Txt_Height.Text;
|
141
|
Properties.Settings.Default._Width = Txt_Width.Text;
|
142
|
Properties.Settings.Default.Save();
|
143
|
ConvertPdfToImage(Txt_PDFPath.Text);
|
144
|
}
|
145
|
else
|
146
|
{
|
147
|
MessageBox.Show("PDF 경로와 DPI를 확인해주세요");
|
148
|
}
|
149
|
|
150
|
}
|
151
|
}
|
152
|
}
|