How to print a PDF with C#

前端 未结 11 589
抹茶落季
抹茶落季 2020-12-14 17:58

I´ve trying to solve this problem for nearly 2 days. There are a lot of more or fewer good solutions on the net, but not a single one fits my task perfectly.

11条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 18:28

    You can use ghostscript to convert PDF into image formats.

    The following example converts a single PDF into a sequence of PNG-Files:

    private static void ExecuteGhostscript(string input, string tempDirectory)
    {
        // %d will be replaced by ghostscript with a number for each page
        string filename = Path.GetFileNameWithoutExtension(input) + "-%d.png";
        string output = Path.Combine(tempDirectory, filename);
    
        Process ghostscript = new Process();
        ghostscript.StartInfo.FileName = _pathToGhostscript;
        ghostscript.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    
        ghostscript.StartInfo.Arguments = string.Format(
            "-dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r300 -sOutputFile=\"{0}\" \"{1}\"", output, input);
    
        ghostscript.Start();
        ghostscript.WaitForExit();
    }
    

    If you prefer to use Adobe Reader instead you can hide its window:

    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    

提交回复
热议问题