5

I want to print a .docx file silently and being able to choose the tray of the printer.

At first I tried to print the .docx with the Microsoft.Office.Interop.Word but word is opening...

After I converted the .docx file to an image and printed it with ProcessStartInfo but it shows a printing window to the user.

ProcessStartInfo info = new ProcessStartInfo(imageFilePath);

            info.Verb = "Print";
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            Process.Start(info);

I tried another method it print the image silently BUT the image is blured and not scaled correctly.

            PrinterSettings settings = new PrinterSettings();
            string defaultPrinter = settings.PrinterName;

            FileInfo fileInfo = new FileInfo(imageFilePath);


            PrintDocument pd = new PrintDocument();
            pd.DocumentName = fileInfo.Name;

            pd.PrintPage += (sender, args) =>
            {
                Image i = Image.FromFile(imageFilePath);
                PrintPageEventArgs arguments = args;


                System.Drawing.Rectangle m = new System.Drawing.Rectangle()
                {
                    Y = 0,
                    X = 0,
                    Location = new System.Drawing.Point(0, 0),
                    Height = args.MarginBounds.Height,
                    Size = args.MarginBounds.Size,
                    Width = args.MarginBounds.Width
                };



                if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height)
                {
                    m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
                }
                else
                {
                    m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
                }
                args.Graphics.DrawImage(i, m);
            };

            pd.Print();

So is it possible to print a .docx silently and being able to choose the tray of the printer ?

Did anyone face the same issue. Any help in this regard. Thanks in advance.

xqlimax
  • 115
  • 3
  • 7
  • An idea would be to convert the docx to pdf then send to the printer. Maybe the printer would better manage a pdf. This is a simple guess. – aloisdg Oct 25 '19 at 12:11
  • You can [hide the word instance](https://learn.microsoft.com/en-us/office/vba/api/word.application.visible). – Longoon12000 Oct 25 '19 at 12:20
  • I tried to convert it in pdf but I don't know why there is an extra margin on top and on left of the pdf document. – xqlimax Oct 25 '19 at 13:31

2 Answers2

1

I did something very similar to this myself but I never looked up the documentation if you could choose the tray. I believe these are set on the print server itself (if you are using one) and would be able to reference those if your application has the access rights.

string PrinterName = @"\\Server\nameOfThePrinter";
            ProcessStartInfo printProcessInfo = new ProcessStartInfo()
            {
                Verb = "PrintTo",
                CreateNoWindow = true,
                FileName = pdfFileName,
                Arguments = "\"" + PrinterName + "\"",
                WindowStyle = ProcessWindowStyle.Hidden
            };

            Process printProcess = new Process();
            printProcess.StartInfo = printProcessInfo;
            printProcess.Start();
            printProcess.WaitForInputIdle();

            printProcess.WaitForExit(10000);

            if (printProcess.HasExited)
            {

            }else
            {
                printProcess.Kill();
            }

            return true;

Also, you may want to investigate this article here https://www.codeproject.com/Tips/598424/How-to-Silently-Print-PDFs-using-Adobe-Reader-and

Cheers!

  • It works but I cannot choose the printer tray with this method... – xqlimax Oct 25 '19 at 13:33
  • Correct. I do not know the argument required to specify paper tray which is a problematic case. If you want another method check out this other poster https://stackoverflow.com/questions/8943312/changing-printer-trays-during-print-job – Jim Ciccone Oct 25 '19 at 13:42
  • Thanks for the link ! I'm able to choose an array now even if the document is not printed correctly it's a good progress ! – xqlimax Oct 25 '19 at 13:58
0

I found a solution I couldn't print a .docx silently so I converted it as a .png image before.

Link to convert .docx to .png

Here is the code to print the image :

            PrinterSettings settings = new PrinterSettings();
            string PrinterName = settings.PrinterName;

            //set paper size
            PaperSize oPS = new PaperSize
            {
                RawKind = (int)PaperKind.A4
            };
            //choose the tray here
            PaperSource oPSource = new PaperSource
            {
                RawKind = (int)PaperSourceKind.Upper
            };
            PrintDocument printDoc = new PrintDocument
            {
                PrinterSettings = settings,
            };
            //set printer name here it's the default printer
            printDoc.PrinterSettings.PrinterName = PrinterName;
            printDoc.DefaultPageSettings.PaperSize = oPS;
            printDoc.DefaultPageSettings.PaperSource = oPSource;

            printDoc.PrintPage += new PrintPageEventHandler((sender, args) =>
            {
                System.Drawing.Image img = System.Drawing.Image.FromFile(imageFilePath);
                int printHeight = (int)printDoc.DefaultPageSettings.PrintableArea.Height;
                int printWidth = (int)printDoc.DefaultPageSettings.PrintableArea.Width;
                int leftMargin = 0;
                int rightMargin = 0;

                args.Graphics.DrawImage(img, new System.Drawing.Rectangle(leftMargin, rightMargin, printWidth, printHeight));
            });

            printDoc.Print();
            printDoc.Dispose();
xqlimax
  • 115
  • 3
  • 7