Hi Alexey,
thank you for your response!
I am aware of the specificity of the tray settings to certain printers. But this is not the reason for my issue. Please refer to the note at the end of my original post. The whole process of creating the document, manipulating it from Aspose.Words and printing it has been done using my default Windows printer.
To clarify this, here again my basic scenario (using “Manual Paper” tray as example, would be any of the installed trays in production environment):
-
Create template document using MS Word and setting tray for first and other pages in the first section (it only has one) to “Manual Paper”. Printing the document from MS Word uses “Manual Paper”. Ok.
-
Reading the template into my application using Aspose.Words. Doing some manipulation (replacing of some values).
-
Printing the document using Document.Print(): Does NOT use “Manual Paper”, but uses the “Automatic” tray. Not Ok.
-
Saving the Aspose.Words document. Opening this document with MS Word and printing: Uses “Manual Paper” (and also can see the setting in the Word Page Setup). Ok.
So, in conclusion, my issue is that Step #3 ignores the setting in the Word document when printing, but correctly writes this setting into the Word file.
This is not consistent behaviour and it makes it impossible for me to achieve what I want to achieve: Users should be able to specify trays for first and other pages for each section in the template document and my application should then print each page to the appropriate tray.
My second scenario is a workaround: I am trying to find to at least be able to offer some selection option to my users for paper trays. This workaround defines a PrinterSettings object, sets its PaperSource to “Manual Paper” and uses Document.Print(MyPrinterSettings).
Doing this instead of Step #3 above also fails (see my above post)! Whatever I specify in MyPrinterSettings is ignored and printing is to the “Automatic” tray!
But as I also pointed out above, there is a way to make this work! And this is to not specify any tray in the template document. If I define “Standard Tray” in the template document, MyPrinterSettings are used and printing is to “Manual Paper” using Document.Print(MyPrinterSettings) as it should.
This is what I was referring to above as really not consistent behaviour of Aspose.Words.
In order to get some more information on what is going on interally, I have used the AsposeWordsPrintDocument object. Key elements of my code look like this (with MyWorkDocument being the template document after reading in and doing some manipulations):
Dim MyWorkDocument As Aspose.Words.Document
Dim MyPrinterSettings As System.Drawing.Printing.PrinterSettings
Dim MyPaperSource As New System.Drawing.Printing.PaperSource
Dim MyPrintDialog As System.Windows.Forms.PrintDialog = New System.Windows.Forms.PrintDialog
MyPrintDialog.ShowDialog()
MyPrinterSettings = MyPrintDialog.PrinterSettings
MyPaperSource = MyPrinterSettings.PaperSources(5)
MyPrinterSettings.DefaultPageSettings.PaperSource = MyPaperSource
MyPrintDocument As Aspose.Words.Rendering.AsposeWordsPrintDocument
MyPrintDocument = New Aspose.Words.Rendering.AsposeWordsPrintDocument(MyWorkDocument)
MyPrintDocument.PrinterSettings = MyPrinterSettings
AddHandler MyPrintDocument.PrintPage, AddressOf PrintPage
MyPrintDocument.Print()
...
Private Sub PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Console.WriteLine(e.PageSettings.PaperSource.RawKind)
End Sub
With this code I get the following results:
Paper sources in MyPrinterSettings.PaperSource are:
MyPrinterSettings.PaperSources(0)
{System.Drawing.Printing.PaperSource}
Kind: FormSource {15}
RawKind: FormSource {15}
SourceName: "Automatisch auswählen"
MyPrinterSettings.PaperSources(1)
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: Custom {257}
SourceName: "Auto Select"
MyPrinterSettings.PaperSources(2)
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 258
SourceName: "Tray 1"
...
MyPrinterSettings.PaperSources(5)
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 261
SourceName: "Manual Paper"
...
Case 1 (Template document has set the tray to "Manual Paper", i.e. the scenario described in the beginning):
Before executing MyPrintDocument.Print() I have the following:
MyWorkDocument.Sections(0).PageSetup.FirstPageTray
261
MyPrinterSettings.DefaultPageSettings.PaperSource
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 261
SourceName: "Manual Paper"
MyPrintDocument.PrinterSettings.DefaultPageSettings.PaperSource
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 261
SourceName: "Manual Paper"
After executing MyPrintDocument.Print(), monitoring the MyPrintDocument.PrintPage event yields:
e.PageSettings.PaperSource
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: Custom {257}
SourceName: "Auto Select"
> Print goes to “Automatic tray”. Not Ok.
Case 2 (Template document has set the tray to “Standard tray”):
Before executing MyPrintDocument.Print() I have the following:
MyWorkDocument.Sections(0).PageSetup.FirstPageTray
DefaultBin {0}
MyPrinterSettings.DefaultPageSettings.PaperSource
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 261
SourceName: "Manual Paper"
MyPrintDocument.PrinterSettings.DefaultPageSettings.PaperSource
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 261
SourceName: "Manual Paper"
After executing MyPrintDocument.Print(), monitoring the MyPrintDocument.PrintPage Event yields:
e.PageSettings.PaperSource
{System.Drawing.Printing.PaperSource}
Kind: Custom {257}
RawKind: 261
SourceName: "Manual Paper"
> Print goes - as it should - to “Manual Paper”. Ok.
I hope this clarifies the situation. My suspicion is that Aspose.Words has a problem if the PageSetup of the Word document specifies a tray (in my example 261) which is not contained in the System.Drawing.Printing.PaperSource Enumeration. If this happens, RawKind is set to 257 irrespective of the settings in the PrinterSettings. These are only taken account of if the Word document specifies a tray contained in the System.Drawing.Printing.PaperSource Enumeration (or even only if it is set to DefaultBin).
So, in essence I am reporting two issues here:
-
Document.Print ignores page setup in the Word document (at least for trays with RawKind above 257).
-
Document.Print (MyPrinterSettings) ignores the defined paper source if the page setup in the Word document specifies a non-standard tray (or at least a tray with RawKind above 257).
For Issue #2 there is a workaround by not making any selection of paper sources in my template document. So, I can get this to work (but I still would see this as some kind of bug).
But this workaround still does not give me the full possibilities which MS Words offers in printing (defining different paper sources for different sections and for first and other pages in a section). This would only be possible by Issue #1 being solved.
The only workaround here would be to save the document with Aspose.Words and then open it through Word Automation and print it from there. But this is not really an approach I would like to take because the whole issue of buying a Aspose.Words license was to get rid of Word Automation which I was using before …
Best regards,
Wolfgang