Solved

printing page x of y using a richedit

Posted on 2002-06-25
18
339 Views
Last Modified: 2013-11-22
hi, is there a way to calculate and print "page x of y" using a trichedit?

i defines a region for the text so i have space to create either a footer or header. any way to do this?

thanks a lot

uri
0
Comment
Question by:urif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 4
  • +1
18 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7106753
best would be to use quickreport or
any other report-designer
0
 

Author Comment

by:urif
ID: 7106778
thx, but it has to be a simpler way, i mean, i only want to print the text i'm editing and etiher at the bottom or top of the page place a page number and filename that's all
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7106783
i don't think, that this you want will be easier
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:urif
ID: 7106919
it's just playing with the printer or richedit canvas. for example, this is what i use to print giving the page borders

procedure TForm1.Print1Click(Sender: TObject);
var presX, presY:integer;
aster,name:string;
j:integer;
begin
  if PrintDialog1.Execute then
  begin
    with ActiveRich do
    begin
      plaintext:=true;
      presX:=GetDeviceCaps(printer.handle,LOGPIXELSX);
      presY:=GetDeviceCaps(printer.handle,LOGPIXELSY);
      with pagerect do
      begin
        left:=presX div 2; //1 inch left margin //added div 2
        top:=3 * presY div 4;//1.5 inch top margin// changed from div2 to 4
        right:=Printer.PageWidth - 3 * presX div 4; //0.75 inch
        bottom:=Printer.PageHeight - presY; // 1 inch bottom
        Print(PageControl1.ActivePage.Caption);
      end;
    end;
  end;
end;
0
 
LVL 3

Expert Comment

by:lopem
ID: 7109118
listening
0
 

Author Comment

by:urif
ID: 7109726
well, any ideas?
someone over there must be a guru on thr priter's canvas, right?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7109737
>someone over there must be a guru on thr priter's canvas
definitivly this i'm not ;-)
0
 

Author Comment

by:urif
ID: 7109747
well, i guess i'll have to spend the rest of my life trying to figure out something that even notepad can do :(
0
 
LVL 3

Expert Comment

by:raidos
ID: 7109947
Hmmm...well...this wouldn't be that difficult would it ?

Just write a function that iterates through the lines of text in the richedit, and decide if that line of text should be printed or not...if it should be printed then just Textout onto the printer canvas.. else just dont..

Geez, even I don't understand that.
Hope you do...i'll se if i can write a simple function for you sometime today

Regards
//raidos
0
 

Author Comment

by:urif
ID: 7110023
thx!

the only problem is that if i print thru the printer's canvas then the function to handle the borders and stuff won't work anymore (look at my example above)
0
 
LVL 3

Accepted Solution

by:
raidos earned 50 total points
ID: 7111504
I haven't tested this but it should work...maybe a little tweaking with aRange necessary..hope it does the trick...


Procedure PrintRichEdit(ARichEdit: TRichEdit; aRange: TPoint; Caption: String);
var
  Range: TFormatRange;
  LastChar, MaxLen, LogX, LogY, OldMap: Integer;
  SaveRect: TRect;
  PageCounter : Integer;
  UseRange : Boolean;
Procedure SetupPrinter;
Begin
  With Printer, Range Do Begin
    If OldMap <> 0 Then
      SetMapMode(hdc, OldMap);
    Title := Caption;
    BeginDoc;
    hdc := Handle;
    hdcTarget := hdc;
    LogX := GetDeviceCaps(Handle, LOGPIXELSX);
    LogY := GetDeviceCaps(Handle, LOGPIXELSY);
    if IsRectEmpty(aRichEdit.PageRect) then
    begin
      rc.right := PageWidth * 1440 div LogX;
      rc.bottom := PageHeight * 1440 div LogY;
    end
    else begin
      rc.left := aRichEdit.PageRect.Left * 1440 div LogX;
      rc.top := aRichEdit.PageRect.Top * 1440 div LogY;
      rc.right := aRichEdit.PageRect.Right * 1440 div LogX;
      rc.bottom := aRichEdit.PageRect.Bottom * 1440 div LogY;
    end;
    rcPage := rc;
    SaveRect := rc;
    LastChar := 0;
    MaxLen := aRichEdit.GetTextLen;
    chrg.cpMax := -1;
    // ensure printer DC is in text map mode
    OldMap := SetMapMode(hdc, MM_TEXT);
  End;
End;

begin
  UseRange := (aRange.Y > 0) And (aRange.X > 0) And (aRange.X <= aRange.Y);
  FillChar(Range, SizeOf(TFormatRange), 0);
  with Printer, Range do
  begin
    OldMap := 0;
    SetupPrinter;
    SendMessage(aRichEdit.Handle, EM_FORMATRANGE, 0, 0);    // flush buffer
    PageCounter := 1;
    try
      repeat
        rc := SaveRect;
        chrg.cpMin := LastChar;
        LastChar := SendMessage(aRichEdit.Handle, EM_FORMATRANGE, 1, Longint(@Range));
        if (LastChar < MaxLen) and (LastChar <> -1) then Begin
          If UseRange And (PageCounter < aRange.X) Then Begin
            Abort;
            SetupPrinter;
          End Else If (UseRange And (PageCounter < aRange.Y)) Or Not UseRange Then
            NewPage;
          Inc(PageCounter);
        End;
      until (LastChar >= MaxLen) or (LastChar = -1) Or (PageCounter >= aRange.Y);
      EndDoc;
    finally
      SendMessage(aRichEdit.Handle, EM_FORMATRANGE, 0, 0);  // flush buffer
      SetMapMode(hdc, OldMap);       // restore previous map mode
    end;
  end;
end;

Regards
//raidos
0
 
LVL 3

Expert Comment

by:raidos
ID: 7111509
Doh...

Call the function like this
  PrintRichEdit(ActiveRich, Point(FirstPage, LastPage), 'YourTitle');

Regards again.. =)
0
 
LVL 3

Expert Comment

by:raidos
ID: 7111516
Geez, i can't even remember what you asked for...*goddamnit* how to delete comments ?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7111536
interesting code, raidos
0
 

Author Comment

by:urif
ID: 7112788
looks good, i'm going to try it now.
but where do you pass the code for the page number?
0
 
LVL 3

Expert Comment

by:raidos
ID: 7113020
Urif, as i said in my last comment, the code provided is not really what you asked for...

sorry about that..

however if you read the code you'll see that on every new page it sets up the printer...and before it does the new page it could put the current page on the printers canvas i guess...and then you would only need a function to get the total pagecount..

kretzschmar, interesting code ? =)

Regards
//raidos
0
 

Author Comment

by:urif
ID: 7113056
ok, thanks for the code :)
0
 

Author Comment

by:urif
ID: 7119568
Raidos,

do you know what do i need to include in uses for TFormatRange?

i'm using delphi 5 enterprise... i can't seem to find it anywhere.

thx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

688 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