0

当我在 windows cmd 中使用带有 setup.ps postscript 文件的 ghostscript 时,它会完美地打印我的 pdf。

安装程序.ps

mark
/OutputFile (%printer%HP LaserJet 1018)
/BitsPerPixel  1 
/NoCancel false     
/UserSettings
   <<
      /DocumentName(document) 
      /MaxResolution 360  
   >>

(mswinpr2)finddevice 
putdeviceprops
setdevice
    <<
      /BeginPage {10 -55 translate}
    >>
setpagedevice

命令行

start /d "C:\Program Files (x86)\gs\gs9.19\bin" gswin32.exe -sOutputFile="%printer%HP LaserJet 1018" -dBATCH -dNOPAUSE -dFIXEDMEDIA setup.ps a.pdf

(我不知道为什么它在 setup.ps 和命令行中需要 sOutputFile 但否则它不起作用)

现在,当我在使用 Ghostscript.NET 包装器的 C# 项目中放置相同的开关时。

  private static void CreateSetupPsFile(string printername)
    {
        const string Translationstring = @"{10 -15 translate}";
        string ps = $@"
                      mark
                /OutputFile (%printer%{printername})
                /BitsPerPixel  1 
                /NoCancel false     % don't show the cancel dialog
                /UserSettings
                    <<
                    /DocumentName(document) % name for the Windows spooler
                    /MaxResolution 360  
                    >>

                (mswinpr2)finddevice % select the Windows device driver
                putdeviceprops
              setdevice
                    <<
                    /PageOffset [30 -30] 
                    >>
                    setpagedevice";
        File.WriteAllText("setup.ps", ps);

    }

    private static void PrintA4(string pdfFileName, PrinterSettings printerSettings)
    {
        using (var processor = new GhostscriptProcessor(GsDll))
        {
            CreateSetupPsFile(printerSettings.PrinterName);
            var switches = new List<string>
            {
                $"-sOutputFile=\"%printer%{printerSettings.PrinterName}\"",
                @"-dBATCH",
                @"-dNOPAUSE",
                @"-dFixedMedia",
                "setup.ps",
                "-f",
                pdfFileName

            };
            processor.StartProcessing(switches.ToArray(), null);

    }
}

它完全忽略了 setup.ps 文件中的所有内容。有谁知道为什么?它只是忽略并且不说出了什么问题

先感谢您

更新

我设法运行了一些 poscript... 显然,包装器需要像这样给出后记:

 var switches = new List<string>
                {

                    @"-dBATCH",
                    @"-dNOPAUSE",
                    @"-sDEVICE=mswinpr2",
                    $@"-sOutputFile=%printer%{printerSettings.PrinterName}",
                    "-c",
                    $"<</BeginPage {translateString}>> setpagedevice",
                    "-f",
                    pdfFileName

                };
                processor.StartProcessing(switches.ToArray(), null);

不是这样的:

 var switches = new List<string>
                {

                    @"-dBATCH",
                    @"-dNOPAUSE",
                    @"-sDEVICE=mswinpr2",
                    $@"-sOutputFile=%printer%{printerSettings.PrinterName}",
                    $"-c <</BeginPage {translateString}>> setpagedevice -f",
                    pdfFileName

                };
                processor.StartProcessing(switches.ToArray(), null);

这简直令人难以置信。

4

1 回答 1

0

你怎么知道它忽略了 setup.ps 中的内容?

Tray 在 PostScript 程序中添加一些调试,例如

(Inside setup.ps) == flush

我看到的第一件也是最明显的事情是你认为 setup.ps 包含:

<<
  /BeginPage {10 -55 translate}
>>
setpagedevice

然而,您创建 setup.ps 的代码包含:

                <<
                /PageOffset [30 -30] 
                >>
                setpagedevice";

显然,这些不一样,这让我怀疑您正在执行您认为的 PostScript 代码。

您如何继续我对您之前问题的回答?

添加一个示例以在不使用 setup.ps 或非标准扩展的情况下完成所有 hacky setup.ps 的工作。

首先,您不需要使用 finddevice、putdeviceprops 或 setdevice。只需在命令行上设置设备并使用 setpagedevice 设置属性。这是标准 PostScript 和设备的配置方式。Ghostscript 可能会在幕后使用其非标准的东西,但您无需担心。

所以像:

gswin32 -sDEVICE=mswinpr2 -sOutputFile=%printer%PrinterName -c "<</BitsPerPixel 1 /NoCancel false /DocumentName (dummy) /MaxResolution 360 /BeginPage {10 10 translate}>> setpagedevice" -f PDF.pdf

因为您没有使用 finddevice/putdeviceprops/setdevice,所以您不必为尝试设置 OutputFile 两次而烦恼。我假设您确实想要 BeginPage 而不是 PageOffset。我不知道你是否真的想要所有这些特定于设备的设置,因为我不知道你使用的是什么打印机,所以我刚刚离开了它们。

显然我无法测试这个,因为我没有你的打印机,但它应该可以工作。setup.ps 中的所有乱七八糟的东西都是坏消息,如果可能的话,我会避免它。

于 2016-09-08T12:38:57.747 回答