-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPDFHelper.cs
211 lines (192 loc) · 6.32 KB
/
PDFHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Created by SharpDevelop.
* User: nk1449
* Date: 2017/07/06
* Time: 13:01
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Drawing;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System.IO;
using System.Windows.Forms;
namespace UyghurOCR
{
/// <summary>
/// Description of PDFHelper.
/// </summary>
public class PDFHelper : IRenderListener
{
int _imgNo = 1;
string _outputFolder;
int _currentPage=1;
string _imgname;
int _pageRotation;
public PDFHelper()
{
}
public bool ExtractImages(string pdfPath, string outputFolder, ProgressBar prBar)
{
_outputFolder = outputFolder;
_imgname=System.IO.Path.GetFileNameWithoutExtension(pdfPath);
try{
using (PdfReader pdfReader = new PdfReader(pdfPath))
{
if (pdfReader.IsEncrypted())
throw new ApplicationException( pdfPath + " is encrypted.");
PdfReaderContentParser pdfParser = new PdfReaderContentParser(pdfReader);
prBar.Maximum=pdfReader.NumberOfPages;
prBar.Minimum=1;
prBar.Visible=true;
while (_currentPage <= pdfReader.NumberOfPages)
{
_imgNo = 1;
prBar.Value=_currentPage;
Application.DoEvents();
_pageRotation=pdfReader.GetPageRotation(_currentPage);
pdfParser.ProcessContent(_currentPage, this);
_currentPage++;
}
}
prBar.Visible=false;
return true;
}catch(Exception ee){
throw new Exception("PDF ni resimge aylandurushtiki xataliq", ee);
}
}
public void BeginTextBlock() { }
public void EndTextBlock() { }
public void RenderText(TextRenderInfo renderInfo) { }
public void RenderImage(ImageRenderInfo renderInfo)
{
PdfImageObject pdfimage = renderInfo.GetImage();
string pp= pdfimage.GetImageBytesType().FileExtension;
string imgtp=pdfimage.GetFileType();
if("jpg".Equals(imgtp,StringComparison.OrdinalIgnoreCase)||
"png".Equals(imgtp,StringComparison.OrdinalIgnoreCase)||
"gif".Equals(imgtp,StringComparison.OrdinalIgnoreCase)){
}
else{
imgtp=pp;
imgtp="png";
}
String imageFileName = String.Format("{0}_{1:000}_{2}.{3}", _imgname , _currentPage, _imgNo, imgtp);
// imageFileName= _outputFolder+"\\"+_imgname+_currentPage.ToString("_000")+"_"+_imgNo+".png";
imageFileName= _outputFolder+"\\"+imageFileName;
try{
using (Image dotnetImg = pdfimage.GetDrawingImage())
{
if (dotnetImg != null)
{
if(_pageRotation==270){
dotnetImg.RotateFlip(RotateFlipType.Rotate270FlipNone);
}
else if(_pageRotation==90){
dotnetImg.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
else if(_pageRotation==180){
dotnetImg.RotateFlip(RotateFlipType.Rotate180FlipNone);
}
dotnetImg.Save(imageFileName);
}
}
}catch(Exception ee){
System.Diagnostics.Debug.WriteLine(ee.StackTrace);
}
_imgNo++;
}
public void RenderImageOrg(ImageRenderInfo renderInfo)
{
PdfImageObject pdfimage = renderInfo.GetImage();
string imgtp=pdfimage.GetFileType();
String imageFileName = String.Format("{0}_{1:000}_{2}.{3}", _imgname , _currentPage, _imgNo, imgtp);
imageFileName= _outputFolder+"\\"+imageFileName;
var imageRawBytes = pdfimage.GetImageAsBytes();
File.WriteAllBytes(imageFileName, imageRawBytes);
_imgNo++;
}
private bool ExtractImageOLD(String pdfFile, string imgPath, ProgressBar progressBar1)
{
bool ret=true;
int imgNo=1;
// string pdfFile=@"F:\gheyret\kitablar\Matitey we anarqiz pajiesi.pdf";
// string imgPath=@"F:\gheyret\kitab_suret\Hazirqi Zaman Uyghur Tili Gramatikisi\";
string imgname=System.IO.Path.GetFileNameWithoutExtension(pdfFile);
progressBar1.Visible=true;
progressBar1.Minimum=1;
try{
PdfDictionary pg,tg;
PdfDictionary res;
PdfDictionary xobj;
PdfImageObject pdfimage;
PdfObject lpdfobjW;
PdfObject lpdfobjH;
ImageRenderInfo imgRI;
string width,height;
int pageRotation;
PdfReader pdfReader = new PdfReader(pdfFile);
progressBar1.Maximum=pdfReader.NumberOfPages;
for (int pageNumber = 1; pageNumber <= pdfReader.NumberOfPages; pageNumber++)
{
// if(gStop) break;
// pg = pdfReader.GetPageN(pageNumber);
pg = pdfReader.GetPageNRelease(pageNumber);
res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
imgNo=1;
pageRotation=pdfReader.GetPageRotation(pageNumber);
foreach (PdfName name in xobj.Keys)
{
PdfObject obj = xobj.Get(name);
if (obj.IsIndirect())
{
tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
lpdfobjW=tg.Get(PdfName.WIDTH);
lpdfobjH=tg.Get(PdfName.HEIGHT);
if(lpdfobjW!=null && lpdfobjH!=null){
width = lpdfobjW.ToString();
height = lpdfobjH.ToString();
// imgRI = ImageRenderInfo.CreateForXObject(new Matrix(float.Parse(width), float.Parse(height)), (PRIndirectReference)obj, tg);
imgRI = ImageRenderInfo.CreateForXObject(new GraphicsState(), (PRIndirectReference)obj, tg);
pdfimage = imgRI.GetImage();
ExtractImageOLD(pdfimage,imgPath+"\\"+imgname+pageNumber.ToString("_000")+"_"+imgNo+".png",pageRotation);
imgNo++;
}
}
}
progressBar1.Value=pageNumber;
Application.DoEvents();
}
pdfReader.Close();
}
catch(Exception ee){
// MessageBox.Show(ee.Message,"Xataliq koruldi");
System.Diagnostics.Debug.WriteLine(ee.StackTrace);
ret=false;
}
progressBar1.Visible=false;
return ret;
}
private void ExtractImageOLD(PdfImageObject pdfimage,string path, int pageRotation)
{
using (Image dotnetImg = pdfimage.GetDrawingImage())
{
if (dotnetImg != null)
{
if(pageRotation==270){
dotnetImg.RotateFlip(RotateFlipType.Rotate270FlipNone);
}
else if(pageRotation==90){
dotnetImg.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
else if(pageRotation==180){
dotnetImg.RotateFlip(RotateFlipType.Rotate180FlipNone);
}
dotnetImg.Save(path);
}
}
}
}
}