[使用C#中的Ghostscript压缩PDF

问题描述 投票:0回答:1

我正在尝试使用Ghostscript压缩PDF,但是它在压缩后会创建损坏的PDF。

我写了下面的方法来执行pdf压缩。

public void CompressPDF(string inputFile, string outputFile)
        {
            GhostscriptVersionInfo gv = GhostscriptVersionInfo.GetLastInstalledVersion();

            using (GhostscriptProcessor processor = new GhostscriptProcessor(gv, true))
            {
                //processor.Processing += new GhostscriptProcessorProcessingEventHandler(processor_Processing);

                List<string> switches = new List<string>();
                switches.Add("-empty");
                switches.Add("-dSAFER");
                switches.Add("-sDEVICE=pdfwrite");
                //switches.Add("-dCompatibilityLevel=1.4");
                switches.Add("-dPDFSETTINGS=/screen");
                switches.Add("-dNOPAUSE");
                switches.Add("-dQUIET");
                switches.Add("-dBATCH");
                switches.Add("-dEmbedAllFonts=true");
                switches.Add("-dSubsetFonts=true");
                switches.Add("-dColorImageDownsampleType=/Bicubic");
                switches.Add("-dColorImageResolution=144");
                switches.Add("-dGrayImageDownsampleType=/Bicubic");
                switches.Add("-dGrayImageResolution=144");
                switches.Add("-dMonoImageDownsampleType=/Bicubic");
                switches.Add("-dMonoImageResolution=144");
                switches.Add(@"-sOutputFile=" + outputFile);
                switches.Add(@"-f");
                switches.Add(inputFile);

                LogStdio stdio = new LogStdio();
                processor.Process(switches.ToArray(), null);
            }
        }

我正在使用GhostScript nuget package

我正在从我的api控制器中调用上述方法,如下所示:

GhostScriptHelper ghostScriptHelper = new GhostScriptHelper();
var appPath = HostingEnvironment.MapPath("~/");
var inputPath = Path.Combine(appPath, "Contents/Input/Sample.pdf");
var outputPath = Path.Combine(appPath, "Contents/Output/Compressed.pdf");
ghostScriptHelper.CompressPDF(inputPath, outputPath);

它的确在输出文件夹中创建了一个pdf文件,但该pdf已损坏。无法打开该PDF。

我不知道,这是怎么了。

c# asp.net ghostscript ghostscript.net
1个回答
0
投票

我建议您从命令行使用Ghostcript开始,然后看看会发生什么。您不能在单色图像上使用Bicubic下采样滤镜,因为那样会使它们变成灰度图像。我“相当”肯定该代码会怀疑单色图像的唯一有效滤镜是Subsample。

您指向的NuGet包不是Ghostscript,而是它的Ghostscript.NET,它是对Ghostscript DLL的包装。这是一个小问题,但是如果该包装程序有问题,则必须与作者联系。因此,如果命令行中的Ghostscript使用相同的设置,而.NET代码不起作用,那么您应该与他联系。

查看输入和输出文件可能会有所帮助,并且所使用的Ghostscript版本是有用的信息。

© www.soinside.com 2019 - 2024. All rights reserved.