WPCubed Product Support

How to position or create a textbox on a specific page?

Thank you. Had to rethink my approach but it is working now. 🙂👍🏻

#3 ♥ 0
3.5k Views 1 links 2 users 1 ♥ read 3 min
wpsupport

How can I create a textbox on a specific page?

Textboxes are connected to a paragraph. To have it on a specify page you need to attach it to a paragraph located on that page. If there is a paragraph, you can position it relative to it or by X,Y on the page.

Maybe check out the event OnGetSpecialText - you can have header or footer individually for each page. But the placement logic is on you and you cannot use RTF format to save and load anymore.

#2 ♥ 1

I've been looking through the textbox related demos but didn't find a solution for the problem stated in the subject above:

How can I create a textbox on a specific page?

I've created the following code:

function TForm1.CreateTextBox(const AName, AContent: String; const AWidth,
  AHeight, AXOffset, AYOffset: Integer; const AAlign: TAlignSet;
  const ARTFPageNr: Integer): TWPTextObj;
var
  txtBox: TWPORTFTextBox;
  txtObj: TWPTextObj;
  i: Integer;
  hasTextBox: Boolean;
begin

  // Check if a textbox with the given name already exists.

  // If yes, update it. If not, create it first.
  hasTextBox := FALSE;
  for i := 0 to WP.TextObjects.ObjCount - 1 do
    begin
      txtObj := WP.TextObjects.ObjList[ i ];
      if txtObj.CompareName( AName ) then
        begin
          hasTextBox := TRUE;
          break;
        end;
    end;

  if not hasTextBox then
    begin
      txtBox := TWPORTFTextBox.Create( WP );
      txtBox.ObjName := AName;
    end
  else
    txtBox := TWPORTFTextBox( txtObj.ObjRef );

  txtBox.WidthTW := AWidth;
  txtBox.HeightTW := AHeight;
  txtBox.AsString := AContent;

  if not hasTextBox then
    Result := WP.TextObjects.InsertMovableImage( txtBox )
  else
    Result := txtObj;

  if Assigned( Result ) then
    begin
      Result.ParentRTFPage := ARTFPageNr; // I assumed this will place the textbox on the given page

      Result.RelX := AXOffset;
      Result.RelY := AYOffset;

      if alRight in AAlign then
        Result.RelX := Result.RelX - Result.Width;

      if alBottom in AAlign then
        Result.RelY := Result.RelY - Result.Height;
    end;

  WP.ReformatAll( TRUE, TRUE );
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Create a table with several columns and rows
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  i: Integer;
begin

  // Go through all pages and create a textbox on each page.
  for i := 0 to WP.PageCount - 1 do
    CreateTextBox( Format( '%s_%d', [ 'footer', i + 1 ] ),
      Format( '<p align=right>%s</p>', [ Random( MaxInt ).ToString ] ),
      2000, 0,
      WP.Header.PageWidth - WP.Header.LeftMargin,
      WP.Header.PageHeight - WP.Header.TopMargin - WP.Header.BottomMargin,
      [ alRight ],
      i + 1 );
end;

After the app started I click on Button1 to create a table with enough content for 7 pages. Then I click Button3 to create the text boxes. But the problem is that all text boxes are put on page 1:

🖼 jUxv7Ie.png (i.imgur.com)

#1 ♥ 0