Scale image in page by adding margins to PDF with Ghostscript

感情迁移 提交于 2019-12-11 19:16:06

问题


My customer wants me to add additional information to right and top border on their single page PDF. The file contains image of the product with measures and other info. I need to scale the content of the page, so that right/top margins would be clear to add extra data. I have played around with Ghostscript in Windows, but so far either the image is cropped out or nothing happens: gswin32c -sOutputFile=scaled2.pdf -dBATCH -dNOPAUSE -dDEVICEHEIGHTPOINTS=312 -dDEVICEWIDTHPOINTS=421 -dFIXEDMEDIA -dPDFFitPage -sDEVICE=pdfwrite -c "<> setpagedevice" -f 255x195x175mm_EB_Fefco0427_FSN1013.pdf

As I usually don't work with PDFs then I'm out of my debth here.

I need the image in PDF scaled smaller and sitting in the bottom-right corner. PDF size should be the same.

Any other solution that works in Windows and can be handled from commandline, is also OK.


回答1:


You can look at the code presented in my answer here. That shows you how to rescale the content of a PDF file to a different media size, and how to reposition the scaled content on the nominal output medium.

I'm rather surprised that -c "<>setpagedevice" -f doesn't simply produce an error, since that's not valid PostScript.

[edit]

OK so you either need to do two passes or do the calculation of the scale yourself.

In the two pass model you first apply the scaling, which will get you a PDF file where the content still fills the page, but the page has been resized to the smaller dimensions. You then run it with the translation and the desired final media size, but don't set FitPage.

To do the scaling yourself you would need to add the scale to the BeginPage command instead of having Ghostscript do it for you. That means you are going to have to calculate the scale factors yourself.

The parameter to the setpagedevice operator would look something like :

<</BeginPage {0.5 0.5 scale 200 0 translate}>> setpagedevice

That will scale the content down by 50% in each direction, and move the origin by 100 points to the right. Obviously the translate values will depend on the scale factor and the size of the original and scaled media. I would try this to check it, but you haven't supplied an example PDF file to test.



来源:https://stackoverflow.com/questions/54050625/scale-image-in-page-by-adding-margins-to-pdf-with-ghostscript

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