Advertisement
Guest User

Untitled

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