Print pdf in duplex mode From C#

ⅰ亾dé卋堺 提交于 2021-01-29 05:52:03

问题


I have a pdf file that I would like to print from my C# app. I was able to figure out in code if the printer is duplex capable but get the pdf to print in duplex from my code. Here is my code for regular simplex printing. I was able to check the metadata of the pdf's print dialog preset to duplex. but it does not work.

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

回答1:


after much research, I wrote this code which is working for me. This is the neat copy of the cope, I could write much research and fiddle around. I couldn't find a better solution and post my work to help others. I am printing a multipage tiff file, but this code also works for a 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) { }      
}



回答2:


I don't know if it's right to post an answer given time passed, but i struggled a lot with this question and turns out the code is simpler than we all thought:

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
        }

So basically it seems as you create the PrinterSettings its saved somewhere in memory and used in the next printing. It worked perfectly for me. Only thing important to note is that you MUST have a default application to read PDF (Adobe Reader for example), this is done easily in Control Panel-->Default Applications.



来源:https://stackoverflow.com/questions/11657651/print-pdf-in-duplex-mode-from-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!