First,
determine the bounding box of your first PDF page:
gs
-q
-dBATCH
-dNOPAUSE
-sDEVICE=bbox
-dLastPage=1
stackoverflowQuestion.pdf
2>&1
| grep %%BoundingBox
The resulting output will be:
%%BoundingBox: 119 531 464 814
It means:
- the lower left corner of the bounding box is at coordinate
(119,531)
- the upper right corner of the bounding box is at coordinate
(464,814)
The values are in PostScript points (where 72 pt == 1 inch
) . The bounding box is that rectangle, which includes these graphical PDF objects that leave ink or toner marks on a page.
Then,
create your PNG.
Deriving from the bounding box value, you seem to want it 345 pt wide (= 464 - 119
) and 283 pt high (= 814 - 531
). This leads to a pages size of -g345x283
(given in pixels, because Ghostscript uses by default 72 dpi for image output (unless specified otherwise), and therefor 72 px == 1 inch
.
Or better, we keep a security zone of 1 pt away from the bounding box, so we make the image a bit bigger than the bare minimum and we get this image dimension: -g347x285
.
You also need to cut off 119 pt from the left edge (118 pt for 'security') and 531 pt from the bottom edge (530 for security).
Hence the command would be:
gs
-o out.png
-sDEVICE=pngalpha
-g347x285
-dLastPage=1
-c "<</Install {-118 -530 translate}>> setpagedevice"
-f stackoverflowQuestion.pdf
Here is the resulting PNG:
For a better PNG quality, increase the resolution from the default 72 dpi to 720 dpi and use this command:
gs
-o out720dpi.png
-sDEVICE=pngalpha
-r720
-g3470x2850
-dLastPage=1
-c "<</Install {-118 -530 translate}>> setpagedevice"
-f stackoverflowQuestion.pdf
Update:
On Windows in a CMD window, the console application names for Ghostscript are gswin32c.exe
and/or gswin64c.exe
(instead of gs
). Also, you'd have to use ^
as a line continuation character (instead of
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…