We're sorry Aspose doesn't work properply without JavaScript enabled.

# Printing from trays in Word docs from v8.0.0

Reading in a word doc or docx with a Tray 2 setting causes this error when trying to print:

Error printing with Aspose: System.InvalidOperationException: Cannot convert '260'.
at ؒ.ါ.ူ(Hashtable ေ, Object ဲ, Object ཬ)
at ؒ.ါ.ူ(Hashtable ေ, Object ဲ)
at ✚.䋆.䋝(PaperTray Ӷ)
at ✚.䋆.◄(㺵 䋒, ӟ 䉬)
at Aspose.Words.Document.ԋ(Int32 Ԍ)
at Aspose.Words.Document.RenderToScale(Int32 pageIndex, Graphics graphics, Single x, Single y, Single scale)
at Aspose.Words.Rendering.AsposeWordsPrintDocument.OnPrintPage(PrintPageEventArgs e)
at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e)
at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document)
at System.Drawing.Printing.PrintController.Print(PrintDocument document)
at System.Drawing.Printing.PrintDocument.Print()
at Aspose.Words.Document.Print(PrinterSettings printerSettings, String documentName)
at Aspose.Words.Document.Print(PrinterSettings printerSettings)
at Aspose.Words.Document.Print()
at AMSPrint.Form1.PrintFileWithAsposeClick(Object sender, EventArgs e) in C:\AmsDev\AMSPrint\AMSPrint\Form1.cs:line 177


My code is simply:

try
{
var doc = new Document("C:\\temp\\TestTrays.docx");
doc.Print();
}
catch (Exception e2)
{
AppendText("Error printing with Aspose: " + e2);
}


Or:

try
{
var doc = new Document("C:\\temp\\TestTrays.docx");
var pd = new AsposeWordsPrintDocument(doc);
pd.Print();
}
catch (Exception e2)
{
AppendText("Error printing with Aspose: " + e2);
}


Hi

Best regards,

Attached.

Thanks,

Doug

Hi

Thank you for additional information. I managed to reproduce the problem on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.

Best regards,

Hi Andrey,

Are you able to give me any work around in v7 or v8 that might work for printing from different trays for existing docs? Sorry, but starting to get desperate.

Cheers,

Doug

Hi

Thanks for your request. The exception should not occur with 7.0.0 version. So as a temporary workaround, you can try downgrading to Aspose.Words 7.0.0.

Best regards,

We’ve been trying for the last month with V7 and previously with V6.6. It won’t crash, but we can’t get the trays to print correctly from the docs at all.

I had sent through the test code but had sent the v6.6 version by mistake
https://forum.aspose.com/t/82271

Was really hopeful when I saw the V8 release notes re the trays.

Hi Doug,

Thank you for additional information. In Aspose.Words 8.0.0 print tickets were added to generated XPS with paper size, orientation and paper tray information. The issue you have reported earlier is still unresolved. You will be notified once it is fixed.

Best regards,

Apologies Andrey, but I need to persue this just a little more.

Is it possible with v7 to read and print the attached doc? It simply is set to print from Tray 1 then Tray 2, Tray1 and Tray 2.

If you know it is possible, then I’ll keep working on it - if you have code or clues, that would be fantastic.

If you know it’s not possible until the fix for v8 crash is available, I’d like to know that.

Regards,

Doug

Hi Doug,

Thanks for your request. The exception will not occur with Apsose.Words 7.0.0. However, printing from different tray can work improperly, because not all paper trays are supported by Aspose.Words at the moment. Currently Apsose.Words supports printing from tray listed in PaperTray enum.

Your request is already linked to the appropriate issue. You will be notified once printing from trays is fully supported.

Best regards,

Hi Andrey,

Can you give me the name of a printer driver that is known to work with your supported PaperTrays?

Thanks,
Doug

Hi

Thanks for your request. Aspose.Words supports all standard trays which are evaluable by MS Word.

Best regards,

I have the exact same problem, I’m using version 8 and using code like:

Dim doc As New Document("C:\in.doc")
Dim imageStream As New MemoryStream()
doc.SaveToImage(0, 1, imageStream, ImageFormat.Jpeg, Nothing)


Running this code with the attached document results in this error:

• InvalidOperationException, Cannot convert ‘260’

Obviously code missing above, but this will re-create the exception.

I’m pretty sure this is related to printer trays - only happens on Tray 2 or 3. On other documents, I also get other integer values such as "Cannot convert ‘292’).

Hi

Thanks for your inquiry. I managed to reproduce the problem with your document on my side. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved. As a workaround please try using the following code:

Document doc = new Document(@"Test001\in.doc");
foreach (Section section in doc.Sections)
{
section.PageSetup.FirstPageTray = PaperTray.DefaultBin;
section.PageSetup.OtherPagesTray = PaperTray.DefaultBin;
}
doc.SaveToImage(0, 1, @"Test100\out.jpg", null);


Best regards,

Unfortunately, your trays have a Paper Tray Kind of “Custom” which is not working properly yet. I’m hopeful the next release will sort it.

Some print drivers for non-HP printers are still using the older Paper Tray Kinds that Aspose do support.

Hi

Thank you for additional information. Unfortunately, I cannot suggest you any way to work this problem around. The original issue is already resolved in the current codebase. The fix will be included into the next hotfix, which will be released closer to the end of December. You will be notified.

Best regards,

The issues you have found earlier (filed as 11758) have been fixed in this update.

