View difference between Paste ID: KZGq7D0R and 5qRYp04Y
SHOW: | | - or go back to the newest paste.
1-
//
1+
2-
// GhostscriptViewerPdfFormatHandler.cs
2+
3-
// This file is part of Ghostscript.NET library
3+
4-
//
4+
5-
// Author: Josip Habjan (habjan@gmail.com, http://www.linkedin.com/in/habjan) 
5+
6-
// Copyright (c) 2013-2014 by Josip Habjan. All rights reserved.
6+
7-
//
7+
8-
// Author ported some parts of this code from GSView. 
8+
9-
//
9+
10-
// Permission is hereby granted, free of charge, to any person obtaining
10+
11-
// a copy of this software and associated documentation files (the
11+
12-
// "Software"), to deal in the Software without restriction, including
12+
13-
// without limitation the rights to use, copy, modify, merge, publish,
13+
14-
// distribute, sublicense, and/or sell copies of the Software, and to
14+
15-
// permit persons to whom the Software is furnished to do so, subject to
15+
16-
// the following conditions:
16+
17-
//
17+
18-
// The above copyright notice and this permission notice shall be
18+
19-
// included in all copies or substantial portions of the Software.
19+
20-
//
20+
21-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
22-
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22+
23-
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
24-
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24+
25-
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25+
26-
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26+
27-
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27+
28
29
        public override void Initialize()
