Solved

Help needed on line printing

Posted on 2002-06-03
7
409 Views
Last Modified: 2010-08-05
Anyone knows how to stop advancing to the next page in line printing? Meaning the printer will print a portion of the paper (if it does not use up the whole page) and wait for the next print.

Currently this is the coding I am using for the printing part:

// p points to the start of the buffer, pSize holds the number of characters
void PrintString(char *p, DWORD pSize)
{
    CDC             dc;
    CPrintDialog    printDlg(FALSE);
    CRect           r;
    int             nHeight;
   
    // ask the user to select a printer
    if (printDlg.DoModal() == IDCANCEL)
        return;
   
    // Attach a printer DC
    dc.Attach(printDlg.GetPrinterDC());
    dc.m_bPrinting = TRUE;

    // use Textmappingmode, that's easiest to map the fontsize
    dc.SetMapMode(MM_TEXT);
   
    // setup font specifics
    LOGFONT LogFont;
   
    CFont     aFont, *oldFont;
   
    LogFont.lfHeight = -MulDiv(10, GetDeviceCaps(dc, LOGPIXELSY), 72);
    LogFont.lfWidth = 0;
    LogFont.lfEscapement = 0;
    LogFont.lfOrientation = 0;
    LogFont.lfWeight = 0;
    LogFont.lfItalic = false;
    LogFont.lfUnderline = 0;
    LogFont.lfStrikeOut = 0;
    LogFont.lfCharSet = ANSI_CHARSET;
    LogFont.lfOutPrecision = OUT_TT_PRECIS;
    LogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
    LogFont.lfQuality = DEFAULT_QUALITY;
    LogFont.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS;
    lstrcpy (LogFont.lfFaceName, "MS Sans Serif");
    dc.SetBkMode(OPAQUE);
    aFont.CreateFontIndirect ( &LogFont );
    // ok, we've build the font, now use it
    oldFont = dc.SelectObject( &aFont );        
       
    // Get the application title
    CString strTitle;
    strTitle.LoadString(AFX_IDS_APP_TITLE);
   
    // Initialise print document details
   
    DOCINFO di;
    ::ZeroMemory (di, sizeof (DOCINFO));
    di.cbSize = sizeof (DOCINFO);
    // application title appears in the spooler view
    di.lpszDocName = strTitle;
   
    // Begin a new print job
    BOOL bPrintingOK = dc.StartDoc( &di );
       
    // Get the printing extents and store in the m_rectDraw field of a
    // CPrintInfo object
    CPrintInfo Info;
    int w = dc.GetDeviceCaps(HORZRES);
    int h = dc.GetDeviceCaps(VERTRES);
    Info.m_rectDraw.SetRect(0,0, w, h);
   
    char *startAt = p;
    int totalDone = 0;
    int lengthToGo = pSize;
   
    for (UINT page = Info.GetMinPage();
    bPrintingOK & totalDone < lengthToGo; page++)
    {
        // begin new page
        dc.StartPage();
        Info.m_nCurPage = page;
       
        // calc how much text fits on one page
        r = Info.m_rectDraw;
        r.bottom = r.top;
        int i = 0;
        while (r.bottom < Info.m_rectDraw.bottom & totalDone + i < lengthToGo)
        {
            r.right = Info.m_rectDraw.right;
            nHeight = dc.DrawText(startAt, i++, r,
                DT_CALCRECT|DT_WORDBREAK|DT_NOCLIP|DT_EXPANDTABS);
        }
        // go one back to assure correct height
        if (r.bottom >= Info.m_rectDraw.bottom)
            i--;
       
       
        // print that text
        dc.DrawText(startAt, i, r, DT_WORDBREAK|DT_NOCLIP|DT_EXPANDTABS);
       
        // go to next page
        startAt += i;
        totalDone += i;
       
        // end page
        bPrintingOK = (dc.EndPage() > 0);
    }
   
    // end a print job
    if (bPrintingOK)
        dc.EndDoc();
    else
        // abort job.
        dc.AbortDoc();
   
   
    // restore font
    dc.SelectObject(oldFont);
    // free font memory
    qFont.DeleteObject();
    // detach the printer DC
    dc.Detach();
}

0
Comment
Question by:cytay
  • 3
  • 3
7 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 7053254
If you never call
   dc.EndPage()
then it won't eject the page.  

But this sounds very odd.  You really can't leave the printer and the spooler 'dangling' with a half page.  

The only reason you might want to do this is because you expect to print some more data on the bottom part of the page.  In that case, the normal procedure would be to go ahead and print that data -- make it part of the same print job.

Can you explain your goal?  There may be other, better ways to accomplish it.

-- Dan
0
 

Author Comment

by:cytay
ID: 7053266
Hi Dan,

Even if I commented out dc.EndPage(), it will still eject the page.

I am developing a program which uses dot matrix printer. Everytime the user places any purchase on sth then the program will print out the purchase detail. I need them to be printed on the same paper.

It doesn't make sense for me to eject out all the paper in this case right?

Thanks.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7053318
dc.EndPage() is called twice.

For dot-matrix output of partial page, it is probably best to use old style printing, where you open LPT1 as a file and dump text to it.

Windows output is page-oriented.  It is best to accumulate a full page, then print it all at once.

One outside-the-box idea is to define a short page (custom paper size) ... say about the length of 10 lines of text.  Then each output is a single page.  You would need to set up the printer so that it knows to advance only 10 lines for each page.  This will be printer-dependant.  Check all of the settings for the printer driver, there may be some help there.

-- Dan
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:cytay
ID: 7053388
Hi Dan,

Still can't work out for this time...
the old method of open
FILE *ptr = fopen("LPT1","w");
should close by this
fclose(ptr);

I am facing problem that it only print once...
Thanks.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 300 total points
ID: 7053415
>>I am facing problem that it only print once...

What do you mean? How many times do you want it to print?  

-- Dan
0
 
LVL 22

Expert Comment

by:nietod
ID: 7053624
>> For dot-matrix output of partial page, it is probably best to use old style printing,
>> where you open  LPT1 as a file and dump text to it.
>>
>> Windows output is page-oriented.  It is best to accumulate a full page, then print it all at once.
to continue this thought...  windows output is page-oriented because most new printers are "page printers" as opposed to "line printers".  A page printer does not physically print until it has an entire page of information to print.  It cannot print partial pages.  

Theoretically you could leave a page partially "used" in the printer and then print to the rest of the page at a later time.  But windows will never let you do this.  Whenever you stopp printing to it, windows will assume the last page printed is a full page and will finsih cause the page to be printed.  It has to do this.    It is very reasonable to assume that programs want to print to whole pages.  Image what would happen if it didn't make this assumption.  If a program is printing and crashes it might leave a page 1/2 done.  Then the next program to print would have its output mixed with the previous program's output.   Windows won't allow this.

Your best bet is to rethink your entire approach.

Write the information to a log file and when you have a full page worth, then print the info.  (We write point-of-sales systems and this is precisely what we do.)  There are many advantages to this.

For example, consider what you will do now if the printer jams?   You can reprint the 2nd part of the page since you are generatig that "currently" but the 1st part of the page was generated earlier, could you regenerate it?  probably not.   By keeping a log you can regenerate all the necessary info to perform a reprint.
0
 

Author Comment

by:cytay
ID: 7055310
Dan,

Thanks for your suggestions and comments. I have solved the problem by using the old method of printing.

nietod,

Thanks to you too. Unfortunately I have to give the points to Dan becoz he was here first. :)



0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question