10

We have a bunch of scanned pages (about 600) for which every PDF viewer displays the image with zero margin on the right edge, but about 2 inch margin on the left. (Presumably while scanning, there was a wrong setting used...)

We want to print these pages, preferably as a booklet. Is there a way to permanently shift all page images towards the center and have the PDF display these pages also in a more pleasing way? Can Ghostscript do that? Can one do this with some other method, such as programatically with the help of some PDF processing library?

  • This is not a programming question and would benefit from being moved to SuperUser. – Robino Jan 14 '16 at 09:09
  • @Robino I'm sorry, is postscript not a programming language? – ebyrob Jun 10 '18 at 12:20
  • @ebyrob The goal is to alter a PDF in a specific way. It is not about programming, even though there is a solution using a programming language. – Robino Jun 12 '18 at 19:07
  • 1
    @Robino By that logic, walking on the moon is not related to orbital mechanics. PS - PDF (though compressed and obfuscated) is also basically a programming language. Certainly we wouldn't expect users to understand *.doc format better than programmers. – ebyrob Jun 14 '18 at 15:51

2 Answers2

12

If you don't want to write your own program code (as Nikolaus suggested), but use a Ghostscript commandline instead, you need to know 3 things:

  1. PostScript has a setpagedevice operator that takes a PageOffset parameter;
  2. Ghostscript will process snippets of PostScript code if you pass them with -c ... on the commandline;
  3. Ghostscript can evaluate and apply (some) PostScript code even for direct PDF=>PDF conversions.

Now try this commandline to shift all page images by 1 inch (==72pt) to the left:

gswin32c.exe ^
  -sDEVICE=pdfwrite ^
  -o c:/path/to/output/pdf-shifted-by-1-inch-to-left.pdf ^
  -dPDFSETTINGS=/prepress ^
  -c "<</PageOffset [-72 0]>> setpagedevice" ^
  -f c:/path/to/input/pdf-original.pdf

(The -dPDFSETTINGS=/prepress I put in in order to not loose any picture quality of the scans...)

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • I've got a new problem now. It is about font extracting. I'll ask a separate question about this. –  Aug 15 '10 at 15:05
  • @Community: **NO** -- Ghostscript does **not** require the backslashes instead of the forward slashes. You can keep them as they are and it will work the same. (Doesn't hurt to change to backslashes though... however, you may need to change to **double** backslashes for some versions even...) And **NO**, you don't need to change hyphens (`-`) to forward slash (`/`) -- that one can even hurt! I'll revert your modification, sorry! – Kurt Pfeifle Jul 26 '12 at 13:20
  • I got error `**** Unable to open the initial device, quitting.` while launching this command. Any idea ? – Stéphane Jun 06 '14 at 14:11
  • @Stéphane: is your `-o ...` parameter pointing to an output directory where you do not own the privilege to write into? – Kurt Pfeifle Jun 09 '14 at 13:53
  • Hum no that's /tmp pointing to ... Anyway I endeded using convert command, which is not perfect. And I modified my original odt document to add margin. Thanks – Stéphane Jun 11 '14 at 09:38
3

you can use iText to move, scale or crop pdf-pages

you need to define a PdfReader for your source file, and a Document for your Target file then you iterate over the pages if the Reader, create a new page in the Document and add the sourcePage as a Template to the new page (shifting, scaling etc wherever you want)

    PdfReader reader = new PdfReader( input );
    int n = reader.getNumberOfPages();

    Rectangle psize = reader.getPageSize(1);
    float width = psize.getHeight();
    float height = psize.getWidth();

    Document document = new Document(new Rectangle(height, width));
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream( output ));

    document.open();

    PdfContentByte cb = writer.getDirectContent();

    int i = 0;
    while (i < n) {
        i++;
        document.newPage();
        PdfImportedPage page = writer.getImportedPage(reader, i);
        cb.addTemplate(page, factor, 0, 0, factor, left, down);
    }

    document.close();
Nikolaus Gradwohl
  • 19,708
  • 3
  • 45
  • 61