0

我有一个我想从我的 C# 应用程序打印的 pdf 文件。我能够在代码中确定打印机是否支持双面打印,但可以从我的代码中获取 pdf 以双面打印。这是我的常规单面打印代码。我能够检查 pdf 打印对话框预设为双面的元数据。但它不起作用。

            string verbToUse = "PrintTo";
            startInfo.Verb = verbToUse;
            startInfo.Arguments = workCenterPrinterName.Value.ToString();
            Process p = Process.Start(startInfo);
            p.WaitForExit(5000);//random time after which process will be killed
            if (p.HasExited == false)
            {
                p.Kill();
            }
4

2 回答 2

0

经过大量研究,我编写了这段对我有用的代码。这是应付的整洁副本,我可以写很多研究和摆弄。我找不到更好的解决方案并发布我的工作以帮助他人。我正在打印一个多页 tiff 文件,但此代码也适用于 PDF

using( Image img = Image.FromFile(@"c:\temp\testfile1.tif") ) {
printDocument.DocumentName = controlNumber;
printDocument.DefaultPageSettings.Margins = new Margins( 15, 0, 0, 0 );
printDocument.OriginAtMargins = true;
printDocument.PrinterSettings.PrinterName = request_printer;
printDocument.PrinterSettings.Duplex = Duplex.Default;
FrameDimension frames = new FrameDimension( img.FrameDimensionsList[ 0 ] );
int pages = img.GetFrameCount( frames );
if( printDocument.PrinterSettings.IsValid ) {
    try {
        printDocument.PrinterSettings.Duplex = Duplex.Default;
        int page = 0;
        printDocument.PrintPage += ( sender, e ) => {
                    img.SelectActiveFrame( frames, page );
                    Bitmap bmp = new Bitmap( img );
                    pictureBox.Image = bmp;
                    pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                    printDocument.DefaultPageSettings.Landscape = false;
                    if( bmp.Width > bmp.Height ) {
                         printDocument.DefaultPageSettings.Landscape = true;
                    }

                    if( printDocument.PrinterSettings.IsValid ) {
                         if( e.PageSettings.PrinterSettings.CanDuplex ) {
                                e.PageSettings.PrinterSettings.Duplex = Duplex.Default;
                         }
                         e.Graphics.DrawImage( img, 0, 0 );
                         e.HasMorePages = page < 1;
                    }
             page++;
        };
        printDocument.Print();
        } catch (Exception ex) { }      
}
于 2014-03-04T15:26:06.443 回答
0

我不知道在给定时间过去的情况下发布答案是否正确,但我在这个问题上挣扎了很多,结果发现代码比我们想象的要简单:

public static void Printing(string printer, string fileLoc)
        {
            //Set Duplex Settings Session
            PrinterSettings set = new PrinterSettings();
            set.PrinterName = printer;
            set.Duplex = Duplex.Default;

            //Start Printing process
            ProcessStartInfo info = new ProcessStartInfo();
            info.Verb = "print";
            info.FileName = fileLoc;
            //Print and close program immediatly
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;

            Process P = new Process();
            P.StartInfo = info;
            P.Start();

            P.WaitForInputIdle();
            System.Threading.Thread.Sleep(3000);
            set.Duplex = Duplex.Simplex; //It seems setting back printer to normal wasn't thoroughly tested though 
            if (false == P.CloseMainWindow())
                P.Kill();
            //kill process
        }

因此,基本上,当您创建 PrinterSettings 时,它似乎保存在内存中的某个位置并用于下一次打印。它对我来说非常有效。唯一需要注意的是,您必须有一个默认应用程序才能阅读 PDF(例如 Adob​​e Reader),这可以在控制面板->默认应用程序中轻松完成。

于 2018-05-24T18:27:45.963 回答