<< Click to Display Table of Contents >> Navigation: Programming > WPTools Premium > Text boxes > Low level textbox creation > TextObjects.InsertTextBox |
Alternatively you can also use WPRichText1.TextObjects.InsertTextBox:
procedure TForm1.WPRichText1DragDrop(Sender, Source: TObject; X,
Y: Integer);
var
aField : String;
w, h: Integer;
txtobj: TWPTextObj;
RTFDataBlock : TWPRTFDataBlock;
PageNr, PageXTW, PageYTW : Integer;
par : TParagraph;
begin
If (Source=FieldList) And (FieldList.ItemIndex>=0) And
WPRichText1.GetPageXYfromXY(X,Y,PageNr, PageXTW, PageYTW,par) Then
begin
aField := FieldList.Items[FieldList.ItemIndex];
w := WPCentimeterToTwips(5);
h := WPCentimeterToTwips(0.5);
// Insert the box
WPRichText1.TextCursor.MoveTo(par,0,False);
txtobj := WPRichText1.TextObjects.InsertTextBox(w,h,RTFDataBlock);
// Set contents
RTFDataBlock.RtfText.AsString := aField; // HTML would be possible or RTF !
// Now update the txtobj
txtobj.Source := aField;
txtobj.PositionMode := wpotPage;
txtobj.Wrap := wpwrNone;// Important For page objects
txtobj.RelX := PageXTW; // X In twips !
txtobj.RelY := PageYTW; // Y In twips !
// We want To resize it freely
// disable the autosizing (but at least the contents must fit!)
txtobj.Mode := txtobj.Mode + [wpobjDisableAutoSize];
End;
End;
Tip: To create a text box which is automatically positioned in the margin of the page use the modes wpobjLockedPos,wpobjPositionInMargin
Tip: You can assign a name to a text box. Use the property TWPTextObj.Source (not property Name!). You can move the cursor to this text box using the code
WPRichText1.CodeMoveTo('name', wpobjImage, [], [wpCompareSource]);
WPRichText1.SetFocus;
This low level code can be used to create a text box in the header text:
var par : TParagraph;
block, block2 : TWPRTFDataBlock;
obj : TWPTextObj;
begin
block := WPRichText1.HeaderFooter.Get(wpIsHeader,wpraOnAllPages,'');
block.Clear(true);
par := block.AppendNewPar();
// We create the box directly in the header (par!)
obj := WPRichText1.TextObjects.InsertTextBox(1440, 1440, block2, par);
if obj <> nil then
begin
obj.Frame := [wpframe1pt];
obj.RelX := WPRichText1.Header.LeftMargin;
// Create a TWPRTFDataBlock for this box
par := block2.AppendNewPar();
par.Append('Some Text in box');
end;