Center cover image

General TRichView support forum. Please post your questions here
Post Reply
Jim Knopf
Posts: 241
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Center cover image

Post by Jim Knopf »

Hi Sergey,

I intend to put an image on the first page of an novels text document (cover image). The image should be centered vertically and horizontally and fit to the space inside the margins.

I do it that way as the first insertion in this document:

Code: Select all

      Units := srvCo.UnitsProgram; 
      srvCo.UnitsProgram := rvuPixels;
      Wi := Round(srvCo.PageProperty.PageWidth - srvCo.PageProperty.LeftMargin - srvCo.PageProperty.RightMargin) - 2;
      He := Round(srvCo.PageProperty.PageHeight - srvCo.PageProperty.TopMargin - srvCo.PageProperty.BottomMargin) - 3;

      CIm := TImage.Create(nil);
      CIm.Picture.LoadFromFile(tlEx.Items[0].Strings[tcExImage.Index]);

      R.AddPictureEx('Cover', CIm.Picture.Graphic, 4, rvvaBaseline);     // *)

      SV := CIm.Picture.Graphic.Height / CIm.Picture.Graphic.Width;
      Gii := TRVGraphicItemInfo(R.GetItem(1));
      if Round(He/Wi*100) >= Round(SV*100) then
      begin
        Gii.SetExtraIntProperty(rvepImageWidth, Wi);
        Gii.SetExtraIntProperty(rvepImageHeight, Trunc(Wi*SV));
      end
      else
      begin
        Gii.SetExtraIntProperty(rvepImageWidth, Trunc(He/SV));
        Gii.SetExtraIntProperty(rvepImageHeight, He);
      end;

      srvCo.UnitsProgram := Units;
*) ParaStyle 4 means horizontally centered

It works fine, iamage is centered but there is a blank page in front of the cover page.

1. Why is the picture item number 1 and not 0? A few lines above I assign some properties and during first assignment a text item is created automatically (line with <<<):

Code: Select all

  R.Clear;
  if (R.Style = rvsComplete) or (R.Style = rvsCompleteUni) then
  begin
    srvCo.PageProperty.PageHeight   := Str3ToFloat(tlPa.Items[cePageTemplate.ItemIndex].Strings[tcPaHeight.Index]);    <<<<<<<<<<
    . . .
Why is this item created?

2. If I leave the assignment of ParaStyle in that way:

Code: Select all

R.AddPictureEx('Cover', CIm.Picture.Graphic, -1, rvvaBaseline);  
then the blank page above fails but the image is not centered (in spite of the image is item 1 and not 0)

3. How can I center the image vertically?

4. In Word & Co I can assign several possibilities with the property 'page break' as 'with text in one line', 'square', 'fit', 'transparent', 'behind text' ...
Does this option also exist in ScaleRichView escpecially 'behind text'? Perhaps the task could be settled in that way?
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Initially, the editor contains one empty text item.
So call R.Clear before R.AddPictureEx.

Idea 1: you can use a table (TRVTableItemInfo)
Create a table 1x1 having the height and width of the page, assign this image as a background image, position it in the center (table.BackgroundImage, table.BackgroundStyle).
Alternatively, you can insert the picture in the table cell. It can be aligned vertically using Cell.VAlign

Pluses:
- table can be automatically resized to page width (more exactly, width of main text area)
- the image is placed in the center of the table automatically
Minus:
- table height still needs calculations

Idea 2: you can use a text box (TRVTextBoxItemInfo) - a new feature of TRichView 15 and ScaleRichView 6.

