如何使用Ghostscript裁剪pdf(无需手动输入边界框) [英] How to crop pdf using Ghostscript (without entering manually Bounding Box)

查看:138
本文介绍了如何使用Ghostscript裁剪pdf(无需手动输入边界框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将pdf文件裁剪到其边界框。
首先,我计算实际的边界框:

I need to crop a pdf-file to its Bounding Box. First I calculate actual Bounding Box:

gswin64c.exe ^
  -o nul ^
  -sDEVICE=bbox ^
  input.pdf

结果

%% HiResBoundingBox:156.350019 391.521011 44​​5.919963 446.259010

%% HiResBoundingBox: 156.350019 391.521011 445.919963 446.259010

我替换为

gswin64c.exe ^
  -o output.pdf ^
  -sDEVICE=pdfwrite ^
  -dUseCropBox=true ^
  -c "[/CropBox [156.350019 391.521011 445.919963 446.259010] /PAGES pdfmark" ^
  -f input.pdf

有没有办法自动替换边界框?

is there a way to substitute the Bounding Box automatically?

谢谢。

推荐答案

您需要的称为命令替换。请通过 for /?命令参考帮助

What you need is called command substitution. Please refer to help by 'for /?' command

为简单起见,我将答案分为两个文件

For simplicity I have separated answer into two files

第一个文件( getbb.bat )获得边界框

First file (getbb.bat) get bounding box

@echo off
"C:\Program Files\gs\gs9.02\bin\gswin64c.exe"^
  -o nul -sDEVICE=bbox %1 2>&1 | find "ResBoundingBox"

第二个文件( replacebb.bat

@echo off

for /f "tokens=2 delims=:" %%b in ('getbb.bat %1') do (
call :Trim bbox %%b
"C:\Program Files\gs\gs9.02\bin\gswin64c.exe" ^
  -o output.pdf -sDEVICE=pdfwrite -dUseCropBox=true ^
  -c "[/CropBox [%bbox%] /PAGES pdfmark" -f input.pdf
)
exit /b

:Trim
SetLocal EnableDelayedExpansion
set Params=%*
for /f "tokens=1*" %%a in ("!Params!") do EndLocal & set %1=%%b
exit /b

这篇关于如何使用Ghostscript裁剪pdf(无需手动输入边界框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