1

I'm looking for a ghostscript (or other commandline) command to reimpose a pdf page so that the elements on the left side are copied to the right. Like so: schematic view of reimposition

The size of the page shouldn't change (each page is cropped and cut differently), and while I could supply the final size manually, it would be neater to read it from the original pdf.

For the sake of simplicity, let's assume that the input file has only one page.

I've come up with an extremely complicated series of commands, involving

  • reading the CropBox with pdfinfo
  • copying the file and changing the cropbox so that the left half is shortened and the right half is extended with the -c "[/CropBox [*new dimensions*] /PAGES pdfmark" command
  • copying the file and changing the cropbox so that the left half remains
  • reprocessing this file with the old page dimensions -g "PageDimension" and -c \"<<\/Install{1 1 scale WithOfRightside 0 translate}>> setpagedevice\"
  • use pdftk to merge the two new files into one page: pdftk.exe lefthalf.pdf background righthalf.pdf output combinedfile.pdf

I couldn't get this to work satisfactorily, however, and I don't like either the series of steps involved or the number of tools invovled. Surely all the steps could be performed with ghostscript and in fewer steps (and less reprocessing of the original).

mheim
  • 366
  • 1
  • 12
  • You could do it with Ghostscript, but...... Not in one step, you would need at least 3. – KenS Feb 08 '16 at 19:31
  • Ok, so how would I do this? I'm especially wondering about how to do the actual imposition of two pages / two regions of a page on one new page in Ghostscript – mheim Feb 09 '16 at 09:29
  • 1
    First you need to create a page which contains only the left portion, then a page which contains only the right portion. Then you need to create a new PDF file where you render the right portion to the left of the page and the left portion offset to the right. To do the latter (imposition) you need to use a BeginPage and EndPage procedure in the page device dictionary. Your EndPage procedure needs to *not* emit page 1, but emit page 2. You'll have to experiment with getting page 2 relocated, might be possible as part of EndPage but might need BeginPage as well. – KenS Feb 09 '16 at 10:00

1 Answers1

0

I have finally come up with a useful solution - though it does not reflect the original question fully.

This solution is based on (proprietary) Acrobat, and uses the Acrobat JavaScript interface - not GhostScript. But the following script works beautifully, which is why I decided to share it:

/*
 * Acrobat PDF script
 * transpose part of left page to right side and recrop document
 */

// define cutting line, in points from left
var cuttingline = 300;

/* define offset(s)  ---  if uncertain, leave at 0
   a) of new left page border, 
   b) of transposed half of page
   
   WATCH OUT: 
   a) may expose material from original left half when negative
   b) may expose material from original right half when negative - leave "correctcrop" true to avoid this.
*/
var offsetleft = 5;
var offsettransposition = -50;
var correctcrop = true;

// cut off left page and add as much white space to right, then insert left part of page on top right
for (var p = 0; p < this.numPages; p++) {
  // add white space to media box right
  console.println("\nPage " + (p + 1));
  var aRect = this.getPageBox("Media", p);
  console.println("Original media box: " + aRect);
  aRect[2] += cuttingline + offsettransposition;
  console.println("New media box: " + aRect);
  this.setPageBoxes("Media", p, p, aRect);


  // Add copy of page as overlay, shifted to the right
  this.addWatermarkFromFile({
    cDIPath: this.path,
    nSourcePage: p,
    nStart: p,
    nEnd: p,
    nHorizAlign: app.constants.align.left,
    nVertAlign: app.constants.align.bottom,
    nHorizValue: aRect[2] - cuttingline + offsettransposition,
    nVertValue: 0
  });

  // crop left, add space to crop box right to reveal page copy
  var aRect = this.getPageBox("Crop", p);
  console.println("Original crop box: " + aRect);
  aRect[0] += cuttingline + offsetleft;
  aRect[2] += cuttingline + offsettransposition + (((correctcrop == true) && (offsettransposition < 0)) ? offsettransposition : 0);
  console.println("New crop box: " + aRect);
  this.setPageBoxes("Crop", p, p, aRect);
}

// flatten layers
this.flattenPages();

Please take note: this doubles the page content. Use a Preflight profile or Acrobat's document cleanup-tools to remove the (invisible) page contents.

mheim
  • 366
  • 1
  • 12