Pluses:
- text box can be automatically resized to page size (more exactly, size of main text area), both width and height
- image can be placed in center (vertical position is defined in textbox.BoxProperties.VAlign
Jim Knopf
Posts: 241
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Post by Jim Knopf »

Thank you, I like idea 2.

I experimented now for hours. Is there an example how to add a textbox with AddItem and insert an Image?
Would be very helpful!

Thanks in advance ! :D
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Code: Select all

var TextBox: TRVTextBoxItemInfo;
begin
  SRichViewEdit1.RichViewEdit.Clear;

  TextBox := TRVTextBoxItemInfo.Create(SRichViewEdit1.RichViewEdit.RVData);
  TextBox.Document.AddPictureEx(...); // use a centered paragraph style
  // setting anchor areas; box properties are already configured to align
  // to the top left of anchors
  TextBox.BoxPosition.HorizontalAnchor := rvhanMainTextArea;
  TextBox.BoxPosition.VerticalAnchor := rvvanMainTextArea;
  // size
  TextBox.BoxProperties.WidthType := rvbwtRelMainTextArea;
  TextBox.BoxProperties.HeightType := rvbhtRelMainTextArea;
  TextBox.BoxProperties.Width := 100000; // 100%
  TextBox.BoxProperties.Height := 100000; // 100%
  TextBox.BoxProperties.VAlign := tlCenter;
  // box border
  TextBox.BoxProperties.Border.Style := rvbNone;

  // adding text box
  SRichViewEdit1.RichViewEdit.AddItem('', TextBox);
  // adding page break after
  SRichViewEdit1.RichViewEdit.AddNL('', 0, 0);
  SRichViewEdit1.RichViewEdit.PageBreaksBeforeItems[SRichViewEdit1.RichViewEdit.ItemCount-1] := True;
  SRichViewEdit1.Format;
end;
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Here is a final version of this code, inserting an image from Image1 at the first page of SRichViewEdit1, in a text box.
As a reminder: a text box is needed to center the picture vertically. Changing size and horizontal centering could be done without a text box.
Note: this code assumes that the header and footer on the first page do not reduce the size of the main text area, otherwise He calculation is wrong.

Code: Select all

var r: TCustomRichViewEdit;
    TextBox: TRVTextBoxItemInfo;
    GraphicItem: TRVGraphicItemInfo;
    Graphic: TGraphic;
    SV: Double;
    He, Wi: TRVStyleLength;
    {..............................................................}
    function GetCenteredParagraph(RVStyle: TRVStyle): Integer;
    var ParaStyle: TParaInfo;
    begin
      ParaStyle := TParaInfo.Create(nil);
      try
        ParaStyle.Assign(RVStyle.ParaStyles[0]);
        ParaStyle.Alignment := rvaCenter;
        ParaStyle.SpaceBefore := 0;
        ParaStyle.SpaceAfter := 0;
        ParaStyle.LeftIndent := 0;
        ParaStyle.RightIndent := 0;
        ParaStyle.FirstIndent := 0;
        ParaStyle.LineSpacingType := rvlsPercent;
        ParaStyle.LineSpacing := 100;
        Result := RVStyle.FindParaStyle(ParaStyle);
      finally
        ParaStyle.Free;
      end;
    end;
    {..............................................................}
begin
  R := SRichViewEdit1.RichViewEdit;
  R.Clear;
  TextBox := TRVTextBoxItemInfo.Create(R.RVData);
  Graphic := RVGraphicHandler.CreateGraphic(TGraphicClass(Image1.Picture.Graphic.ClassType));
  Graphic.Assign(Image1.Picture.Graphic);
  TextBox.Document.AddPictureEx('Cover', Graphic, GetCenteredParagraph(R.Style), rvvaAbsTop); // use a centered paragraph style
  // setting anchor areas; box properties are already configured to align
  // to the top left of anchors
  TextBox.BoxPosition.HorizontalAnchor := rvhanMainTextArea;
  TextBox.BoxPosition.VerticalAnchor := rvvanMainTextArea;
  // size
  TextBox.BoxProperties.WidthType := rvbwtRelMainTextArea;
  TextBox.BoxProperties.HeightType := rvbhtRelMainTextArea;
  TextBox.BoxProperties.Width := 100000; // 100%
  TextBox.BoxProperties.Height := 100000; // 100%
  TextBox.BoxProperties.VAlign := tlCenter;
  // removing border and spacing around
  TextBox.BoxProperties.Border.Style := rvbNone;
  TextBox.BoxProperties.Border.BorderOffsets.SetAll(0);
  TextBox.BoxProperties.Border.Width := 0;
  TextBox.BoxProperties.Background.BorderOffsets.SetAll(0);
  // resizing the image
  He := R.Style.PixelsToUnits(SRichViewEdit1.PageHeight100Pix - SRichViewEdit1.TopMargin100Pix - SRichViewEdit1.BottomMargin100Pix);
  Wi := R.Style.PixelsToUnits(SRichViewEdit1.PageWidth100Pix - SRichViewEdit1.LeftMargin100Pix - SRichViewEdit1.RightMargin100Pix);
  SV := Image1.Picture.Graphic.Height / Image1.Picture.Graphic.Width;
  GraphicItem := TRVGraphicItemInfo(TextBox.Document.GetItem(0));
  if Round(He/Wi) >= Round(SV) then begin
    GraphicItem.SetExtraIntProperty(rvepImageWidth, Wi);
    GraphicItem.SetExtraIntProperty(rvepImageHeight, Trunc(Wi*SV));
    end
  else begin
    GraphicItem.SetExtraIntProperty(rvepImageWidth, Trunc(He/SV));
    GraphicItem.SetExtraIntProperty(rvepImageHeight, He);
  end;
  // adding text box
  R.AddItem('', TextBox);
  // adding page break after
  R.AddNL('', 0, 0);
  R.PageBreaksBeforeItems[R.ItemCount-1] := True;
  SRichViewEdit1.Format;
end;
Jim Knopf
Posts: 241
Joined: Mon Dec 30, 2013 10:07 pm
Location: Austria
Contact:

Post by Jim Knopf »

Thank you very much for this detailed information!
Post Reply