(35)

Thought I’d post again regarding this issue, I’ve downloaded the latest version (8.2.0) and although the exception has been fixed (the cannot convert 260 or whatever integer), the print still does not occur on the correct tray.

This is a major issue for us, and we have found a possible workaround for anyone else encountering this. Essentially the code is this:

Public Class PrintHelper
Private _CurrentPage As Integer = 0
Private _ResizePercent As Single = 0.95
Private _Document As Aspose.Words.Document

Private _TrayInfo As New Dictionary(Of Integer, Integer)
Private _TotalPages As Integer
Private WithEvents _PrintDoc As PrintDocument = Nothing

Public Sub Print(ByVal aspDoc As Document, ByVal PrinterName As String, ByVal Copies As Integer)
Dim myPrintSettings As System.Drawing.Printing.PrinterSettings
'
Try
' get the current page numbers from the document.
For i As Integer = 0 To aspDoc.PageCount - 1
Next
' reset the number to known bins.
For Each sec As Section In aspDoc.Sections
sec.PageSetup.FirstPageTray = PaperTray.DefaultBin
sec.PageSetup.OtherPagesTray = PaperTray.DefaultBin
Next
aspDoc.UpdatePageLayout()
_TotalPages = aspDoc.PageCount
_Document = aspDoc
_CurrentPage = 0
' setup the print settings and print the document.
_PrintDoc = New PrintDocument
myPrintSettings = New System.Drawing.Printing.PrinterSettings
myPrintSettings.FromPage = 1
myPrintSettings.ToPage = aspDoc.PageCount
myPrintSettings.Copies = CShort(Copies)
myPrintSettings.PrinterName = PrinterName
_PrintDoc.PrinterSettings = myPrintSettings
_PrintDoc.Print()
Catch ex As Exception
Throw
Finally
If (_PrintDoc IsNot Nothing) Then
_PrintDoc.Dispose()
End If
End Try
End Sub

Private Sub Doc_QuerySettings(ByVal sender As Object, ByVal e As QueryPageSettingsEventArgs) Handles _PrintDoc.QueryPageSettings
For i As Integer = 0 To e.PageSettings.PrinterSettings.PaperSources.Count - 1
If (e.PageSettings.PrinterSettings.PaperSources(i).RawKind = TrayInfo (_CurrentPage)) Then
e.PageSettings.PaperSource = e.PageSettings.PrinterSettings.PaperSources(i)
End If
Next
' set page options.
e.PageSettings.PaperSize = _Document.GetPageInfo (_CurrentPage).GetDotNetPaperSize
e.PageSettings.PrinterSettings)
e.PageSettings.Landscape = _Document.GetPageInfo(_CurrentPage).Landscape
End Sub

Private Sub Doc_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles _PrintDoc.PrintPage
Dim top As Integer
Dim left As Integer
Dim width As Integer
Dim height As Integer
' set the initial values.
width = e.PageBounds.Width
height = e.PageBounds.Height
' then resize to fit page.
width = CInt(width * _ResizePercent)
height = CInt(height * _ResizePercent)
' position image in middle.
top = CInt((e.PageBounds.Height - height) / 2)
left = CInt((e.PageBounds.Width - width) / 2)
' render the document to the printer.
_Document.RenderToSize(_CurrentPage, e.Graphics, left, top, width, height)
If (_CurrentPage < (_TotalPages - 1)) Then
e.HasMorePages = True
_CurrentPage += 1
Else
' we are at the end.
e.HasMorePages = False
End If
End Sub
End Class


We’re still testing the code (still needs some work), but so far it’s looking pretty good. Each page of the Word document will print to the specified tray, even custom trays.

So for us, on a given document, one page will go to tray 259, the other ones will go to tray 260. Both are custom trays. Using the latest version of Aspose, the document will print, but not to the correct tray (unless I’m missing something in code).

This code works (not sure about that resize either - we’re just testing that) and it is based on an example Aspose provided, but changed around quite a lot.

The biggest thing I noticed is the method on the Aspose document - RenderToSize. Basically I render the document onto the graphics using specified page and dimensions. There is another method which saves a page in the document to a stream using Aspose image options. We tried this method and cranking the DPI up to 600 and although the quality increased, it was never as good as a plain old normal Print using Aspose. Using RenderToSize however gives the same quality.

Hi Tyron,

Thank you for sharing your experience. It is perfect that you find a workaround of the problem. This code seems to be based on the code I provided here:
https://forum.aspose.com/t/86340

Best regards.

Hey Alexey,

The code is very similiar, and that’s the code I based my work on (so thanks, honestly forgot to reference your link!) The key difference is the code your provided called the SaveToImage method.

Whereas I call the RenderToSize method (on Document class). I guess both do similiar things, but RenderToSize draws directly to a graphics object with a specific width and height. Taking scaling into account, the quality was so much better using RenderToSize.

When I used SaveToImage and saved to a memory stream, the quality was not good enough (even with high DPI) for our situation.

The other difference is I set the paper tray in the QueryPageSettings event, rather than the PrintPage event. Seems to make a difference in getting the printer to print to the right tray in that event instead.

Funnily enough, after all that work, one of our clients actually print to a tray based on the paper type of the document (rather than specifying the source for individual pages). So this fix works for some, but not others (but that’s our problem).

Thanks for your guys help though - the structure of your components are top notch and are wonderful to program for - so good work!