Click here to Skip to main content
Licence 
First Posted 20 Jul 2004
Views 42,594
Bookmarked 12 times

Simple way to get the name of the paper a printer supports

By | 19 Jan 2005 | Article
Use DeviceCapabilities function to get the paper name of a printer.

Sample Image - Printer_Paper_Name.jpg

Introduction

There are lots of articles on Code Project about printing procedure. We think this is a topic which is missing and so decided to give some idea. During one of our projects, we had to select the paper names and size, a particular printer supports. For the time being, we are giving the exe and some code; when time permits, we will describe in detail the procedure.

First we define a structure:

typedef struct PaperName{
    WORD    dwType;
    char szName[100];
}PAPERNAME;

and initialize it with:

PAPERNAME g_objPaperName[MAX_PAPER_COUNT] = {
    { DMPAPER_LETTER,  "Letter, 8 1/2- by 11-inches" },
    { DMPAPER_LEGAL, "Legal, 8 1/2- by 14-inches " },
    { DMPAPER_A4, "A4, Sheet, 210- by 297-millimeters " },
    { DMPAPER_CSHEET, "C Sheet, 17- by 22-inches " },
    { DMPAPER_DSHEET, "D Sheet, 22- by 34-inches " },
    { DMPAPER_ESHEET, "E Sheet, 34- by 44-inches " },
    { DMPAPER_LETTERSMALL, "Letter Small, 8 1/2- by 11-inches " },
    { DMPAPER_TABLOID, "Tabloid, 11- by 17-inches " },
    { DMPAPER_LEDGER, "Ledger, 17- by 11-inches " },
    { DMPAPER_STATEMENT, "Statement, 5 1/2- by 8 1/2-inches " },
    { DMPAPER_EXECUTIVE, "Executive, 7 1/4- by 10 1/2-inches" }, 
    { DMPAPER_A3, "A3 sheet, 297- by 420-millimeters " },
    { DMPAPER_A4SMALL, "A4 small sheet, 210- by 297-millimeters " },
    { DMPAPER_A5, "A5 sheet, 148- by 210-millimeters " },
    { DMPAPER_B4, "B4 sheet, 250- by 354-millimeters " },
    { DMPAPER_B5, "B5 sheet, 182- by 257-millimeter paper" }, 
    { DMPAPER_FOLIO, "Folio, 8 1/2- by 13-inch paper " },
    { DMPAPER_QUARTO, "Quarto, 215- by 275-millimeter paper " },
    { DMPAPER_NOTE, "Note, 8 1/2- by 11-inches " },
    { DMPAPER_ENV_9, "#9 Envelope, 3 7/8- by 8 7/8-inches " },
    { DMPAPER_ENV_10, "#10 Envelope, 4 1/8- by 9 1/2-inches " },
    { DMPAPER_ENV_11, "#11 Envelope, 4 1/2- by 10 3/8-inches " },
    { DMPAPER_ENV_12, "#12 Envelope, 4 3/4- by 11-inches " },
    { DMPAPER_ENV_14, "#14 Envelope, 5- by 11 1/2-inches " },
    { DMPAPER_ENV_DL, "DL Envelope, 110- by 220-millimeters " },
    { DMPAPER_ENV_C5, "C5 Envelope, 162- by 229-millimeters " },
    { DMPAPER_ENV_C3, "C3 Envelope, 324- by 458-millimeters " },
    { DMPAPER_ENV_C4, "C4 Envelope, 229- by 324-millimeters " },
    { DMPAPER_ENV_C6, "C6 Envelope, 114- by 162-millimeters " },
    { DMPAPER_ENV_C65, "C65 Envelope, 114- by 229-millimeters " },
    { DMPAPER_ENV_B4, "B4 Envelope, 250- by 353-millimeters " },
    { DMPAPER_ENV_B5, "B5 Envelope, 176- by 250-millimeters " },
    { DMPAPER_ENV_B6, "B6 Envelope, 176- by 125-millimeters " },
    { DMPAPER_ENV_ITALY, "Italy Envelope, 110- by 230-millimeters " },
    { DMPAPER_ENV_MONARCH, "Monarch Envelope, 3 7/8- by 7 1/2-inches " },
    { DMPAPER_ENV_PERSONAL, "6 3/4 Envelope, 3 5/8- by 6 1/2-inches " },
    { DMPAPER_FANFOLD_US, "US Std Fanfold, 14 7/8- by 11-inches " },
    { DMPAPER_FANFOLD_STD_GERMAN, "German Std Fanfold, 8 1/2- by 12-inches " },
    { DMPAPER_FANFOLD_LGL_GERMAN, "German Legal Fanfold, 8 1/2- by 13-inches " }
    };

