Problem with header

General TRichView support forum. Please post your questions here
Post Reply
MLP
Posts: 10
Joined: Tue Jan 23, 2007 10:59 am

Problem with header

Post by MLP »

I have a problem with the size of my header.
Only the lowest part of my header is shown on the page (if the header is more than 5 lines of text).

It seems that the page does not allocate enough space for the full header - but how do I tell RVPrint how much space it should allocate for the header?

I'm using the onPagePrepaint because I need to make different headers for different pages.

My "insert header" code:

Code: Select all

procedure TfrmEditTemplate.CopyDoc(src, dst: TCustomRichView);
var Stream: TMemoryStream;
begin
  Stream := TMemoryStream.Create;
  src.SaveRVFToStream(Stream, False);
  Stream.Position := 0;
  dst.LoadRVFFromStream(Stream);
  Stream.Free;
end;

procedure TfrmEditTemplate.RVPrintPagePrepaint(
  Sender: TRVPrint;
  PageNo: Integer;
  Canvas: TCanvas;
  Preview: Boolean;
  PageRect, PrintAreaRect: TRect);
begin
  CopyDoc(rvHeader, rvhHeader.RichView);

  rvhHeader.Init(Canvas, PrintAreaRect.Right-PrintAreaRect.Left);
  rvhHeader.FormatNextPage(PrintAreaRect.Bottom-PrintAreaRect.Top);
  rvhHeader.DrawPageAt(PrintAreaRect.Left, PrintAreaRect.Top-rvhHeader.GetLastPageHeight,
    1, Canvas, False, rvhHeader.GetLastPageHeight);
end;

Relevant part of my dfm file:

Code: Select all

object frmEditTemplate: TfrmEditTemplate
  object rvHeader: TRichViewEdit
    Left = 24
    Top = 163
    Width = 200
    Height = 118
    ReadOnly = False
    ParentShowHint = False
    PopupMenu = mnuSpell
    ShowHint = False
    TabOrder = 5
    DoInPaletteMode = rvpaCreateCopies
    MaxTextWidth = 783
    MinTextWidth = 783
    Options = [rvoAllowSelection, rvoScrollToEnd, rvoShowPageBreaks, rvoTagsArePChars, rvoAutoCopyText, rvoAutoCopyRVF, rvoAutoCopyImage, rvoAutoCopyRTF, rvoFormatInvalidate, rvoDblClickSelectsWord, rvoRClickDeselects, rvoShowItemHints]
    RTFReadProperties.UnicodeMode = rvruMixed
    RTFReadProperties.TextStyleMode = rvrsAddIfNeeded
    RTFReadProperties.ParaStyleMode = rvrsAddIfNeeded
    RVFOptions = [rvfoSavePicturesBody, rvfoSaveControlsBody, rvfoIgnoreUnknownPicFmt, rvfoIgnoreUnknownCtrls, rvfoConvUnknownStylesToZero, rvfoConvLargeImageIdxToZero, rvfoSaveBinary, rvfoSaveBack, rvfoLoadBack, rvfoSaveTextStyles, rvfoSaveParaStyles, rvfoSaveLayout, rvfoLoadLayout]
    RVFParaStylesReadMode = rvf_sInsertMap
    RVFTextStylesReadMode = rvf_sInsertMap
    Style = rvsHeader
  end

  object rvsHeader: TRVStyle
    TextStyles = <
      item
        StyleName = 'Normal text'
        FontName = 'Arial'
      end
      item
        StyleName = 'Code'
        FontName = 'Arial'
        Style = [fsBold]
        BackColor = clGrayText
        Protection = [rvprStyleProtect, rvprModifyProtect, rvprConcateProtect, rvprDoNotAutoSwitch]
      end>
    ParaStyles = <
      item
        StyleName = 'Paragraph Style'
        Tabs = <>
      end
      item
        StyleName = 'Centered'
        Alignment = rvaCenter
        Tabs = <>
      end>
    ListStyles = <
      item
        StyleName = 'Bullets'
        Levels = <
          item
            LeftIndent = 24
            Font.Height = -16
          end>
        LstID = 542175222
      end>
    SpacesInTab = -1
    InvalidPicture.Data = {---snip---}
    StyleTemplates = <>
    Left = 31
    Top = 198
  end

  object rvhHeader: TRVReportHelper
    PreviewCorrection = True
    RichView.BackgroundStyle = bsNoBitmap
    RichView.BottomMargin = 0
    RichView.Options = [rvoAllowSelection, rvoScrollToEnd, rvoShowPageBreaks, rvoTagsArePChars, rvoAutoCopyText, rvoAutoCopyRVF, rvoAutoCopyImage, rvoAutoCopyRTF, rvoFormatInvalidate, rvoDblClickSelectsWord, rvoRClickDeselects, rvoFastFormatting]
    RichView.RTFReadProperties.TextStyleMode = rvrsAddIfNeeded
    RichView.RTFReadProperties.ParaStyleMode = rvrsAddIfNeeded
    RichView.RVFOptions = [rvfoSavePicturesBody, rvfoSaveControlsBody, rvfoSaveBinary, rvfoSaveTextStyles, rvfoSaveParaStyles, rvfoSaveDocProperties, rvfoLoadDocProperties]
    RichView.Style = rvsHeader
    RichView.TopMargin = 0
    Left = 280
    Top = 128
  end

  object RVPrint: TRVPrint
    PreviewCorrection = True
    LeftMarginMM = 20
    RightMarginMM = 20
    TopMarginMM = 20
    BottomMarginMM = 20
    OnPagePrepaint = RVPrintPagePrepaint
    Left = 166
    Top = 172
  end