30
        {
31
            // define our routine for preparing to show a page.  
32
            // This writes out some tags which we capture in the 
33
            // callback to obtain the page size and orientation. 
34
            this.Execute(string.Format(@"
35
                /GSNETViewer_PDFpage {{ 
36
        	        ({0}) print dup == flush 
37
        	        pdfgetpage /Page exch store 
38
                    Page /MediaBox pget 
39
                    {{ ({1}) print == flush  }} 
40
        	        if 
41
                    Page /CropBox pget 
42
         	        {{ ({2}) print == flush }} 
43
        	        if 
44
                    Page /Rotate pget not {{ 0 }} if 
45
        	        ({3}) print == flush 
46
                }} def", PDF_PAGE_TAG, PDF_MEDIA_TAG, PDF_CROP_TAG, PDF_ROTATE_TAG));
47
48
            // put these in userdict so we can write to them later
49
            this.Execute(@"
50
                    /Page null def
51
                    /Page# 0 def
52
                    /PDFSave null def
53
                    /DSCPageCount 0 def
54
                ");
55
56
            // open PDF support dictionaries
57
            this.Execute(@"
58
                    GS_PDF_ProcSet begin
59
                    pdfdict begin");
60
        }
61
62
        #endregion
63
64
        #region Open
65
66
        public override void Open(string filePath)
67
        {           
68
            // open PDF file
69
            this.Execute(string.Format("({0}) (r) file pdfopen begin", filePath.Replace("\\", "/")));
70
71
            this.Execute("/FirstPage where { pop FirstPage } { 1 } ifelse");
72
            this.Execute("/LastPage where { pop LastPage } { pdfpagecount } ifelse");
73
74
            // flush stdout and then send PDF page marker to stdout where we capture the page numbers via callback
75
            this.Execute(string.Format("flush ({0}) print exch =only ( ) print =only (\n) print flush", PDF_PAGES_TAG));
76
        }
77
78
        #endregion
79
80
        #region StdInput
81
82
        public override void StdInput(out string input, int count)
83
        {
84
            input = string.Empty;
85
        }
86
87
        #endregion
88
89
        #region StdOutput
90
91
        public override void StdOutput(string message)
92
        {
93
            if (message.Contains(PDF_TAG))
94
            {
95
                int startPos = message.IndexOf(PDF_TAG);
96
                int endPos = message.IndexOf(": ");
97
98
                string tag = message.Substring(startPos, endPos + 2);
99
                string rest = message.Substring(endPos + 2, message.Length - endPos - 2);
100
101
                switch (tag)
102
                {
103
                    case PDF_PAGES_TAG:
104
                        {
105
                            string[] pages = rest.Split(new char[] { ' ' });
106
                            this.FirstPageNumber = int.Parse(pages[0]);
107
                            this.LastPageNumber = int.Parse(pages[1]);
108
                        }
109
                        break;
110
                    case PDF_PAGE_TAG:
111
                        {
112
                            this.CurrentPageNumber = int.Parse(rest);
113
                            break;
114
                        }
115
                    case PDF_MEDIA_TAG:
116
                        {
117
                            string[] mb = rest.Split(new char[] { ' ' });
118
                            this.MediaBox = new GhostscriptRectangle(
119
                                    float.Parse(mb[0].TrimStart('['), System.Globalization.CultureInfo.InvariantCulture), 
120
                                    float.Parse(mb[1], System.Globalization.CultureInfo.InvariantCulture), 
121
                                    float.Parse(mb[2], System.Globalization.CultureInfo.InvariantCulture), 
122
                                    float.Parse(mb[3].TrimEnd(']'), System.Globalization.CultureInfo.InvariantCulture));
123
                            break;
124
                        }
125
                    case PDF_CROP_TAG:
126
                        {
127
                            string[] cb = rest.Split(new char[] { ' ' });
128
                            this.CropBox = new GhostscriptRectangle(
129
                                    float.Parse(cb[0].TrimStart('['), System.Globalization.CultureInfo.InvariantCulture),
130
                                    float.Parse(cb[1], System.Globalization.CultureInfo.InvariantCulture),
131
                                    float.Parse(cb[2], System.Globalization.CultureInfo.InvariantCulture),
132
                                    float.Parse(cb[3].TrimEnd(']'), System.Globalization.CultureInfo.InvariantCulture));
133
134
                            break;
135
                        }
136
                    case PDF_ROTATE_TAG:
137
                        {
138
                            int rotate = int.Parse(rest);
139
140
                            while (rotate < 0)
141
                            {
142
                                rotate += 360;
143
                            }
144
145
                            while (rotate >= 360)
146
                            {
147
                                rotate -= 360;
148
                            }
149
                                
150
                            switch (rotate)
151
                            {
152
                                case 90:
153
                                    this.PageOrientation = GhostscriptPageOrientation.Landscape;
154
                                    break;
155
                                case 180:
156
                                    this.PageOrientation = GhostscriptPageOrientation.UpsideDown;
157
                                    break;
158
                                case 270:
159
                                    this.PageOrientation = GhostscriptPageOrientation.Seascape;
160
                                    break;
161
                                default:
162
                                    this.PageOrientation = GhostscriptPageOrientation.Portrait;
163
                                    break;
164
                            }
165
166
                            break;
167
                        }
168
                }
169
            }
170
        }
171
172
        #endregion
173
174
        #region StdError
175
176
        public override void StdError(string message)
177
        {
178
179
        }
180
181
        #endregion
182
183
        #region InitPage
184
185
        public override void InitPage(int pageNumber)
186
        {
187
            if (pageNumber >= this.FirstPageNumber && pageNumber <= this.LastPageNumber)
188
            {
189
                this.Execute(string.Format("{0} GSNETViewer_PDFpage", pageNumber));
190
            }
191
            else
192
            {
193
                throw new GhostscriptException("Page number is not in pages number range!");
194
            }
195
        }
196
197
        #endregion
198
199
        #region ShowPage
200
201
        public override void ShowPage(int pageNumber)
202
        {
203
            if (pageNumber >= this.FirstPageNumber && pageNumber <= this.LastPageNumber)
204
            {
205
                this.Execute("Page pdfshowpage_init pdfshowpage_finish");
206
            }
207
            else
208
            {
209
                throw new GhostscriptException("Page number is not in pages number range!");
210
            }
211
        }
212
213
        #endregion
214
215
    }
216
}