简体   繁体   English

Ghostscript剪辑pdf文件

[英]Ghostscript clips a pdf file

I'm trying to print a pdf with Ghostscript using those settings : 我正在尝试使用这些设置使用Ghostscript打印pdf:

var switches = new List<string>
            {
                @"-empty",
                @"-dPrinted",
                @"-dNOPAUSE",
                @"-dNOSAFER",
                @"-dQUIET",
                @"-dPDFSETTINGS=/printer",
                @"-dNumCopies=1",
                @"-sDEVICE=mswinpr2",
                @"-dCompatibilityLevel=1.4",
                @"-sOutputFile=%printer%" + printerSettings.PrinterName,
                @"-f",
                pdfFileName
            };

but either the pdf or Ghostscript have bad margins and while it's good when I print it to file it clips when I print it on my printer. 但是pdf或Ghostscript的页边距都不好,当我在打印机上打印它时,将其打印为文件片段时效果很好。

Is there any way to add those programatically with Ghostscript ? 有什么办法可以通过Ghostscript以编程方式添加这些内容? I tried many different solutions from first pages of google but none of them work and they seem to have no effect on the printed pdf. 我从Google首页尝试了许多不同的解决方案,但没有一个起作用,而且它们似乎对打印的pdf没有影响。

When I try to print it out with Adobe or IE it magicaly adds margins as soon as I choose the printer and it prints fine. 当我尝试使用Adobe或IE打印时,一旦选择打印机,它就会神奇地增加页边距,并且打印效果很好。

How to achive the same with Ghostscript ? 如何使用Ghostscript达到相同的目的?

OK well the first thing is that many of the switches you are setting have no effect: 好吧,第一件事是您要设置的许多开关都无效:

-empty isn't a Ghostscript-understood switch, and I'm slightly surprised it doesn't cause an error. -empty不是可理解Ghostscript的开关,我有点惊讶它不会引起错误。

-dPDFSETTINGS only affects the pdfwrite device, which is why it is documented in the vector devices section. -dPDFSETTINGS仅影响pdfwrite设备,这就是为什么在vector devices部分中对其进行了说明的原因。

-dCompatabilityLevel only affects the output of the pdfwrite device. -dCompatabilityLevel仅影响pdfwrite设备的输出。

-dNOSAFER doesn't have any effect, as that's the default setting. -dNOSAFER没有任何作用,因为这是默认设置。

-f is used to 'close' direct PostScript insertion begun with -c, if you don't use -c you don't need -f -f用于“关闭”以-c开头的直接PostScript插入,如果您不使用-c,则不需要-f

Now almost certainly neither Ghostscript nor your PDF has 'bad margins', the most likely explanation for your problem is that the printer you are using cannot print to the boundaries of the page, the left/right edges, and potentially the top and bottom edges are used by the paper transport mechanism, and the printer cannot print there. 现在几乎可以肯定,Ghostscript或PDF都没有“页边距”,最可能的问题原因是您使用的打印机无法打印到页面边界,左/右边缘以及可能的上下边缘由纸张传输机构使用,并且打印机无法在那里打印。

In order to deal with that, you will need to reduce the size of the image, which you can 'probably' do by setting -dDEVICEWIDTHPOINTS and -dDEVICEHEIGHTPOINTS and -dFIXEDMEDIA. 为了解决这个问题,您将需要减小图像的大小,可以通过设置-dDEVICEWIDTHPOINTS和-dDEVICEHEIGHTPOINTS以及-dFIXEDMEDIA来“减小”图像的大小。 Its going to be up to you to work out the correct values for width and height. 由您决定正确的宽度和高度值。

Added after the comments below 在以下评论之后添加

There are two parts to this problem, the first is to deduce the size of the actual available area to print on, and to scale the output to that size. 此问题有两部分,第一部分是推论要打印的实际可用区域的大小,然后将输出缩放到该大小。 The second is to then reposition the output on the media so that it is all printed. 第二种是然后将输出重新放置在介质上,以便将其全部打印出来。 If as you say the content is significantly smaller than the media then you can ignore rescaling it, but the entire solution is presented here for completeness. 如果您说的内容比媒体小得多,则可以忽略对内容进行缩放的问题,但是此处出于完整性考虑提供了整个解决方案。

