Free Support Forum - aspose.com

BUG Printing to specific PaperTrays

Picture the seen:
I have a 10 page Word document (.DOC specifically, but probably all) with different sections and different First Page / Other Pages printer trays setup. I want to ignore the page setup of the document and specify the first page from Tray 2 and the other pages from Tray 3 in Duplex.
This isn’t too difficult and the following code should work:

Aspose.Words.Document doc = new Aspose.Words.Document(this.File.FullName, loadOptions);
//Set the document up to print on Headed
for (int s = 0; s < doc.Sections.Count; s++)
{
doc.Sections[s].PageSetup.FirstPageTray = (int)HeadedTray;
doc.Sections[s].PageSetup.OtherPagesTray = (int)HeadedTray;
}
printerSettings.PrintRange = System.Drawing.Printing.PrintRange.SomePages;
printerSettings.FromPage = 1;
printerSettings.ToPage = 2;
doc.Print(printerSettings);

//Set the document up to print on Plain
for (int s = 0; s < doc.Sections.Count; s++)
{
doc.Sections[s].PageSetup.FirstPageTray = (int)PlainTray;
doc.Sections[s].PageSetup.OtherPagesTray = (int)PlainTray;
}
printerSettings.PrintRange = System.Drawing.Printing.PrintRange.SomePages;
printerSettings.FromPage = 3;
printerSettings.ToPage = 10;
doc.Print(printerSettings);

This doesn’t work and the document prints from the paper trays specified in the document.

However if I replace the 2 occurences of code line:

doc.Print(printerSettings);

with

Aspose.Words.Document cloneDoc = doc.Clone();
cloneDoc.Print(printerSettings);

then this works fine.

If you have any issues recreating this issue then let me know.
Cheers,
Tom

@TRobson,

Please ZIP and upload your sample input Word document you are getting this problem with here for testing. We will investigate the issue on our end and provide you more information.

Multi-Section and Page Test Doc.zip (12.6 KB)

@TRobson,

Can you please run the following code on your end and share the output here for our reference?

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

foreach (PaperSource source in settings.PaperSources)
    Console.WriteLine(source.RawKind);

I can just tell you that, with some extra details:

15 - " Automatically selected"
257 - " Printer auto select"
258 - " Manual Feed in Tray 1"
259 - " Tray 1"
260 - " Tray 2"
261 - " Tray 3"
1285 - “Unspecified”
1284 - “Plain”
1513 - “HP LaserJet 90g”
…then 30 odd with RawKind greater than 1000…
0 - “USERTYPE1”
0 - “USERTYPE2”
0 - “USERTYPE3”
0 - “USERTYPE4”
0 - “USERTYPE5”
and the trays I am using are 260 and 261, Tray 2 and 3.

I’ve dug about a bit more and it appears that duplex etc. are all red herrings but looking at the documents PageCount property is critical in recreating this issue, e.g.:

if (doc.PageCount < 2) { }

So if you set up a simple document with 2 pages and set the first page to Tray X and the other pages to Tray Y then run the following code you should see the BUG.

PrinterSettings printerSettings = new PrinterSettings();
Aspose.Words.Document doc = new Aspose.Words.Document(“TestDoc.docx”);
if (doc.PageCount < 2) { }
foreach (Aspose.Words.Section section in doc.Sections)
{
section.PageSetup.OtherPagesTray = section.PageSetup.FirstPageTray;
}
doc.AcceptAllRevisions();
doc.Print(printerSettings);
Aspose.Words.Document newDoc = doc.Clone();
newDoc.Print(printerSettings);

but if you remove the highlighted line then it works fine.

@TRobson,

For the sake of any correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-17348. We will further look into the details of this problem and will keep you updated on the status of this issue. We apologize for your inconvenience.

Where you able to recreate the issue?
And will I be alerted when the issue is resolved?

@TRobson,

We had logged an investigation ticket (WORDSNET-17348). I am afraid, at the moment we do not have a physical printer with so many trays. Will you be able to list step by step procedure to reproduce this issue on our end when using some soft printer instead?

Yes, we will inform you via this thread about the progress of this ticket.

You only need 2 trays to test it and the code I provided will recreate the issue in as short a format as I can think of.

@TRobson,

Thanks for the additional information. We have logged your comment in our issue tracking system and will keep you informed of further updates.

@TRobson,

Regarding WORDSNET-17348, it is to update you that we have completed the analysis of this issue. The problem occurs because PageTray is set to the document but its layout is already cached (during PageCount calculating) and will be used for printing. To get correct result we need to invalidate/rebuild layout (call UpdatePageLayout)

According to UpdatePageLayout description. – “if you modify the document after rendering and then attempt to render it again - Aspose.Words will not update the page layout automatically. In this case you should call UpdatePageLayout before rendering (printing) again.”

So, we do not think that we are going to fix this as this is an expected behavior.

Please try using the following workaround:

PrinterSettings printerSettings = new PrinterSettings();
Document doc = new Document("TestDoc.docx");

if (doc.PageCount < 2) { } // highlighted line

foreach (Aspose.Words.Section section in doc.Sections)
{
    section.PageSetup.OtherPagesTray = section.PageSetup.FirstPageTray;
}

// Just call UpdatePageLayout to fix the problem.
doc.UpdatePageLayout();

doc.Print(printerSettings);
Document newDoc = doc.Clone();
newDoc.Print(printerSettings);

Just call UpdatePageLayout after setting new PagesTray to fix the problem. Hope, this helps.