Printing PDFs from Windows Command Line

后端 未结 11 525
我寻月下人不归
我寻月下人不归 2020-11-30 01:06

I\'m trying to print all pdfs in current dir. When I call this bash script in cmd (singlepdf.sh): \'\"C:\\Program Files (x86)\\Adobe\\Reader 10.0\\Reader

11条回答
  •  青春惊慌失措
    2020-11-30 01:55

    Here is another solution:

    1) Download SumatraPDF (portable version) - https://www.sumatrapdfreader.org/download-free-pdf-viewer.html

    2) Create a class library project and unzip the SumatraPDF.exe to the project directory root and unblock it.

    3) Inside the project Properties, go to the Resoruces tab and add the exe as a file.

    4) Add the following class to your library:

    public class SumatraWrapper : IDisposable
    {
        private readonly FileInfo _tempFileForExe = null;
        private readonly FileInfo _exe = null;
    
        public SumatraWrapper()
        {
            _exe = ExtractExe();
        }
    
        public SumatraWrapper(FileInfo tempFileForExe)
            : this()
        {
            _tempFileForExe = tempFileForExe ?? throw new ArgumentNullException(nameof(tempFileForExe));
        }
    
        private FileInfo ExtractExe()
        {
            string tempfile = 
                _tempFileForExe != null ? 
                _tempFileForExe.FullName : 
                Path.GetTempFileName() + ".exe";
    
            FileInfo exe = new FileInfo(tempfile);
            byte[] bytes = Properties.Resources.SumatraPDF;
    
            using (FileStream fs = exe.OpenWrite())
            {
                fs.Write(bytes, 0, bytes.Length);
            }
    
            return exe;
        }
    
        public bool Print(FileInfo file, string printerName)
        {
            string arguments = $"-print-to \"{printerName}\" \"{file.FullName}\"";
            ProcessStartInfo processStartInfo = new ProcessStartInfo(_exe.FullName, arguments)
            {
                CreateNoWindow = true
            };
            using (Process process = Process.Start(processStartInfo))
            {
                process.WaitForExit();
                return process.ExitCode == 0;
            }
        }
    
        #region IDisposable Support
        private bool disposedValue = false; // To detect redundant calls
    
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects).
                }
    
                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.
                try
                {
                    File.Delete(_exe.FullName);
                }
                catch
                {
    
                }
    
                disposedValue = true;
            }
        }
    
        // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
        // ~PdfToPrinterWrapper() {
        //   // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
        //   Dispose(false);
        // }
    
        // This code added to correctly implement the disposable pattern.
        public void Dispose()
        {
            // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
            Dispose(true);
            // TODO: uncomment the following line if the finalizer is overridden above.
            // GC.SuppressFinalize(this);
        }
        #endregion
    }
    

    5) Enjoy printing pdf files from your code.

    Use like this:

    FileInfo file = new FileInfo(@"c:\Sandbox\dummy file.pdf");
    SumatraWrapper pdfToPrinter =
        new SumatraWrapper();
    pdfToPrinter.Print(file, "My Printer");
    

提交回复
热议问题