pdf2dsc.ps 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. % Copyright (C) 1994, 1995, 1996, 1997, 1998 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This file is part of AFPL Ghostscript.
  4. %
  5. % AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  6. % distributor accepts any responsibility for the consequences of using it, or
  7. % for whether it serves any particular purpose or works at all, unless he or
  8. % she says so in writing. Refer to the Aladdin Free Public License (the
  9. % "License") for full details.
  10. %
  11. % Every copy of AFPL Ghostscript must include a copy of the License, normally
  12. % in a plain ASCII text file named PUBLIC. The License grants you the right
  13. % to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14. % conditions described in the License. Among other things, the License
  15. % requires that the copyright notice and this notice be preserved on all
  16. % copies.
  17. % $Id: pdf2dsc.ps,v 1.3 2000/12/29 05:03:49 alexcher Exp $
  18. % pdf2dsc.ps
  19. % read pdf file and produce DSC "index" file.
  20. %
  21. % Input file is named PDFname
  22. % Output file is named DSCname
  23. %
  24. % Run using:
  25. % gs -dNODISPLAY -sPDFname=pdffilename -sDSCname=tempfilename pdf2dsc.ps
  26. % Then display the PDF file with
  27. % gs tempfilename
  28. %
  29. % Modified by Geoff Keating <geoffk@ozemail.com.au> 21/12/98:
  30. % Add DocumentMedia, PageMedia comments
  31. % Use inherited BoundingBox and Orientation
  32. % Reformat, add new macro 'puts', generally clean up
  33. % Modified by Johannes Plass <plass@dipmza.physik.uni-mainz.de> 1996-11-05:
  34. % Adds BoundingBox and Orientation if available.
  35. % Modified by rjl/lpd 9/19/96
  36. % Updates for compatibility with modified pdf_*.ps code for handling
  37. % page ranges (i.e., partial files) better.
  38. % Modified by Geoff Keating <Geoff.Keating@anu.edu.au> 7/3/96:
  39. % include Title and CreationDate DSC comments (these are displayed by
  40. % Ghostview);
  41. % reduce the size of typical output files by a factor of about 3.
  42. % Modified by L. Peter Deutsch 3/18/96:
  43. % Removes unnecessary and error-prone code duplicated from pdf_main.ps
  44. % Modified by L. Peter Deutsch for GS 3.33
  45. % Originally by Russell Lang 1995-04-26
  46. /DSCfile DSCname (w) file def
  47. systemdict /.setsafe known { .setsafe } if
  48. /puts { DSCfile exch writestring } bind def
  49. /DSCstring 255 string def
  50. /MediaTypes 10 dict def
  51. % (str1) (str2) concatstr (str1str2)
  52. /concatstr {
  53. 2 copy length exch length add string
  54. dup dup 5 2 roll copy length
  55. % stack: newstring newstring str2 str1-length
  56. exch putinterval
  57. } bind def
  58. GS_PDF_ProcSet begin
  59. pdfdict begin
  60. PDFname (r) file
  61. pdfopen begin
  62. /FirstPage where { pop } { /FirstPage 1 def } ifelse
  63. /LastPage where { pop } { /LastPage pdfpagecount def } ifelse
  64. % scan through for media sizes, keep them in the dictionary
  65. FirstPage 1 LastPage {
  66. pdfgetpage /MediaBox pget pop % MediaBox is a required attribute
  67. aload pop
  68. 3 -1 roll sub 3 1 roll exch sub exch
  69. 2 array astore
  70. aload 3 1 roll 10 string cvs exch 10 string cvs
  71. (x) exch concatstr concatstr cvn
  72. MediaTypes 3 1 roll exch put
  73. } for
  74. % write header and prolog
  75. (%!PS-Adobe-3.0\n) puts
  76. Trailer /Info knownoget
  77. {
  78. dup /Title knownoget
  79. {
  80. (%%Title: ) puts
  81. DSCfile exch write==
  82. }
  83. if
  84. /CreationDate knownoget
  85. {
  86. (%%CreationDate: ) puts
  87. DSCfile exch write==
  88. }
  89. if
  90. }
  91. if
  92. % This is really supposed to be sorted by frequency of usage...
  93. (%%DocumentMedia: )
  94. MediaTypes {
  95. exch pop
  96. 1 index puts
  97. (y) puts dup 1 get DSCstring cvs puts
  98. (x) puts dup 0 get DSCstring cvs puts
  99. ( ) puts dup 0 get DSCstring cvs puts
  100. ( ) puts 1 get DSCstring cvs puts
  101. ( 70 white ()\n) puts
  102. pop (%%+ )
  103. } forall
  104. pop
  105. (%%Pages: ) puts
  106. LastPage FirstPage sub 1 add DSCstring cvs puts
  107. (\n%%EndComments\n) puts
  108. (%%BeginProlog\n) puts
  109. (/Page null def\n/Page# 0 def\n/PDFSave null def\n) puts
  110. (/DSCPageCount 0 def\n) puts
  111. (/DoPDFPage {dup /Page# exch store pdfgetpage pdfshowpage } def\n) puts
  112. (GS_PDF_ProcSet begin\npdfdict begin\n) puts
  113. (%%EndProlog\n) puts
  114. (%%BeginSetup\n) puts
  115. DSCfile PDFname write==only
  116. ( \(r\) file 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