You can use following VC++ source code to retrieve paper size information from a special printer,
// Function that enumerates all available paper forms or paper size:
void FillPrinterPapers()
{
int nPaperCount = 0;
WORD* lpPapers = NULL;
char* lpPaperNames = NULL;
POINT* lpPaperSizes = NULL;
HANDLE hPrinter = NULL;
if (OpenPrinter(PRINTER_NAME, &hPrinter, NULL))
{
DWORD dwNeeded = 0;
// get printer forms
nPaperCount = DeviceCapabilities(PRINTER_NAME, NULL, DC_PAPERS, NULL, NULL);
if (nPaperCount)
{
lpPapers = new WORD[nPaperCount];
ZeroMemory(lpPapers, sizeof(WORD)*nPaperCount);
DeviceCapabilities(PRINTER_NAME, NULL, DC_PAPERS, (LPSTR) lpPapers, NULL);
lpPaperNames = new char[nPaperCount*64];
ZeroMemory(lpPaperNames, sizeof(char)*64*nPaperCount);
DeviceCapabilities(PRINTER_NAME, NULL, DC_PAPERNAMES, lpPaperNames, NULL);
lpPaperSizes = new POINT[nPaperCount];
ZeroMemory(lpPaperSizes, sizeof(POINT)*nPaperCount);
DeviceCapabilities(PRINTER_NAME, NULL, DC_PAPERSIZE, (LPSTR) lpPaperSizes, NULL);
}
ClosePrinter(hPrinter);
}
else
{
// report error
}
}
// Function that sets current paper form:
BOOL SetPrinterPaper(short dmPaperSize, const char* pszFormName)
{
BOOL bResult = FALSE;
HANDLE hPrinter = NULL;
if (OpenPrinter(PRINTER_NAME, &hPrinter, NULL))
{
DWORD dwNeeded = 0;
GetPrinter(hPrinter, 2, NULL, 0, &dwNeeded);
if (dwNeeded > 0)
{
BYTE* lpPrinterInfo = new BYTE[dwNeeded];
if (GetPrinter(hPrinter, 2, lpPrinterInfo, dwNeeded, &dwNeeded))
{
DEVMODE* pDevMode = ((PRINTER_INFO_2*) lpPrinterInfo)->pDevMode;
pDevMode->dmPaperSize = dmPaperSize;
lstrcpy((char*) &pDevMode->dmFormName, pszFormName);
SetPrinter(hPrinter, 2, lpPrinterInfo, 0);
bResult = TRUE;
}
else
{
// report error
}
delete [] lpPrinterInfo;
}
ClosePrinter(hPrinter);
}
else
{
// report error
}
return bResult;
}
One Reply to “How to change current paper form?”