pdf2dsc.ps 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. % Copyright (C) 1994, 1995, 1996, 1997, 1998 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This software is provided AS-IS with no warranty, either express or
  4. % implied.
  5. %
  6. % This software is distributed under license and may not be copied,
  7. % modified or distributed except as expressly authorized under the terms
  8. % of the license contained in the file LICENSE in this distribution.
  9. %
  10. % For more information about licensing, please refer to
  11. % http://www.ghostscript.com/licensing/. For information on
  12. % commercial licensing, go to http://www.artifex.com/licensing/ or
  13. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  14. % San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  15. % $Id: pdf2dsc.ps,v 1.7 2002/03/30 23:39:15 raph Exp $
  16. % pdf2dsc.ps
  17. % read pdf file and produce DSC "index" file.
  18. %
  19. % Input file is named PDFname
  20. % Output file is named DSCname
  21. %
  22. % Run using:
  23. % gs -dNODISPLAY -sPDFname=pdffilename -sDSCname=tempfilename pdf2dsc.ps
  24. % Then display the PDF file with
  25. % gs tempfilename
  26. %
  27. % Modified by Geoff Keating <geoffk@ozemail.com.au> 21/12/98:
  28. % Add DocumentMedia, PageMedia comments
  29. % Use inherited BoundingBox and Orientation
  30. % Reformat, add new macro 'puts', generally clean up
  31. % Modified by Johannes Plass <plass@dipmza.physik.uni-mainz.de> 1996-11-05:
  32. % Adds BoundingBox and Orientation if available.
  33. % Modified by rjl/lpd 9/19/96
  34. % Updates for compatibility with modified pdf_*.ps code for handling
  35. % page ranges (i.e., partial files) better.
  36. % Modified by Geoff Keating <Geoff.Keating@anu.edu.au> 7/3/96:
  37. % include Title and CreationDate DSC comments (these are displayed by
  38. % Ghostview);
  39. % reduce the size of typical output files by a factor of about 3.
  40. % Modified by L. Peter Deutsch 3/18/96:
  41. % Removes unnecessary and error-prone code duplicated from pdf_main.ps
  42. % Modified by L. Peter Deutsch for GS 3.33
  43. % Originally by Russell Lang 1995-04-26
  44. /PDFfile PDFname (r) file def
  45. /DSCfile DSCname (w) file def
  46. systemdict /.setsafe known { .setsafe } if
  47. /puts { DSCfile exch writestring } bind def
  48. /DSCstring 255 string def
  49. /MediaTypes 10 dict def
  50. % (str1) (str2) concatstr (str1str2)
  51. /concatstr {
  52. 2 copy length exch length add string
  53. dup dup 5 2 roll copy length
  54. % stack: newstring newstring str2 str1-length
  55. exch putinterval
  56. } bind def
  57. GS_PDF_ProcSet begin
  58. pdfdict begin
  59. PDFfile
  60. pdfopen begin
  61. /FirstPage where { pop } { /FirstPage 1 def } ifelse
  62. /LastPage where { pop } { /LastPage pdfpagecount def } ifelse
  63. % scan through for media sizes, keep them in the dictionary
  64. FirstPage 1 LastPage {
  65. pdfgetpage /MediaBox pget pop % MediaBox is a required attribute
  66. aload pop
  67. 3 -1 roll sub 3 1 roll exch sub exch
  68. 2 array astore
  69. aload 3 1 roll 10 string cvs exch 10 string cvs
  70. (x) exch concatstr concatstr cvn
  71. MediaTypes 3 1 roll exch put
  72. } for
  73. % write header and prolog
  74. (%!PS-Adobe-3.0\n) puts
  75. Trailer /Info knownoget
  76. {
  77. dup /Title knownoget
  78. {
  79. (%%Title: ) puts
  80. DSCfile exch write==
  81. }
  82. if
  83. /CreationDate knownoget
  84. {
  85. (%%CreationDate: ) puts
  86. DSCfile exch write==
  87. }
  88. if
  89. }
  90. if
  91. % This is really supposed to be sorted by frequency of usage...
  92. (%%DocumentMedia: )
  93. MediaTypes {
  94. exch pop
  95. 1 index puts
  96. (y) puts dup 1 get DSCstring cvs puts
  97. (x) puts dup 0 get DSCstring cvs puts
  98. ( ) puts dup 0 get DSCstring cvs puts
  99. ( ) puts 1 get DSCstring cvs puts
  100. ( 70 white ()\n) puts
  101. pop (%%+ )
  102. } forall
  103. pop
  104. (%%Pages: ) puts
  105. LastPage FirstPage sub 1 add DSCstring cvs puts
  106. (\n%%EndComments\n) puts
  107. (%%BeginProlog\n) puts
  108. (/Page null def\n/Page# 0 def\n/PDFSave null def\n) puts
  109. (/DSCPageCount 0 def\n) puts
  110. (/DoPDFPage {dup /Page# exch store pdfgetpage pdfshowpage } def\n) puts
  111. (GS_PDF_ProcSet begin\npdfdict begin\n) puts
  112. (%%EndProlog\n) puts
  113. (%%BeginSetup\n) puts
  114. DSCfile PDFname write==only
  115. ( \(r\) file { DELAYSAFER { .setsafe } if } stopped pop\n) puts
  116. ( pdfopen begin\n) puts
  117. (%%EndSetup\n) puts
  118. % process each page
  119. FirstPage 1 LastPage {
  120. (%%Page: ) puts
  121. dup DSCstring cvs puts
  122. ( ) puts
  123. dup DSCstring cvs puts
  124. (\n) puts
  125. dup pdfgetpage
  126. dup /MediaBox pget pop
  127. (%%PageMedia: y) puts
  128. aload pop 3 -1 roll sub DSCstring cvs puts
  129. (x) puts exch sub DSCstring cvs puts
  130. (\n) puts
  131. dup /CropBox pget {
  132. (%%PageBoundingBox: ) puts
  133. {DSCfile exch write=only ( ) puts} forall
  134. (\n) puts
  135. } if
  136. /Rotate pget {
  137. (%%PageOrientation: ) puts
  138. 90 div cvi 4 mod dup 0 lt {4 add} if
  139. [(Portrait) (Landscape) (UpsideDown) (Seascape)] exch get puts
  140. (\n) puts
  141. } if
  142. DSCfile exch DSCstring cvs writestring
  143. ( DoPDFPage\n) puts
  144. } for
  145. currentdict pdfclose
  146. end
  147. end
  148. end
  149. % write trailer
  150. (%%Trailer\n) puts
  151. (currentdict pdfclose\nend\nend\nend\n) puts
  152. (%%EOF\n) puts
  153. % close output file and exit
  154. DSCfile closefile
  155. quit
  156. % end of pdf2dsc.ps