Board index » cppbuilder » Unwanted extra pages when printing

Unwanted extra pages when printing

Using BCB5 and Printer(), I would like to print the total number of pages in
a document on the printout. eg Page 1 of 3, Page 2 of 3, Page 3 of 3. I have
written the following that works fine for printers connected through a LPT
port. As soon as I print across the network, the correct printout is
preceeded with a Page 1 of 1 and Page 2 of 2 page for a three page document.
I have tried several types of printers and operating systems. Local printers
work (Canon BJC-2000 for instance). Network printers do not (HP 4050
laserjet for instance).

I did have to cut quite a bit out of the code, but I hope this gives a
pretty good idea of what's going on.

void __fastcall TForm1::DoPrint(void)
{
    //go through the first time to get the total number of pages and abort
    //print the second time through
    for (PrintLoop = 0; PrintLoop < 2; PrintLoop++)
    {
        Printer()->Title = "Recipe Report: "+FRecipeName;
        Printer()->BeginDoc();

        PrintCanvas = new TReportCanvas(Printer()->Canvas);  //PrintCanvas
contains functions that do all the math for newlines, margins, etc. See
constructor below
        PrintCanvas->OnNewPage = PrinterNewPage;

        PrintCanvas->PageHeight = Printer()->PageHeight;
        PrintCanvas->PageWidth  = Printer()->PageWidth;

        if (PrintLoop == 0)
        {
          TotalNumPages = IntToStr(Printer()->PageNumber);
        }//end if

        AnsiString HeaderText = "Page: " + IntToStr(Printer()->PageNumber) +
" of " + TotalNumPages;  //Feature A-000265
        PrintCanvas->ClearLastOutput();
// forget anything that was printed at the end of the last page
        PrintCanvas->Output(HeaderText);
// print the header text
        PrintCanvas->NewLine(1);

/*
** print other stuff here
*/

        if (PrintLoop == 0)
          Printer()->Abort();
//first time through just get number of pages, don't print
        if (PrintLoop == 1)
          Printer()->EndDoc();
// complete job - it will print now

        delete PrintCanvas;
        PrintCanvas = NULL;
    }//end for loop

Quote
}

TReportCanvas::TReportCanvas(TCanvas* Canvas)
{
        Fxpos = 0;
// initialize variables
        Fypos = 0;
        FLeftMargin = 0;
        FTopMargin = 0;
        FRightMargin = 0;
        FTopMargin = 0;
        FCanvas = Canvas;

Quote
}

Any help would be greatly appreciated.

PHiL

 

Re:Unwanted extra pages when printing


"Phillip E. Meyer" <p...@fluidairinc.com> wrote:

Quote
> [...] I did have to cut quite a bit out of the code, but I
> hope this gives a pretty good idea of what's going on.
>        if (PrintLoop == 0)
>        {
>          TotalNumPages = IntToStr(Printer()->PageNumber);
>        }//end if

I didn't give your code a hard look but this could be the
source of the problem where PageNumber is some how corrupted
or never gets updated on a network. As an alternative, you
could count the pages yourself.

I also have a thought for you. Instead of calling Abort(), you
might try using a boolean flag CountingPages. In the function
that actually sends output to the printer add a test if( !CountingPages )
then print the output. I have used this method myself for some
specialized printing and I know that it works across networks.

~ JD

Re:Unwanted extra pages when printing


Thanks for the info JD.

Turns out that when a new page occurred on our "temp" canvas, it was also
setting a new page for the printer. The "fix" was to just not do a NewPage()
during the first loop when we were just counting pages.

void __fastcall TForm::PrinterNewPage(TObject *Sender)
{
    if(PrintLoop == 1)               // 0 is first loop for page counting, 1
is for actual printing
      Printer()->NewPage();      // force new page on printer now

   PageHeaderToCanvas();      //custom header for every page

Quote
}

Why it worked on directly connected printers and not over networks is beyond
me. This "solution" works for every network and parallel printer we've
tried.

Other Threads