Now as previously mentioned, the first part of this is achieved primarily by creating a fixed size canvas; 现在,如前所述,这的第一部分主要是通过创建固定大小的画布来实现的。 this is done with any of the media selection switches and the addition of -dFIXEDMEDIA. 这可以通过任何介质选择开关以及-dFIXEDMEDIA的添加来完成。

NOTE if you alter the media size, then you must, obviously, also alter the scale of the contents, or it won't fit. 注意如果更改了介质尺寸,则显然也必须更改内容的比例,否则将不合适。 So you also need to set PSFitPage, EPSFitPage or PDFFitPage depending on the type of input (very recent versions of Ghostscript can use -dFitPage no matter what the input type). 因此,您还需要根据输入的类型设置PSFitPage,EPSFitPage或PDFFitPage(无论输入类型如何,最新版本的Ghostscript都可以使用-dFitPage)。

As an experiment I used the file /ghostpdl/examples/text_graphic_image.pdf and sent the output to a printer on FILE: 作为实验,我使用文件/ghostpdl/examples/text_graphic_image.pdf并将输出发送到FILE上的打印机:

This command line: 此命令行:

gswin32 -dDEVICEHEIGHTPOINTS=391 -dDEVICEWIDTHPOINTS=306 -dFIXEDMEDIA -sDEVICE=mswinpr2 -sOutputFile=%printer%KensPrinter /ghostpdl/examples/text_graphic_image.pdf

produces output where 3/4 the image is clipped away (the content lies off the newly defined fixed canvas size). 产生的输出将裁剪掉3/4的图像(内容位于新定义的固定画布大小之外)。 If I modify that to: 如果我将其修改为:

gswin32 -dDEVICEHEIGHTPOINTS=391 -dDEVICEWIDTHPOINTS=306 -dFIXEDMEDIA -dPDFFitPage -sDEVICE=mswinpr2 -sOutptuFile=%printer%KensPrinter /ghostpdl/examples/text_graphic_image.pdf

then the result is a perfect reproduction of the original, at 1/4 the size (half in each direction). 这样就可以完美地复制原件,尺寸为1/4(每个方向的一半)。

So, the first thing you need to do is establish the actual printable area of the media on your printer, then you can set the width and height correctly as fixed media, and tell Ghostscript to scale the page to fit. 因此,您需要做的第一件事是在打印机上建立介质的实际可打印区域,然后可以将宽度和高度正确设置为固定介质,并告诉Ghostscript缩放页面以适合页面大小。

However, this will still leave the printed image at the bottom left of the media. 但是,这仍将打印的图像留在介质的左下方。 Since that's in an area that can't be printed, you need to shift the printed image until its centred on the page. 由于该区域处于无法打印的区域,因此需要移动已打印的图像,直到其居中。 As I suggested, you can do this with a BeginPage procedure. 如我所建议的,您可以使用BeginPage过程来做到这一点。 This: 这个:

gswin32 -dDEVICEHEIGHTPOINTS=391 -dDEVICEWIDTHPOINTS=306 -dFIXEDMEDIA -dPDFFitPage -sDEVICE=mswinpr2 -sOutptuFile=%printer%KensPrinter -c "<</BeginPage {100 100 translate}>> setpagedevice" -f /ghostpdl/examples/text_graphic_image.pdf

produces output where the printed image is shifted up and right by 100 points each. 产生输出,其中打印的图像分别向右上移100个点。

I believe that a little investigation will allow you to figure out where exactly your printer is able to print, and to create appropriately sized unprintable margins. 我相信,稍作调查就能使您弄清楚打印机到底可以在哪里打印,并创建适当大小的不可打印页边距。

Note that, for me, the %printer% syntax does not result in a printer selection dialog. 请注意,对我来说,%printer%语法不会导致打印机选择对话框。 I suspect that your code (whatever language that is) is expanding the %p, resulting in a corruption of the name. 我怀疑您的代码(无论使用哪种语言)都在扩展%p,从而导致名称损坏。 Or possibly whatever you use to fork a Ghostscritp process does it. 或者可能使用任何用于派生Ghostscritp进程的方法来执行此操作。 In either event you probably need to double the % signs. 无论哪种情况,您都可能需要将%符号加倍。

You should get this working from the command line first, then work on getting it into an application. 您应该首先从命令行启动此功能,然后再将其添加到应用程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM