在双工模式下打印pdf从C#

时间:2012-07-25 19:59:01

标签: c# pdf printing

我有一个pdf文件,我想从我的C#应用​​程序打印。 我能够在代码中找出打印机是否具有双面功能,但是从我的代码中获取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();
            }

2 个答案:

答案 0 :(得分: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) { }      
}

答案 1 :(得分: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(例如Adobe Reader),这在控制面板中很容易完成 - &gt;默认应用程序。

相关问题