Tuesday, April 4, 2017

Print Management Dialog X++

Sometimes you might need to open up print dialog using X++ code to let user choose what print medium to be used for printing.

This post shows a code sample where sales invoice report is printed using X++ code but before printing Print Management dialog is prompted to choose destination for print as shown below.



Sample code

Args                args;

ReportRun           rr;
Report              rb;
PrintJobSettings    pjs;
CustInvoiceJour     record;
;

select record where record.RecId == 5637175089;

args = new Args("SalesInvoice");
args.record(record);
args.parmEnum(PrintCopyOriginal::OriginalPrint);

// Set report run properties
rr = new ReportRun(args,'');
rr.suppressReportIsEmptyMessage(true);
rr.query().interactive(false);

// set report properties
rb = rr.report();
rb.interactive(true);

// set print job settings
pjs = rr.printJobSettings();
pjs.fileName(strfmt("C:\\%1.pdf", record.SalesId));
pjs.fitToPage(true);

// break the report info pages using the height of the current printer's paper
pjs.virtualPageHeight(-1);

// force PDF printing
pjs.format(PrintFormat::PDF);
pjs.setTarget(PrintMedium::Mail);
pjs.viewerType(ReportOutputUserType::PDF);

// lock the print job settings so can't be changed
// X++ code int the report may try to change the destination
// to the screen for example but this does not make
// sense when running a report here

pjs.lockDestinationProperties(true);

// Initialize the report
rr.init();

if (rr.prompt())
{
    rr.run();
}

No comments:

Post a Comment