forked from SergiyStoyan/PdfDocumentParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPdf.cs
188 lines (175 loc) · 8.71 KB
/
Pdf.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
//********************************************************************************************
//Author: Sergiy Stoyan
// s.y.stoyan@gmail.com, sergiy.stoyan@outlook.com, stoyan@cliversoft.com
// http://www.cliversoft.com
//********************************************************************************************
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System.Diagnostics;
namespace Cliver.PdfDocumentParser
{
/// <summary>
/// PDF routines
/// </summary>
public static partial class Pdf
{
public static System.Drawing.Size GetPageSize(PdfReader pdfReader, int pageI)
{
iTextSharp.text.Rectangle r = pdfReader.GetPageSize(pageI);
return new System.Drawing.Size((int)r.Width, (int)r.Height);
}
static public System.Drawing.Bitmap RenderBitmap(string pdfFile, int pageI, int resolution, bool byFile = false)
{
using (Process p = new Process())
{
p.StartInfo.FileName = Log.AppDir + "\\gswin32c.exe";
int dDownScaleFactor = 1;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
if (!byFile)
{
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.Arguments = "-dNOPROMPT -r" + resolution + " -dDownScaleFactor=" + dDownScaleFactor + " -dBATCH -dFirstPage=" + pageI + " -dLastPage=" + pageI + " -sDEVICE=png16m -dNOPAUSE -sOutputFile=%stdout -q \"" + pdfFile + "\"";
p.Start();
using (MemoryStream ms = new MemoryStream())
{
p.StandardOutput.BaseStream.CopyTo(ms);
p.WaitForExit();
try
{
return new System.Drawing.Bitmap(ms);
}
catch (Exception e)
{
return RenderBitmap(pdfFile, pageI, resolution, true);
}
}
}
else//some pdf's require this because gs puts errors to stdout
{
string bufferFileDir = System.IO.Path.GetTempPath() + Log.ProgramName;
Directory.CreateDirectory(bufferFileDir);
string bufferFile = bufferFileDir + "\\buffer.png";
p.StartInfo.Arguments = "-dNOPROMPT -r" + resolution + " -dDownScaleFactor=" + dDownScaleFactor + " -dBATCH -dFirstPage=" + pageI + " -dLastPage=" + pageI + " -sDEVICE=png16m -dNOPAUSE -sOutputFile=\"" + bufferFile + "\" -q \"" + pdfFile + "\"";
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
string stderr = p.StandardError.ReadToEnd();
if (!string.IsNullOrWhiteSpace(stderr))
Log.Error(stderr);
if (!File.Exists(bufferFile))
throw new Exception("Could not create bufferFile " + bufferFile);
System.Drawing.Bitmap b;// = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(bufferFile);
using (var bt = new System.Drawing.Bitmap(bufferFile))//to free the file
{
b = new System.Drawing.Bitmap(bt);//!!!sets 96dpi
b.SetResolution(bt.HorizontalResolution, bt.VerticalResolution);
}
Directory.Delete(bufferFileDir, true);
return b;
}
}
}
static public List<CharBox> GetCharBoxsFromPage(PdfReader pdfReader, int pageI, bool removeDuplicates)
{
Rectangle r = pdfReader.GetPageSize(pageI);
CharBoxExtractionStrategy s = new CharBoxExtractionStrategy(new System.Drawing.RectangleF(r.Left, r.Bottom, r.Width, r.Height));
PdfTextExtractor.GetTextFromPage(pdfReader, pageI, s);
if (removeDuplicates)
s.CharBoxs = RemoveDuplicates(s.CharBoxs);
return s.CharBoxs;
}
public class CharBoxExtractionStrategy : LocationTextExtractionStrategy
{
public CharBoxExtractionStrategy(System.Drawing.RectangleF pageSize) : base()
{
this.pageSize = pageSize;
}
System.Drawing.RectangleF pageSize;
public List<CharBox> CharBoxs = new List<CharBox>();
public override void RenderText(TextRenderInfo renderInfo)
{
base.RenderText(renderInfo);
//GraphicsState gs = (GraphicsState)gsField.GetValue(renderInfo);//expensive???
//Font font = new Font { Name = string.Join(", ", gs.Font.FullFontName[0]), Size = gs.FontSize };
List<CharBox> cbs = new List<CharBox>();
IList<TextRenderInfo> cris = renderInfo.GetCharacterRenderInfos();
foreach (TextRenderInfo cri in cris)
{
Vector baseLeft = cri.GetBaseline().GetStartPoint();
Vector topRight = cri.GetAscentLine().GetEndPoint();
float x = baseLeft[Vector.I1];
float y = topRight[Vector.I2];
CharBox cb = new CharBox
{
Char = cri.GetText(),
R = new System.Drawing.RectangleF
{
X = x - pageSize.X,
Y = pageSize.Height + pageSize.Y - y,//(!)basic positioning point is char's baseLine, not ascentLine
Width = topRight[Vector.I1] - x,
Height = y - baseLeft[Vector.I2],
},
//Font = font
};
cbs.Add(cb);
}
CharBoxs.AddRange(cbs);
}
static System.Reflection.FieldInfo gsField = typeof(TextRenderInfo).GetField("gs", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
}
public static List<string> GetTextLinesSurroundedByRectangle(IEnumerable<CharBox> cbs, System.Drawing.RectangleF r, TextAutoInsertSpace textAutoInsertSpace)
{
cbs = GetCharBoxsSurroundedByRectangle(cbs, r);
return Page.GetLines(cbs, textAutoInsertSpace, null).Select(a => a.GetString()).ToList();
}
public static List<CharBox> GetCharBoxsSurroundedByRectangle(IEnumerable<CharBox> cbs, System.Drawing.RectangleF r, bool excludeInvisibleCharacters = false)
{
//cbs = RemoveDuplicates(cbs.Where(a => (r.Contains(a.R) /*|| d.IntersectsWith(a.R)*/)));
cbs = cbs.Where(a => (r.Contains(a.R) /*|| d.IntersectsWith(a.R)*/));
if (excludeInvisibleCharacters)
return cbs.Where(a => !InvisibleCharacters.Contains(a.Char)).ToList();
return cbs.ToList();
}
public static List<CharBox> RemoveDuplicates(IEnumerable<CharBox> cbs)
{
List<CharBox> bs = cbs.Where(a => a.R.Width >= 0 && a.R.Height >= 0).ToList();//some symbols are duplicated with negative width and height
for (int i = 0; i < bs.Count; i++)
for (int j = bs.Count - 1; j > i; j--)
{
if (Math.Abs(bs[i].R.X - bs[j].R.X) > Settings.Constants.CoordinateDeviationMargin)//some symbols are duplicated in [almost] same position
continue;
if (Math.Abs(bs[i].R.Y - bs[j].R.Y) > Settings.Constants.CoordinateDeviationMargin)//some symbols are duplicated in [almost] same position
continue;
if (bs[i].Char != bs[j].Char)
continue;
bs.RemoveAt(j);
}
return bs;
}
public static IEnumerable<CharBox> RemoveInvisibles(IEnumerable<CharBox> cbs)
{
return cbs.Where(x => !InvisibleCharacters.Contains(x.Char));
}
public static readonly string InvisibleCharacters = " \t";
public class CharBox : Page.CharBox
{
//public bool AutoInserted = false;
public Font Font;
}
public class Font
{
public string Name;
public float Size;
}
}
}