How to set default tray in printer?

I want to set default tray while printing docx.
For now, I have below code where I am passing tray name in string and that will be set as default tray

Microsoft.Office.Interop.Word.Application WordApplication = new WordApplication();
var options = WordApplication.Options;
if (!string.IsNullOrWhiteSpace(trayName)) //trayName is input parameter in string
{
     options.DefaultTray = trayName;
}

Similarly, how can I set default tray in the aspose for all the documents?

Also, I come across with the link FirstPageTray
where the value setting in FirstPageTray and OtherPageTray is in int data type, but I am passing tray name in string as input. I hope I am going in the right direction of setting DefaultPageTray.

@Dhanashri0709 You are right you should use PageSetup.FirstPageTray and PageSetup.OtherPagesTray properties to set the paper tray that will be used upon printing. Different printer has different paper trays. You can get the available trays of your printer using the following simple code and use PaperSource.RawKind to set PageSetup.FirstPageTray and PageSetup.OtherPagesTray.

PrinterSettings settings = new PrinterSettings();
settings.PrinterName = "Your printer name";

foreach (PaperSource src in settings.PaperSources)
{
    Console.WriteLine("{0} - {1}", src.SourceName, src.RawKind);
}

I got you. Do you mean tray name should be integer right?

Also, do we required foreach to iterate over all the sections to set tray name or do we have any other shortcut

@Dhanashri0709

Yes, you should use PaperSource.RawKind integer value for PageSetup.FirstPageTray and PageSetup.OtherPagesTray properties.

Yes, you should lop through all sections in the document and set the appropriate PageSetup.FirstPageTray and PageSetup.OtherPagesTray for each section.

@alexey.noskov
I have checked in printerSettings.DefaultPageSettings.PaperSource.RawKind I am getting value 15 but in tray name user is passing Tray 1 or LCT.

  1. How can I now pass this value?
  2. Is there any way to get tray names from printer and theirs respecting integer value so that I can pass this to FirstPageTray and OtherPageTray

@Dhanashri0709 You can use the following code to get paper source raw kind by it’s name:

private static int GetPaperSourceRawKindByName(PrinterSettings settings, string name)
{
    foreach (PaperSource src in settings.PaperSources)
    {
        if (src.SourceName == name)
            return src.RawKind;
    }

    // Return the default if paper source is not found by the specified name.
    return settings.DefaultPageSettings.PaperSource.RawKind;
}

@alexey.noskov
I have set printer name but still I am getting count zero in settings.PaperSources

@Dhanashri0709 Please correct me if I am wrong, but looks like you are using “Microsoft Print to PDF” printer. This is to file printer so setting paper tray will not have any effect on the document printing to file.

@alexey.noskov
Yes, I changed my default printer and now I can see tray name and their rawkind.

I must say you are such quick resolver of the problems. I really appreciate and thank you so much :slight_smile:

I will proceed further with implementations

1 Like

@alexey.noskov Hi
When I am using PrinterSettings.PrintToFile = true; PrinterSettings.PrintFileName = "NewDocx" I am expecting in new entry will get inserted with the name NewDocx(Whatever name of file I will provide) in printer queue, but I am not able to see new queue entry in printer. I hope am in right direction.

Without above code if I am printing docx I can see queue entry in printer.

@Dhanashri0709 You should pass the document name into the Print method as a paramenter.

Document doc = new Document(@"C:\Temp\in.docx");

PrinterSettings settings = new PrinterSettings();
settings.PrinterName = "Microsoft XPS Document Writer";
settings.PrintFileName = @"C:\Temp\out.xps";
settings.PrintToFile = true;

doc.Print(settings, "This is my cool document");

Do we require above code without this also I can see queue entry with given name. or do we need above code if we are modifying document name

@Dhanashri0709 This code is required only if you are printing directly into the file.

@alexey.noskov
I have tried above code and my output file seems generating in the give path but when I am trying to open it and checked the content, I am getting below error screen.

@Dhanashri0709 What printer do you use? If you need to save a document as Word document, you can simply used Document.Save method, it is not required to use printing for creating Word documents using Aspose.Words.

@alexey.noskov
Basically, I am converting introp code into aspose.
Below is the old code

OutputFileName can be any file which is passing input parameter by user. I am using “Microsoft Print To PDF”. Previously I was using “Microsoft XPS Document Writer” but I was not understanding what file extension I shall provide for this printer

Update:
I have use OutputFileName file as pdf extension then I am able to open the document with “Microsoft Print To PDF”

@Dhanashri0709 For saving documents in the formats supported by Aspose.Words you should use Document.Save method. IT is not required to use Document.Print method for conversion the document to PDF or XPS.

Microsoft Print To PDF saves the printed document as PDF, so the extension of the file should be .PDF.

Microsoft XPS Document Writer saves the printed file as XPS, so the extension should be either .XPS or .OXPS.

1 Like