Print document with trays setting in the words

Hi there,

I am trying to print a document with printer trays are setup in the docx. When I print the document it is always printed in the default tray. Attachment is my document
And below is how I print:

String printerName = "\\\\printerserver\\my_printer_name";
PrinterJob pj = PrinterJob.getPrinterJob();
PrintService[] service = PrinterJob.lookupPrintServices();
for (PrintService printService : service) {
	if (printService.getName().contains(printerName)) {
		pj.setPrintService(printService);
		break;
	}
}
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
Document docout = new Document(dataDir + "test2.docx");
AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(docout);
pj.setPageable(awPrintDoc);
pj.print(attributes);

document.zip (8.9 KB)

@bangngo1508

Microsoft Word stores printer specific values for paper trays in a Word document and therefore, only printing on the same printer model as the one that was selected when the user specified the paper trays will result in printing from the correct trays. If you print a document on a different printer, then most likely the default paper tray will be used, not the trays specified in the document.

You can try the following code example to get the desired output.

Document doc = new Document(MyDir + "in.docx");

System.Drawing.Printing.PrinterSettings settings = new System.Drawing.Printing.PrinterSettings();

// The paper tray value stored in documents is completely printer specific. This means
// The code below resets all page tray values to use the current printers default tray.
// You can enumerate PrinterSettings.PaperSources to find the other valid paper tray values of the selected printer.

foreach (Section section in doc.Sections)
{
    section.PageSetup.FirstPageTray = settings.DefaultPageSettings.PaperSource.RawKind;
    section.PageSetup.OtherPagesTray = settings.DefaultPageSettings.PaperSource.RawKind;
}

doc.Print(settings);

Dear @tahir.manzoor,

I am using Words for Java. I also print with the printer that I use to setup trays for the document. I want to have a general solution because it is not always 1 printer in our company

Thanks

@bangngo1508

Please note that MS Word’s behavior for paper tray value is completely implementation specific.

If you create a document in Microsoft Word and set paper tray values, the values stored in the document will be for the currently selected (or default maybe) printer. For example, the value of 292 will be stored inside the Word document.

Then, when you take this document to another computer that has a different printer installed and try to print it (or just check in Page Setup to see what paper tray is selected), you will observe that the paper tray is not preserved! It is reset to a default value.

Following code example shows how to set paper tray information.

Document doc = new Document(MyDir + "in.docx");

// Find the printer that will be used for printing this document. In this case it is the default printer.
// You can define a specific printer by using PrintServiceLookup.lookupPrintServices.
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
Media defaultTray = (Media) printService.getDefaultAttributeValue(Media.class);

// The paper tray value stored in documents is completely printer specific. This means
// The code below resets all page tray values to use the current printers default tray.
// You can enumerate getSupportedAttributeValues for Media type to find the other valid
// paper tray values of the selected printer.
for (Section section : doc.getSections()) {
    section.getPageSetup().setFirstPageTray(defaultTray.getValue());
    section.getPageSetup().setOtherPagesTray(defaultTray.getValue());
}

Dear @tahir.manzoor,

Is there any ways to map tray that setup in the template to the printer instead of default trays?. Assume I use only 1 printer and always run the code on 1 machine.

Thanks

@bangngo1508

Please note that MS Word’s behavior for paper tray value is completely implementation specific.

What this all means is that you can only expect the paper tray to be correctly handled by Microsoft Word when printing on the same printer model as the one that was selected when the paper trays were selected.

There are no “universal” or “well known” values for paper trays stored in Word documents. To fix the problem and print to the correct paper trays, please set the value of PageSetup.FirstPageTray and OtherPagesTray properties to simple integers. You can use Java code to get the printer’s paper source information.