[Example] How to store page information in RVF

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Sergey Tkachenko
Site Admin
Posts: 13833
Joined: Sat Aug 27, 2005 10:28 am

[Example] How to store page information in RVF

Post by Sergey Tkachenko » Tue May 12, 2009 11:33 am

TRichView is designed to use the current application page settings. But if you want to store page information in RVF files, you can do it using the code below.

This code is not necessary if you use ScaleRichView. It does all this work automatically.

Page information is written and read in DocParameters property, containing subproperties for storing page size, orientation and margins.
This property is saved and loaded in RVF if rvfoSaveDocProperties and rvfoLoadDocProperties are included in RVFOptions property.
This property is saved and loaded in RTF if rvrtfSaveDocParameters is included in RTFOptions, and RTFReadProperties.ReadDocParameters = True.

How to assign DocParameters:

Code: Select all

  procedure GetPaperWH(var Width, Height: Extended);
  var DC: HDC;
    DC := Printer.Handle;
    Width := GetDeviceCaps(DC, PHYSICALWIDTH) / GetDeviceCaps(DC, LOGPIXELSX);
    Height := GetDeviceCaps(DC, PHYSICALHEIGHT) / GetDeviceCaps(DC, LOGPIXELSY);

  InchPerMM: Extended;
  W,H: Extended;
  rve.DocParameters.Units := rvuInches;
  InchPerMM := 1/rve.DocParameters.UnitsPerInch(rvuMillimeters);
  rve.DocParameters.LeftMargin := RVPrint1.LeftMarginMM*InchPerMM;
  rve.DocParameters.TopMargin := RVPrint1.TopMarginMM*InchPerMM;
  rve.DocParameters.RightMargin := RVPrint1.RightMarginMM*InchPerMM;
  rve.DocParameters.BottomMargin := RVPrint1.BottomMarginMM*InchPerMM;
  rve.DocParameters.HeaderY := RVPrint1.HeaderYMM*InchPerMM;
  rve.DocParameters.FooterY := RVPrint1.FooterYMM*InchPerMM;
  rve.DocParameters.MirrorMargins  := RVPrint1.MirrorMargins;
  rve.DocParameters.Orientation := Printer.Orientation;
  rve.DocParameters.PageWidth := W;
  rve.DocParameters.PageHeight := H;
If you use RichViewActions, you may wish to store additional properties, for example properties of header and footer. See http://www.trichview.com/forums/viewtopic.php?t=10 how to access them.
You can use rve.DocProperties to store them:

Code: Select all

After loading RVF, you need to activate the read page settings.
How to activate DocParameters:

Code: Select all

function ConvertPageSizeToPageFormat(PaperWidth, PaperHeight: Integer): Integer;
var tmp: Integer;
  if PaperHeight<PaperWidth then begin
    tmp := PaperHeight;
    PaperHeight := PaperWidth;
    PaperWidth := tmp;
  // this is not a complete list (and mistakes are possible)
  if (PaperWidth = 297) and (PaperHeight = 420) then
    Result := DMPAPER_A3
  else if (PaperWidth = 210) and (PaperHeight = 297) then
    Result := DMPAPER_A4
  else if (PaperWidth = 148) and (PaperHeight = 210) then
    Result := DMPAPER_A5
  else if (PaperWidth = 257) and (PaperHeight = 364) then
    Result := DMPAPER_B4 // JIS
  else if (PaperWidth = 182) and (PaperHeight = 257) then
    Result := DMPAPER_B5 // JIS
  else if (PaperWidth = 216) and (PaperHeight = 279) then
    Result := DMPAPER_LETTER
  else if (PaperWidth = 216) and (PaperHeight = 356) then
    Result := DMPAPER_LEGAL
  else if (PaperWidth = 215) and (PaperHeight = 355) then
    Result := DMPAPER_LEGAL
  else if (PaperWidth = 279) and (PaperHeight = 432) then
  else if (PaperWidth = 279) and (PaperHeight = 432) then
    Result := DMPAPER_LEDGER
  else if (PaperWidth = 140) and (PaperHeight = 216) then
  else if (PaperWidth = 203) and (PaperHeight = 254) then
    Result := DMPAPER_QUARTO
  else if (PaperWidth = 203) and (PaperHeight = 330) then
    Result := DMPAPER_FOLIO
  else if (PaperWidth = 184) and (PaperHeight = 267) then
  else if (PaperWidth = 99) and (PaperHeight = 225) then
    Result := DMPAPER_ENV_9
  else if (PaperWidth = 105) and (PaperHeight = 241) then
    Result := DMPAPER_ENV_10
  else if (PaperWidth = 114) and (PaperHeight = 264) then
    Result := DMPAPER_ENV_11
  else if (PaperWidth = 121) and (PaperHeight = 279) then
    Result := DMPAPER_ENV_12
  else if (PaperWidth = 127) and (PaperHeight = 292) then
    Result := DMPAPER_ENV_14
    Result := 0;

procedure SetPaperWH(Width, Height: Integer);
  var ADevice, ADriver, APort: array[0..79] of Char;
      ADeviceMode: THandle;
      DevMode: PDeviceMode;
  if ADeviceMode<>0 then begin
    DevMode := PDeviceMode(GlobalLock(ADeviceMode))
    raise Exception.Create('Error initializing printer');
  DevMode.dmFields := DevMode.dmFields or DM_PAPERSIZE;
  DevMode.dmPaperSize := ConvertPageSizeToPageFormat(Width, Height);
  if DevMode.dmPaperSize=0 then begin
    // the results are correct even if standard PaperSize is not detected
    DevMode.dmPaperWidth := Width*10;
    DevMode.dmPaperLength := Height*10;

var MMPerUnit: Extended;
  // assigning margins, HeaderYMM, FooterYMM, MirrorMargins
  Printer.Orientation := rve.DocParameters.Orientation;
  MMPerUnit := rve.DocParameters.UnitsPerInch(rvuMillimeters)/
Assign printing settings to DocParameters and DocProperties when these settings are changed. If you use RichViewActions, use rvActionPageSetup.OnChange event.

If you use TDBRichViewEdit, use OnLoadDocument event to apply loaded values, and OnNewDocument to reset them to the defaults.
If you use RichViewActions, use rvActionOpen.OpenFile to apply loaded values (TrvActionNew resets the to default automatically).

Sergey Tkachenko
Site Admin
Posts: 13833
Joined: Sat Aug 27, 2005 10:28 am

Re: [Example] How to store page information in RVF

Post by Sergey Tkachenko » Fri Nov 10, 2017 9:09 am

The code described in this topic is still valid.
However, some corrections are needed.

1. Use Header and Footer properties of TRVAControlPanel instead of RVA_HeaderInfo and RVF_FooterInfo
2. You can still use "MM" properties of TRVPrint (like HeaderYMM, LeftMarginMM), but they are deprecated. The recommended properties are HeaderY, FooterY, Margins.

Also, there is one more way to store custom information in RVF. Instead of DocProperties string list, you can use DocObjects collection.

Post Reply