简体   繁体   中英

Compress PDF using Ghostscript in C#

I am trying to compress PDF using Ghostscript, But It is creating corrupt PDF after compressing it.

I have written below method to perform pdf compression.

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);
            }
        }

I am using GhostScript nuget package

I am calling above method from my api controller as below:

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);

It does create a pdf file in output folder, but that pdf is corrupt. Can't open that PDF.

I have no clue, what's wrong here.

As Suggested by KenS. I tried to remove extra switches, but it is still not working.

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

            using (GhostscriptProcessor processor = new GhostscriptProcessor(gv, true))
            {
                List<string> switches = new List<string>();

                //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(@"-sOutputFile=" + outputFile);
                switches.Add(@"-f");
                switches.Add(inputFile);
                processor.Process(switches.ToArray(), null);
            }
        }

Where as, If I use command, Then it successfully compress PDF.

gswin64c -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=D:\Input\output.pdf D:\Input\Sample.pdf

I am trying to compress PDF using Ghostscript, But It is creating corrupt PDF after compressing it.

I have written below method to perform pdf compression.

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);
            }
        }

I am using GhostScript nuget package

I am calling above method from my api controller as below:

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);

It does create a pdf file in output folder, but that pdf is corrupt. Can't open that PDF.

I have no clue, what's wrong here.

As Suggested by KenS. I tried to remove extra switches, but it is still not working.

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

            using (GhostscriptProcessor processor = new GhostscriptProcessor(gv, true))
            {
                List<string> switches = new List<string>();

                //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(@"-sOutputFile=" + outputFile);
                switches.Add(@"-f");
                switches.Add(inputFile);
                processor.Process(switches.ToArray(), null);
            }
        }

Where as, If I use command, Then it successfully compress PDF.

gswin64c -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=D:\Input\output.pdf D:\Input\Sample.pdf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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