Solved

printing page x of y using a richedit

Posted on 2002-06-25
18
331 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
  • 8
  • 5
  • 4
  • +1
18 Comments
 
LVL 27

Expert Comment

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

Author Comment

by:urif
Comment Utility
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
Comment Utility
i don't think, that this you want will be easier
0
 

Author Comment

by:urif
Comment Utility
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
Comment Utility
listening
0
 

Author Comment

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

Expert Comment

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

Author Comment

by:urif
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:urif
Comment Utility
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
Comment Utility
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
Comment Utility
Doh...

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

Regards again.. =)
0
 
LVL 3

Expert Comment

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

Expert Comment

by:kretzschmar
Comment Utility
interesting code, raidos
0
 

Author Comment

by:urif
Comment Utility
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
Comment Utility
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
Comment Utility
ok, thanks for the code :)
0
 

Author Comment

by:urif
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now