I have C++ code that works for generating printer output (quasi/abbreviated code):
CDC dcPrinter;
dcPrinter.Attach(hdcPrinter);
DOCINFO docinfo;
dcPrinter.StartDoc(&docinfo);
dcPrinter.StartPage();
dcPrinter.TextOut(leftMarginPlus, 490, _T("Normal report text"));
dcPrinter.EndPage();
What I need is at the END of a report to switch to DUPLEX mode and then print some fine-print/disclosure on the back of ONLY the last page. I've tried this but the output doesn't switch to duplex ~ it just prints "normal" on another new page:
dcPrinter.EndPage();
DEVMODE* dvmd = dlg.GetDevMode();
GlobalLock(dvmd);
dvmd->dmDuplex = DMDUP_VERTICAL;
dvmd->dmFields |= DM_DUPLEX;
GlobalUnlock(dvmd);
dcPrinter.ResetDCW(dvmd);
dcPrinter.StartPage();
dcPrinter.TextOut(leftMargin, 490, _T("all the Fine-Print"));
dcPrinter.EndPage();
Is it possible during a print to switch to Duplex mode? What do I have wrong?
To switch to duplex mode, you probably need to do it before you start the "page" that'll be on the front side of that piece of paper.
As for the rest of the sequence, you have it correct enough that it'll work fine with a lot of printer drivers. By making your code a bit more pedantic, you can avoid problems with more complex drivers.
When you go fiddling with a DEVMODE, you should:
Drivers Be Buggy
All of the above still may not solve your problem. In my experience, some mid-job DEVMODE changes simply don't work with certain drivers.
A common example is trying to switch between portrait and landscape between pages. It simply doesn't work with some printer drivers but it does with others. And there are some where it mostly works, but certain orientation-dependent values and features, like the offset to the printable region or the device resolution (for printers that don't use "square" pixels), may not all be updated consistently.
Another example is trying to change paper bins between pages. For example, if you wanted the first page of a document to print on custom letterhead fed in from the manual feeder or a secondary paper tray and have the rest of the pages use plain paper from the default paper tray, it would work correctly with many printers but not at all with others.
Working Around Buggy Drivers
On a commercial product I worked on, we maintained a configuration file with lists of printer drivers that did not properly support certain features, even though they claimed to when you queried them through the API or inspected their DEVMODE. When it was time to exercise the feature, the code would consult the exceptions like and use a crude fallback.
The most common fallback was simply to break the job into multiple jobs. If the first few pages were supposed to be in portrait orientation, followed by one page in landscape, and then the rest in portrait, we'd just break it into three print jobs and send them sequentially. This was less than ideal in some circumstances, but broken printer drivers didn't leave us much choice.
One possible hack for duplex printing would be to always create a duplex job, and, when you didn't want to print on the backside, you would just print blank pages. Again, not ideal, but what are you gonna do?