使用GhostScript打印PDF [英] Print PDF using GhostScript

查看:941
本文介绍了使用GhostScript打印PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于它使我困扰了一段时间,因此我需要您在以下问题上的支持.我们有一个小的c#实用程序,可以使用GhostScript给定PDF进行打印.此打印符合预期,但无法保留页面格式.但是,当我切换Adobe Acrobat代替GhostScript时,页面按预期打印.因此,我想我在GhostScript的命令行参数上犯了一些明显的错误.

I am in need of your support on the following issue since its pulling me for a while. We have a small c# utility, which print given PDF using GhostScript. This print as expected but fail to retain the page formatting’s. However, pages are printed as expected when I switch Adobe Acrobat in place of GhostScript. So I presume, I am making some obvious mistake on the GhostScript's command line arguments .

背景

以下是c#的核心逻辑,该逻辑在每个页面上打印具有不同样式的给定PDF文件.给定的PDF文件包含页面;

Following is the core c# logic, which print a given PDF file with varying style across each pages. The given PDF file has pages;

  1. 字体样式和颜色不一致
  2. 某些页面的字体大小正常,其他页面则打印得很小
  3. 有些页面建议使用页边距,而另一些页面建议使用页边距
  4. 有些页面是彩色的,其余页面是灰色的.
  5. 有些页面是横向的,其他是纵向的

简而言之,我要打印的PDF仅仅是合并(将单个pdf合并为一个大pdf),其中包含许多具有不同字体样式,大小和边距的小尺寸pdf文档.

In concise, the PDF which I am trying to print is nothing but a consolidation (joining individual pdfs into one large pdf) of numerous small sized pdf document with varying fonts style, size, margins.

问题

以下逻辑使用GhostScript(v9.02)打印PDF文件.尽管以下逻辑可打印任何给定的PDF,但它无法保留页面格式,包括页眉,页脚,字体大小,边距,方向(我的pdf文件中包含横向和纵向页面).

Following logic use GhostScript(v9.02) to print PDF file. Though the following logic print any given PDF, it fail to retain the page formatting including header, footer, font size, margin, orientation ( my pdf file has pages those both landscape and portrait).

有趣的是,如果我使用acrobat阅读器打印相同的PDF,则它将按预期打印以及所有页面级格式.

Interestingly, if I use acrobat reader to print the same PDF then it will print as expected along with all page level formatting's.

PDF标本:第一部分第二部分

  void PrintDocument()
    {
         var psInfo = new ProcessStartInfo();
                psInfo.Arguments =
                    String.Format(
                        " -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=ljet4 -sOutputFile=\"\\\\spool\\{0}\" \"{1}\"",
                        GetDefaultPrinter(), @"C:\PDFOutput\test.pdf");
                psInfo.FileName = @"C:\Program Files\gs\gs9.10\bin\gswin64c.exe";
                psInfo.UseShellExecute = false;

        using (var process= Process.Start(psInfo))
        {
            process.WaitForExit();
        }
    }

推荐答案

答案-UPDATE 16/12/2013

我已设法将其修复,并希望在可行的解决方案中提供帮助,以帮助其他人.特别感谢"KenS",因为他花了很多时间指导我.

I was managed to get it fixed and wanted to enclose the working solution if it help others. Special thanks to 'KenS' since he spent lot of time to guide me.

总而言之,我最终决定使用GSView和GhostScript来打印PDF以绕过Adobe.核心逻辑如下:

To summarize, I finally decided to use GSView along with GhostScript to print PDF to bypass Adobe. The core logic is given below;

 //PrintParamter is a custom data structure to capture file related info
private void PrintDocument(PrintParamter fs, string printerName = null)
        {
            if (!File.Exists(fs.FullyQualifiedName)) return;

            var filename = fs.FullyQualifiedName ?? string.Empty;
            printerName = printerName ?? GetDefaultPrinter(); //get your printer here

            var processArgs = string.Format("-dAutoRotatePages=/All -dNOPAUSE -dBATCH -sPAPERSIZE=a4 -dFIXEDMEDIA -dPDFFitPage -dEmbedAllFonts=true -dSubsetFonts=true -dPDFSETTINGS=/prepress -dNOPLATFONTS -sFONTPATH=\"C:\\Program Files\\gs\\gs9.10\\fonts\" -noquery -dNumCopies=1 -all -colour -printer \"{0}\" \"{1}\"", printerName, filename);
            try
            {

                var gsProcessInfo = new ProcessStartInfo
                                        {
                                            WindowStyle = ProcessWindowStyle.Hidden,
                                            FileName = gsViewEXEInstallationLocation,
                                            Arguments = processArgs
                                        };
                using (var gsProcess = Process.Start(gsProcessInfo))
                {

                    gsProcess.WaitForExit();

                }

        }

这篇关于使用GhostScript打印PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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