Document.Print with different Paper Sources does not work using .NET

Changing a tray from one to another doesn’t seem to work. Actually I can set the tray only once per application life time. Here is a very simple console app extract to demonstrate my problem:

                public void PrintDoc() 
                {
                  Stream inputstream; //code for loading ommited for brevity
                  var doc = new Document(inputstream);
                  var printerSettings = new PrinterSettings();
                  printerSettings.PrinterName = "Kyocera ECOSYS M3550idn fib";
                  printerSettings.DefaultPageSettings.PaperSource = printerSettings.PaperSources[3];
                  doc.Print(printerSettings);
                }
                
            public void PrintCopy() 
            {
              Stream inputstream; //code for loading ommited for brevity
              var doc = new Document(inputstream);
              var printerSettings = new PrinterSettings();
              printerSettings.PrinterName = "Kyocera ECOSYS M3550idn fib";
              printerSettings.DefaultPageSettings.PaperSource = printerSettings.PaperSources[1];
              doc.Print(printerSettings);
            }

If I run “PrintDoc” everything is work as expected and the document is leaving the intended tray. But if I run “PrintCopy” after that, it is printed on the same tray (3), and not on the configured tray (1).
I need to restart the console app to call “PrintCopy”, to have it printed on the correct tray. But then I have the same problem vice versa: If I run “PrintDoc” it is printed on tray 1 and not tray 3, as it was configured. Then again, I need to restart the console app to have the correct tray applied.

How can that be? Everything is “newed” and even the documents are different documents.

@Grenz

Please make sure that the PrinterSettings has correct values that you pass to Document.Print method.

You are passing new instance of PrinterSettings to each method. Please try to use same instance of PrinterSettings in both methods. Hope this helps you.

Hi Tahir,

thanks for replying :slight_smile:

They are supposed to create their own instances because they are both supposed to print on different trays (paper sources).
What would I achieve by passing the same printersettings two both methods?

@Grenz

Please note that Aspose.Words uses .NET printing API to print the document. Aspose.Words reads values from document in Raw format (i.e. as int) and after Print method is called, a function compares the Raw value of PaperTray of current page and values in PrinterSettings.PaperSources and if it does not find coincidence, function returns “Automatic” value. Then it passes value for PaperTray to underlying Windows function that actually sends document to printer.

Also, note that paper tray numbers are printer specific. I suppose the problem might occur because paper trays numbers specified in your document do not match paper tray numbers of the printer running on your local environment. So, first of all you should make sure that paper trays in your document are specified correctly.

In Microsoft Word printer trays are defined per section and are printer specific. Therefore you can programmatically change these values by using the FirstPageTray and OtherPagesTray properties of the PageSetup class for each section. You can set the same source to these properties. Hope this helps you.

@tahir.manzoor

Sorry, but I have the feeling that you haven’t understood the “real” problem. :blush:

The printer settings, which I presented in the code are correct. As I said:

  • If I call the method “PrintDoc()” , the correct tray (1) is used for printing.
  • If I restart the program and then call “PrintCopy()”, the correct tray (3) is used for printing
  • But if I DON’T restart the program, the printersettings selected for the first print task are being remembered and reused for ALL following print tasks.

It doesn’t matter if I select any other tray in other print task, it ALWAYS remains on the tray selected during the first print task.
So if call “PrintDoc()” and after that want to call “PrintCopy()” for the same or any other document, I have to restart the program, because else it would still use tray number 3, although “PrintCopy()” defined tray number 1 (as you can see in the code).
I don’t have this problem in Aspose.Cells, only in Aspose.Words. So my guess is, that this is not an issue by the underlying windows api.

I can test defining the tray per section, if you reckon that this would be more reliable. The documents I am printing consist of just one page anyway.

@Grenz

We understood your scenario. Document.Print method uses .NET printing API and uses defined paper sources. So, there should not be an issue.

Could you please debug or print the PaperSources before calling Document.Print method? Please make sure that print method uses the correct paper source.

Are you facing this issue with specific document? Please check it with some other documents.

Please also share if you face this issue only at specific system and printer. Please check this scenario at different system and let us know if you still face the same issue. Thanks for your cooperation.

If you still face problem, please ZIP and attach your input Word documents for testing purpose.