Next step is to get the devmode from the name of the printer.

On selecting the printer name from the list box, we call GetDeviceMode function to get the device mode (DEVMODE) struct. Using the DEVMODE, we call DeviceCapabilities function which returns the number of papers the printer supports. Then create an array of that size, and call the DeviceCapabilities function again to fill the array. Now, each member of the array represents a paper type. We compare each member of the array with the (WORD) dwType variable of the structure initialized earlier. The WORD that matches represents the paper name from the structure. The following function describes the above procedure:

void CPrintInfoDlg::OnSelchangeListPrntName() 
{
    DEVMODE  DeviceMode;
    /* Remove All Data from the ComboBox */
    m_wndPaperCombo.ResetContent();

    /* Get the Selected Position */
    int nPos = m_wndListName.GetCurSel();
    if(nPos < 0){
        return;
    }

    /* Get the name of the printer */
    CString strPrntName = "";
    m_wndListName.GetText(nPos, strPrntName);

    /* Get the DEVMOCE structure */
    GetDeviceMode(&DeviceMode, strPrntName);

    /* Get the Printer Paper Settings */
    int nPaperCount = DeviceCapabilities(strPrntName, "LPT1", 
            DC_PAPERS, NULL, &DeviceMode);

    /* Allocate Memory */
    LPSTR lpArray = new char[nPaperCount * sizeof(WORD)];

    /* Get the Paper Types */
    DeviceCapabilities(strPrntName, "LPT1", 
            DC_PAPERS, lpArray, &DeviceMode);

    /* Fill The Combo Box */
    FillCombo(lpArray, nPaperCount);

    delete [] lpArray;

    /* Set the combobox text */
    CMainFrame *mf = (CMainFrame*)AfxGetApp()->m_pMainWnd;
    mf->m_objPrinter.m_strMyClPageSize.TrimRight();
    if(m_wndPaperCombo.SelectString(0, 
        mf->m_objPrinter.m_strMyClPageSize) == CB_ERR){
        m_wndPaperCombo.SetCurSel(0);
    }
}
BOOL CPrintInfoDlg::FillCombo(LPSTR lpArray, int nCount)
{
    /* Traverse the array to get the paper type */
    for(int nCnt=0; nCnt<nCount; nCnt++){
        /* Get the Paper Name */
        CString strPaperName = (GetPaperName(lpArray[nCnt]));
        if(strPaperName.IsEmpty()){
            continue;
        }
        m_wndPaperCombo.AddString(strPaperName);
    }
    return(TRUE);
}
CString CPrintInfoDlg::GetPaperName(WORD dwType)
{
    CString strPaperName;
    for(int nCnt=0; nCnt<MAX_PAPER_COUNT; nCnt++){
        if(dwType == g_objPaperName[nCnt].dwType){
            strPaperName = g_objPaperName[nCnt].szName;
            break;
        }
    }
    return(strPaperName);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

_anil_

Web Developer

Japan Japan

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralEnumForms PinmemberDuncan Edwards Jones1:16 18 Sep '07  
Generalarigatoo gozaimasu! PinmemberWindmiller0:59 18 Sep '07  
QuestionHow to select a custom paper name? Pinmemberjulycy22:36 18 May '07  
How to select a custom paper name, eg:Invoice(length 16cm,height 7cm)
GeneralProblem Running Included Project PinmemberRommel the iCeMAn10:58 17 Jan '05  
GeneralRe: Problem Running Included Project PinsussAnonymous12:52 18 Jan '05  
GeneralRe: Problem Running Included Project PinmemberRommel the iCeMAn2:34 20 Jan '05  
QuestionDoes anyone knows how to get the paper name using a system function call? PinmemberValentin Mocanu3:40 8 Oct '04  
AnswerRe: Does anyone knows how to get the paper name using a system function call? PinmemberRommel the iCeMAn11:07 17 Jan '05  
AnswerRe: Does anyone knows how to get the paper name using a system function call? PinmemberOle Thomsen23:28 19 Jan '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 19 Jan 2005
Article Copyright 2004 by _anil_
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid