How can I shift page images in PDF files more to the left or to the right?

佐手、 提交于 2019-11-27 14:26:41

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...)

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();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!