-
Notifications
You must be signed in to change notification settings - Fork 150
/
GhostscriptViewerPdfFormatHandler.cs
285 lines (231 loc) · 10.7 KB
/
GhostscriptViewerPdfFormatHandler.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//
// GhostscriptViewerPdfFormatHandler.cs
// This file is part of Ghostscript.NET library
//
// Author: Josip Habjan (habjan@gmail.com, http://www.linkedin.com/in/habjan)
// Copyright (c) 2013-2016 by Josip Habjan. All rights reserved.
//
// Author ported some parts of this code from GSView.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Drawing;
using System.Globalization;
namespace Ghostscript.NET.Viewer
{
internal class GhostscriptViewerPdfFormatHandler : GhostscriptViewerFormatHandler
{
#region Private constants
private const string PDF_TAG = "%GSNET";
private const string PDF_PAGES_TAG = "%GSNET_VIEWER_PDF_PAGES: ";
private const string PDF_PAGE_TAG = "%GSNET_VIEWER_PDF_PAGE: ";
private const string PDF_MEDIA_TAG = "%GSNET_VIEWER_PDF_MEDIA: ";
private const string PDF_CROP_TAG = "%GSNET_VIEWER_PDF_CROP: ";
private const string PDF_ROTATE_TAG = "%GSNET_VIEWER_PDF_ROTATE: ";
private const string PDF_DONE_TAG = "%GSNET_VIEWER_PDF_DONE: ";
private const string PDF_MARK_TAG = "%GSNET_VIEWER_PDF_MARK: ";
#endregion
#region Constructor
public GhostscriptViewerPdfFormatHandler(GhostscriptViewer viewer) : base(viewer) { }
#endregion
#region Initialize
public override void Initialize()
{
// define our routine for preparing to show a page.
// This writes out some tags which we capture in the
// callback to obtain the page size and orientation.
this.Execute(string.Format(@"
/GSNETViewer_PDFpage {{
({0}) print dup == flush
pdfgetpage /Page exch store
Page /MediaBox pget
{{ ({1}) print == flush }}
if
Page /CropBox pget
{{ ({2}) print == flush }}
if
Page /Rotate pget not {{ 0 }} if
({3}) print == flush
}} def", PDF_PAGE_TAG, PDF_MEDIA_TAG, PDF_CROP_TAG, PDF_ROTATE_TAG));
// put these in userdict so we can write to them later
this.Execute(@"
/Page null def
/Page# 0 def
/PDFSave null def
/DSCPageCount 0 def
");
// open PDF support dictionaries
this.Execute(@"
GS_PDF_ProcSet begin
pdfdict begin");
}
#endregion
#region Open
public override void Open(string filePath)
{
filePath = StringHelper.ToUtf8String(filePath);
// open PDF file
int res = this.Execute(string.Format("({0}) (r) file runpdfbegin", filePath.Replace("\\", "/")));
if(res == ierrors.e_ioerror)
{
throw new GhostscriptException("IO error for file: '" + filePath + "'", ierrors.e_ioerror);
}
this.Execute("/FirstPage where { pop FirstPage } { 1 } ifelse");
this.Execute("/LastPage where { pop LastPage } { pdfpagecount } ifelse");
// flush stdout and then send PDF page marker to stdout where we capture the page numbers via callback
this.Execute(string.Format("flush ({0}) print exch =only ( ) print =only (\n) print flush", PDF_PAGES_TAG));
// fixes problem with the invisible layers
// if we don't run that code, then optional content groups will be left unmarked and always processed
this.Execute("process_trailer_attrs\n");
}
#endregion
#region StdInput
public override void StdInput(out string input, int count)
{
input = string.Empty;
}
#endregion
#region StdOutput
public override void StdOutput(string message)
{
if (message.Contains(PDF_TAG))
{
int startPos = message.IndexOf(PDF_TAG);
int endPos = message.IndexOf(": ");
string tag = message.Substring(startPos, endPos - startPos + 2);
string rest = message.Substring(endPos + 2, message.Length - endPos - 2);
switch (tag)
{
case PDF_PAGES_TAG:
{
string[] pages = rest.Split(new char[] { ' ' });
int first, last;
if (pages.Length >= 2 && int.TryParse(pages[0], out first) && int.TryParse(pages[1], out last))
{
this.FirstPageNumber = first;
this.LastPageNumber = last;
}
break;
}
case PDF_PAGE_TAG:
{
int number;
if (int.TryParse(rest, out number))
{
this.CurrentPageNumber = number;
}
break;
}
case PDF_MEDIA_TAG:
{
string[] mb = rest.Split(new char[] { ' ' });
float llx, lly, urx, ury;
if (mb.Length >= 4 &&
float.TryParse(mb[0].TrimStart('['), NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out llx) &&
float.TryParse(mb[1], NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out lly) &&
float.TryParse(mb[2], NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out urx) &&
float.TryParse(mb[3].TrimEnd(']'), NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out ury))
{
this.MediaBox = new GhostscriptRectangle(llx, lly, urx, ury);
}
break;
}
case PDF_CROP_TAG:
{
string[] cb = rest.Split(new char[] { ' ' });
float llx, lly, urx, ury;
if (cb.Length >= 4 &&
float.TryParse(cb[0].TrimStart('['), NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out llx) &&
float.TryParse(cb[1], NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out lly) &&
float.TryParse(cb[2], NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out urx) &&
float.TryParse(cb[3].TrimEnd(']'), NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out ury))
{
this.CropBox = new GhostscriptRectangle(llx, lly, urx, ury);
}
break;
}
case PDF_ROTATE_TAG:
{
int rotate;
if (int.TryParse(rest, out rotate))
{
while (rotate < 0)
{
rotate += 360;
}
while (rotate >= 360)
{
rotate -= 360;
}
switch (rotate)
{
case 90:
this.PageOrientation = GhostscriptPageOrientation.Landscape;
break;
case 180:
this.PageOrientation = GhostscriptPageOrientation.UpsideDown;
break;
case 270:
this.PageOrientation = GhostscriptPageOrientation.Seascape;
break;
default:
this.PageOrientation = GhostscriptPageOrientation.Portrait;
break;
}
}
break;
}
}
}
}
#endregion
#region StdError
public override void StdError(string message)
{
}
#endregion
#region InitPage
public override void InitPage(int pageNumber)
{
if (pageNumber >= this.FirstPageNumber && pageNumber <= this.LastPageNumber)
{
this.Execute(string.Format("{0} GSNETViewer_PDFpage", pageNumber));
}
else
{
throw new GhostscriptException("Page number is not in pages number range!");
}
}
#endregion
#region ShowPage
public override void ShowPage(int pageNumber)
{
if (pageNumber >= this.FirstPageNumber && pageNumber <= this.LastPageNumber)
{
this.Execute("Page pdfshowpage_init pdfshowpage_finish");
}
else
{
throw new GhostscriptException("Page number is not in pages number range!");
}
}
#endregion
}
}