end
Sergey Tkachenko
Site Admin
Posts: 17310
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Do headers on different pages have different heights?
MLP
Posts: 10
Joined: Tue Jan 23, 2007 10:59 am

Post by MLP »

No
Sergey Tkachenko
Site Admin
Posts: 17310
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

You draw header above the printable area (above the rectangle defined in PrintAreaRect).
You can reserve space for header by increasing RVPrint.TopMarginsMM.
The code below does it. Call it before calling RVPrint1.FormatPages.

Code: Select all

// returns page width and height, in printer pixels
procedure GetPageSize(RVPrint: TRVPrint; var Width, Height: Integer); 
var DC: HDC; 
    phoX, phoY, phW, phH, lpy, lpx, LM, TM, RM, BM: Integer; 
begin 
  DC := RV_GetPrinterDC; // from PtblRV unit 

  Width  := GetDeviceCaps(DC, HORZRES); 
  Height := GetDeviceCaps(DC, VERTRES); 

  lpy := GetDeviceCaps(DC, LOGPIXELSY); 
  lpx := GetDeviceCaps(DC, LOGPIXELSX); 

  phoX := GetDeviceCaps(DC, PHYSICALOFFSETX); 
  phoY := GetDeviceCaps(DC, PHYSICALOFFSETY); 
  phW  := GetDeviceCaps(DC, PHYSICALWIDTH); 
  phH  := GetDeviceCaps(DC, PHYSICALHEIGHT); 

  LM := MulDiv(RVPrint.LeftMarginMM,   5*lpx, 127)- phoX; 
  TM := MulDiv(RVPrint.TopMarginMM,    5*lpy, 127)- phoY; 
  RM := MulDiv(RVPrint.RightMarginMM,  5*lpx, 127)- (phW-(phoX+Width)); 
  BM := MulDiv(RVPrint.BottomMarginMM, 5*lpy, 127)- (phH-(phoY+Height)); 

  if LM<0 then LM := 0; 
  if TM<0 then TM := 0; 
  if RM<0 then RM := 0; 
  if BM<0 then BM := 0; 

  dec(Width, LM+RM); 
  dec(Height, TM+BM); 

  DeleteDC(DC); 
end;



var DC: HDC;
     Canvas: TCanvas;
     Width, Height, HeaderHeight: Integer;

  DC := RV_GetPrinterDC;
  Canvas := TCanvas.Create;
  Canvas.Handle := DC;
  Canvas.Font.PixelsPerInch := GetDeviceCaps(DC, LOGPIXELSY);
  GetPageSize(RVPrint1, Width, Height);
  CopyDoc(rvHeader, rvhHeader.RichView); 
  rvhHeader.Init(Canvas, Width);
  rvhHeader.FormatNextPage(Height);
  HeaderHeight := rvhHeader.GetLastPageHeight;

  RVPrint1.TopMarginMM :=
    RVPrint1.TopMarginMM+MulDiv(HeaderHeight, 127, 5*GetDeviceCaps(DC, LOGPIXELSY));

  Canvas.Handle := 0;
  DeleteDC(DC);
I used the code from http://www.trichview.com/support/files/cppheader.zip . Actually, this demo does exactly what you need, but it is in C++. I converted fragment to Pascal for this post.

There is even more complicated demo where headers may have different heights on different pages. In this case, not only header, but the main document too must be rendered using TRVReportHelper. The example is http://www.trichview.com/support/files/ ... aderex.zip
MLP
Posts: 10
Joined: Tue Jan 23, 2007 10:59 am

Post by MLP »

I had only tried to change TopMarginMM in the RVPrintPagePrepaint event.

Setting TopMaginMM before FormatPages fixes my problem - thank you for the help.
Post Reply