Specifying Paper Tray Stopped Working (.NET 4.7.2 Incompatibility?)

Hello all.

My code for printing to specific pages has been working fine for over a year now. However, I got a bug report from one of my beta testers today that it wasn’t working. Sure enough, it’s not.

I did upgrade the solution to .NET 4.7.2 in this release cycle. However, the previous version of my software (.NET 4.6.1) doesn’t work either anymore. I’m not sure whether I’ll have to completely uninstall 4.7.2 to validate that the old version could still work; hopefully it won’t come to that. I did upgrade some other parts of the application that deal with .docx file generation; so, it could be that too.

Here’s my old code – short and simple:

public void Print(string path, PaperSource paperSource, short copies)
{
 using (AsposeWordsPrintDocument printDoc = new AsposeWordsPrintDocument(new Document(path))
 {
  PrinterSettings = new PrinterSettings { PrinterName = FullName, Copies = copies },
  PrintController = new StandardPrintController(),
 })
 {
  printDoc.DefaultPageSettings.PaperSource = paperSource;
  printDoc.Print();
 }
}

Here’s the code I’m testing. The examples that I found online showed to set the paper source during the QueryPageSettings event. However, everything was still correct at that point in the workflow; so, I didn’t touch anything. It’s not until the PrintPage event when things go sideways, and the event argument’s PaperSource got set to Automatic. I tried to reset the PaperSource property at this point, but it didn’t help. Note that the event argument’s PageSettings property is read-only at this point.

I’d sure appreciate a silver bullet here… Thanks!

public void Print(string path, PaperSource paperSource, short copies)
{
 PaperSource = paperSource;

 using (printDoc = new AsposeWordsPrintDocument(new Document(path))
 {
  PrinterSettings = new PrinterSettings { PrinterName = FullName, Copies = copies },
  PrintController = new StandardPrintController(),
  DefaultPageSettings = new PageSettings { PaperSource = paperSource },
 })
 {
  printDoc.BeginPrint += PrintDoc_BeginPrint;
  printDoc.QueryPageSettings += PrintDoc_QueryPageSettings;
  printDoc.PrintPage += PrintDoc_PrintPage;
  printDoc.EndPrint += PrintDoc_EndPrint;

  printDoc.Print();

  printDoc.BeginPrint -= PrintDoc_BeginPrint;
  printDoc.QueryPageSettings -= PrintDoc_QueryPageSettings;
  printDoc.PrintPage -= PrintDoc_PrintPage;
  printDoc.EndPrint -= PrintDoc_EndPrint;
 }
}

PaperSource PaperSource = null;
AsposeWordsPrintDocument printDoc = null;

private void PrintDoc_BeginPrint(object sender, PrintEventArgs e)
{
 string tray = printDoc.DefaultPageSettings.PaperSource.SourceName; // " Tray2"
 int rawKind = printDoc.DefaultPageSettings.PaperSource.RawKind; // 260
}

private void PrintDoc_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
{
 string tray0 = e.PageSettings.PaperSource.SourceName; // " Tray2"
 int rawKind0 = e.PageSettings.PaperSource.RawKind; // 260
 string tray = printDoc.DefaultPageSettings.PaperSource.SourceName; // " Tray2"
 int rawKind = printDoc.DefaultPageSettings.PaperSource.RawKind;
}

private void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
 string tray0 = e.PageSettings.PaperSource.SourceName; // " Automatically Select" !!!
 int rawKind0 = e.PageSettings.PaperSource.RawKind; // 15 !!!
 string tray = printDoc.DefaultPageSettings.PaperSource.SourceName;// " Tray2"
 int rawKind = printDoc.DefaultPageSettings.PaperSource.RawKind; // 260

 e.PageSettings.PaperSource = PaperSource; // Reset the paper source...

 tray0 = e.PageSettings.PaperSource.SourceName; // " Tray2"
 rawKind0 = e.PageSettings.PaperSource.RawKind; // 260
 tray = printDoc.DefaultPageSettings.PaperSource.SourceName; // " Tray2"
 rawKind = printDoc.DefaultPageSettings.PaperSource.RawKind; // 260
}

private void PrintDoc_EndPrint(object sender, PrintEventArgs e)
{
 string tray = printDoc.DefaultPageSettings.PaperSource.SourceName; // " Tray2"
 int rawKind = printDoc.DefaultPageSettings.PaperSource.RawKind; // 260
}

Edit… I’ve tried this with Aspose.Words versions: 17.10, 18.11 and 19.1. Same behavior in all three.

AJH

@ahilton

Thanks for your inquiry. Please note that 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 doesn’t 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.

Could you please run the following code to determine whether a tray with number 2 actually exists in your printer?

PrinterSettings settings = new PrinterSettings();
settings.PrinterName = "name of your multi-tray printer";

foreach(PaperSource source in settings.PaperSources)
Console.WriteLine(source.RawKind + "-" + source.SourceName);

My paper source values aren’t hard coded. I’m querying for available printers and their trays dynamically. I’m actually testing against two different network printers: a HP M401 and a Sharp MX-3110N.

Here are the sources for the HP:
15- Automatically Select
257- Printer auto select
258- Manual Feed in Tray 1
259- Tray 1
260- Tray 2

Oddly enough for the Sharp printer, I can print to Tray 3, but not Tray 2. I blame gremlins…

I’m just finding it odd that I can configure the PaperSouce correctly in the QueryPageSettings event – like most of the examples I’m finding online – and they get blow away before the PrintPage event.

I’ll look for examples of interacting with FirstPageTray and OtherPagesTray and see what that does.

That was the silver bullet! Here’s my finished working code:

public void Print(string path, PaperSource paperSource, short copies)
{
 Document doc = new Document(path);
 foreach (Section section in doc.Sections.OfType<Section>())
  section.PageSetup.FirstPageTray = section.PageSetup.OtherPagesTray = paperSource.RawKind;

 using (AsposeWordsPrintDocument printDoc = new AsposeWordsPrintDocument(doc)
 {
  PrinterSettings = new PrinterSettings { PrinterName = FullName, Copies = copies },
  PrintController = new StandardPrintController(),
  DefaultPageSettings = new PageSettings { PaperSource = paperSource },
 })
  printDoc.Print();
}

Thank you for the assistance!

@ahilton

Thanks for your feedback. Please let us know if you have any more queries.