Roman
Roman

Reputation: 967

GhostScript/PostScript/PDF - Cut a rectangle from a PS file, rotate it 45 degrees, and paste into another file - how?

I need to automate the following process:

  1. Cut part of a page in a PostScript input file (for example: input.ps, cut everything from 10,10 to 150, 200).

  2. Insert that part into a new PostScript file, rotated by n degrees (for example, rotate by 40 degrees, place near 100,100 ).

Is there any way to do so using PostScript commands, or any special features of GhostScript?

Converting the files to PDF and modyfing the PDF is also an option.

EDIT 1:

Due to the problems I discovered while testing PS bases solutions, I prefer to use PDF.

The answers so far solve the issue of "cutting" a PDF. However, I still need to rotate the result by n degrees, where n is not a multiple of 90.

Any tips or direction will be appreciated.
Thanks!

Upvotes: 1

Views: 2350

Answers (2)

David van Driessche
David van Driessche

Reputation: 7048

My expertise lies more with PDF than PostScript, but with PDF it would certainly be possible. The logical steps would summarised be:

1) Open the first PDF and change the trim-box of this file so that only the part you want remains visible.

2) Open the second PDF file and composite the first PDF on top of it at the desired location.

Doing this in PDF makes it safer because PDF is safer to manipulate than PostScript (no chance of some clever PostScript code in one of the files throwing your algorithm off).

How you do this largely depends on what your constraints are in the project you are working on. There are certainly commercial tools that can do things like this. I would guess that there are also open source / free tools that could help, perhaps even GhostScript itself or something like pdflib.

Upvotes: 0

plinth
plinth

Reputation: 49189

Think of the problem in terms of PostScript. In order to get the effect of a "cut" you need to apply a clipping path. You want a rectangle, so that's easy:

clipleft clipbottom moveto
0 clipheight rlineto
clipwidth 0 rlineto
0 clipheight neg rlineto
closepath clip

If you inject this code before the start of the page you want, it should clip to that rectangle.

To get the affect of a rotation, you either apply a transformation matrix or use the rotate command:

degrees rotate

which will affect all operations after. Now, more likely, you're going to want to do something like this:

degrees rotate
placementx placementy translate
0 0 moveto
0 clipheight rlineto
clipwidth 0 rlineto
0 clipheight neg rlineto
closepath clip

which will rotate the axes, translate the origin to where you want it, draw the clipping rect and clip to it. Any drawing afterwards will be affected by the previous transformations -- unless the code that renders the page calls the PostScript operator initgraphics (or any other operator that resets the page properties), then there's not much you can do except maybe redefine initgraphics, which you really shouldn't do (and on many systems are probably forbidden to do).

Upvotes: 4

Related Questions