views:

62

answers:

3

I have some graphs in postscript format, generated by gnuplot. I need to place some identifying information on the graph. How can I script some instructions to do that? I want to write a number at the top right corner of the graph (a .ps file).

+1  A: 

If you can affect the gnuplot code, you can add the text with gnuplot. See this for example. The command to use is set label.

Peter Jaric
+1  A: 

If you don't have access to the Gnuplot source file, here is a generic method that works (which you can script). It uses Ghostscript and PDFTK.

  1. Create a new PDF file that contains the content you want to overlay to your .ps graphs.
  2. Convert your Gnuplot-originating .ps graphs to PDF using Ghostscript.
  3. Use pdftk (on Unix) or pdftk.exe (on Windows) to overlay the two PDFs.
  4. Use Ghostscript to convert the PDF back to PS (if you need that), or just continue using the PDF.

This SuperUser answer contains many details which will help you along the path outlined above.


With the help of a text editor you may be able to insert PostScript code that render the additional information into your .ps files manually.

However, I cannot give you a working PS code snippet without seeing the graph's PS code first.


Ghostscript you may be able to create new PS files from your graphs using a single commandline.

Try it with s.th. similar to gs -sDEVICE=ps2write -o output-with-comments-gnuplot.ps -c "570 800 moveto /Helvetica findfont 32 scalefont setfont (Number: 42) show" -f /path/to/gnuplot.ps".

However, I cannot really tell if this PS code snippet would really work without seeing the graph's PS code first.

pipitas
I did go through those answers on SuperUser but was looking for a more direct approach, which you say could be done if one has access to the graph ps source. Sorry for the delay, so here it is : http://terminal9.org/.files/gnp-544.ps
sauparna
You could also go through my various answers on PDF [http://stackoverflow.com/search?q=user:359307+%5bpdf%5d] or on Ghostscript [http://stackoverflow.com/search?q=user:359307+%5bghostscript%5d]. Please upvote if you like them -- I have a running bet with a colleague who thought it was impossible to achieve a 1000 score within 4 months, and I'm really close now ;-)
pipitas
+2  A: 

Ok, the example file you linked to is well behaving (and has not re-defined the showpage operator).

So I'm now assuming the following:

  1. All your .ps files are similar to your example file.
  2. All your .ps files are 1 page only (like .eps files).
  3. All your filenames are like constructed as gnp-NNN.ps (like gnp-544.ps is).
  4. The number you want to appear in the top right corner is NNN from the filename.

I also assume you have Ghostscript installed, and it is the most recent version, 8.71. I'm currently on Windows -- if you're on Linux/Unix, just replace gswin32c.exe by gs and all line endings ^ by \.

Test PDF Output

Now try this command first:

gswin32c.exe ^
 -o gnp-with-number-544.pdf ^
 -sDEVICE=pdfwrite ^
 -g5030x5320 ^
 -c "/Helvetica-Italic findfont 15 scalefont setfont 453 482 moveto (544) show" ^
 -f gnp-544.ps

and see if the resulting gnp-with-number-544.pdf looks like you want it.

The -c "..." is used to pass PostScript snippets to Ghostscript and make it process these together with the main .ps file which has then to follow as next parameter, with -f ....

You can modify parameters:

  • Replace the /Helvetica-Italic fontname by /Helvetica, /Courier, /Times, Helvetica-Bold or whatever you've available and prefer.
  • Or modify the fontsize: 15 here means 15 points (in PDF 72 points == 1 inch).
  • Or change the position: 453 482 moves the PostScript currentpoint to "453 points to the left, 482 points to the top" (with the oringin 0,0 set to the bottom left corner).
  • Or tweak the media size: -g5030x5320 gives you 503x532 points (due to the default resolution of 720 dpi used by -sDEVICE=pdfwrite.
  • Or set the string being printed to (File No. 544) show or whatever you want.

You could also add quite a few parameters to tweak the quality of the output file (resolution, font embedding etc.), but these ones will do for now.

Change to PostScript Output

Should you need PostScript output for some reason instead of PDF, change the command like this:

gswin32c.exe ^
 -o gnp-with-number-544.ps ^
 -sDEVICE=ps2write ^
 -g5030x5320 ^
 -c "/Helvetica-Italic findfont 15 scalefont setfont 453 482 moveto (544) show" ^
 -f gnp-544.ps

Batch-convert Lots of Files

Now, how to batch-convert this? For this last step, I'm assuming:

  1. your NNN numbering scheme is not using leading 0s.
  2. your range of files to be converted is gnp-1.ps..gnp-1000.ps.
  3. you want PDF output with the numbers added, not PS.

On Windows, create an addnumbers-make-pdf.bat file with this content:

gswin32c.exe ^
-o gnp-with-number-%1.pdf ^
-sDEVICE=pdfwrite ^
-g5030x5320 ^
-c "/Helvetica-Italic findfont 15 scalefont setfont 453 482 moveto (%1) show" ^
-f gnp-%1.ps

Now run this command in a cmd.exe console:

for /l %i in (1,1,100) do (addnumbers-make-pdfvim.bat %i)

On Linux, create an addnumbers-make-pdf.sh Bash shell script with this content:

#!/bin/bash
gs \
-o gnp-with-number-${1}.pdf \
-sDEVICE=pdfwrite \
-g5030x5320 \
-c "/Helvetica-Italic findfont 15 scalefont setfont 453 482 moveto (${1}) show" \
-f gnp-${1}.ps

Now run this command in a shell:

for i in $(seq 1 1000); do addnumbers-make-pdf.sh ${i} ; done

Update:
Hah!, it even works ;-) I just tested it on Windows. Here is a screenshot with the original and the overlayed file (as PDFs):
alt text

pipitas