Converting a document to XPS and printing to a specific tray

Is it possible to convert a word document (with printer and tray information) to XPS and print it?

This might be a duplicate or followup from Export XPS with support for input tray - #6 by muhammad.ijaz , but in our case everything (generating, saving, printing) happens on the same server, with acces to the printer.


We’re replacing our word-automation with Aspose.
Generating documents seems to work fine, but printing to a printer with tray selections doesn’t seem to work.
The linked issue above seems to conclude that saving to XPS does not include the tray-/bin-information in the printticket.
While Preserve paper tray information when rendering to/printing via XPS says it should work.

We are using the dotnet 20.2 version of Aspose.


These are the steps we use now:

  1. generate a Word document, set the PageSetup.FirstPageTray and OtherPagesTray for all sections to the desired tray
  2. save the word document
    – check the paperSrc in the docx and see the correct tray (262 in this specific case)
  3. supply the word document to a .exe based on your example XpsPrintHelper from Printing a Document in C#|Aspose.Words for .NET
    – debugging learns that pageInfo.PaperTray, section.PageSetup.FirstPageTray and OtherPagesTray all contain the expected tray
  4. sadly the document is printed from the default tray

in XpsPrintHelper the document is saved to a stream as XPS: document.Save(stream, Aspose.Words.SaveFormat.Xps);.
When saving to a file and checking the psk:JobInputBin shows a value of psk:AutoSelect.

@brm

To convert a Word document to XPS and print it while ensuring that the printer uses the correct tray settings, you can follow these steps:

  1. Generate the Word Document: Make sure to set the PageSetup.FirstPageTray and OtherPagesTray properties for all sections in your Word document to specify the desired tray.

  2. Save the Document: After setting the tray properties, save the Word document. You can verify that the paperSrc in the DOCX file reflects the correct tray settings.

  3. Use XpsPrintHelper: When printing, utilize the XpsPrintHelper class to send the document to the printer. This class allows you to print the document directly from a stream after converting it to XPS format.

Here is a code snippet demonstrating how to implement this:

using Aspose.Words;
using Aspose.Words.Rendering;

public void PrintDocument(string filePath, string printerName)
{
    // Load the document
    Document document = new Document(filePath);
    
    // Set the tray settings
    foreach (Section section in document.Sections)
    {
        section.PageSetup.FirstPageTray = 262; // Set to your desired tray
        section.PageSetup.OtherPagesTray = 262; // Set to your desired tray
    }

    // Save the document to a stream in XPS format
    using (MemoryStream stream = new MemoryStream())
    {
        document.Save(stream, SaveFormat.Xps);
        stream.Position = 0; // Reset stream position

        // Print the document using XpsPrintHelper
        XpsPrintHelper.Print(stream, printerName, "My Test Job", true);
    }
}

Important Notes:

  • Ensure that the printer you are targeting supports the specified tray settings.
  • The XpsPrintHelper class is designed to handle the printing process, but you may need to verify that the printer driver is correctly installed and configured to recognize the tray settings.
  • If you encounter issues where the document prints from the default tray, it may be necessary to check the printer’s configuration or the print ticket settings being used.

For further details, you may want to refer to the documentation on printing with Aspose.Words and the specific examples provided in the Aspose GitHub repository.

If you continue to experience difficulties, consider checking related forum discussions for additional insights or solutions.

Sources:
[1]: Print documents.cs

@brm Only few default paper trays are available in public and others are private, custom namespace must be declared for them to use them in XPS print ticket. We have an issue (WORDSNET-12229) for this in our defect tracking system. I linked your request to the appropriate issue. We will keep you updated and let you know once it is resolved.