4D v17WP Get position |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v17
WP Get position
|
WP Get position ( targetObj {; layout} ) -> Function result | ||||||||
Parameter | Type | Description | ||||||
targetObj | Object |
![]() |
Range or element reference or 4D Write Pro document | |||||
layout | Longint |
![]() |
4D Write Pro document layout used for evaluation: 0 (default)=4D Write Pro layout, 1=HTML WYSIWYG | |||||
Function result | Object |
![]() |
Position information | |||||
The WP Get position command returns an object describing the current position of targetObj.
In the targetObj parameter, you can pass:
The command returns the position information in an object with the following properties:
Property name | Type | Description |
section | Number | Number of the first section which intersects targetObj |
page | Number | Number of the first page which intersects targetObj |
column | Number | Index of the first column which intersects targetObj (1-based) |
line | Number | Index of the first line which intersects targetObj (relative to the column - or page if none column - 1-based) |
position | Number | Position in the line of the first character of targetObj (1-based) |
If targetObj is an empty range, WP Get position returns the position information at range start.
If targetObj references a text in header or footer, WP Get position returns the position information where the header and footer is displayed in the first section/page.
If targetObj references an anchored image (that can be replicated on several pages), WP Get position returns the position information where the image is anchored in the first section/page.
The optional layout parameter can be used to set the HTML wysiwyg view for the targetObj evaluation. You can pass one of the following constants from the "4D Write Pro" theme:
Constant | Type | Value | Comment |
wk 4D Write Pro layout | Longint | 0 | Standard 4D Write Pro layout, which can include some specific style attributes |
wk html wysiwyg | Longint | 1 | In this layout, any 4D Write Pro advanced attributes which are not compliant with all browsers are removed (e.g. columns, double underlines...) |
If layout is omitted, the 4D Write Pro layout is used by default.
The same viewing settings as for the WP PRINT command are used with WP Get position:
If targetObj is displayed in a 4D Write Pro form area, make sure that viewing settings of the area match the command viewing settings to get consistent information. Note also that if these settings are different in the area, 4D Write Pro will have to "clone" the document to calculate the layout at each call of the command, which can be time consuming. For performance reasons, it is recommended in this case to build the document offline and to copy it to the form object area only when the build is complete.
You want to know the page number of a range:
C_OBJECT($range;$rangePosition)
$range:=WP Get selection(WParea) //range from user selection
$rangePosition:=WP Get position($range;wk 4D Write Pro layout)
ALERT("First page number in selection is "+String($rangePosition.page))
You want to set to blue color the text in the first and last pages of the document:
C_LONGINT($nbPages)
C_OBJECT($body)
C_COLLECTION($_paragraphs)
$nbPages:=WP Get page count([DOC]Sample)
$body:=WP Get body([DOC]Sample)
$_paragraphs:=WP Get elements($body;wk type paragraph)
For each($paragraph;$_paragraphs)
$info:=WP Get position($paragraph)
If($info.page=1)|($info.page=$nbPages) //first and last page paragraphs in blue
WP SET ATTRIBUTES($paragraph;wk text color;"blue")
Else //other pages paragraphs in black
WP SET ATTRIBUTES($paragraph;wk text color;"black")
End if
End for each
While building a document, you want to avoid paragraph splits:
C_OBJECT(WParea;$body)
C_COLLECTION($_paragraphs)
WParea:=WP New
$body:=WP Get body([DOC]Sample)
$_paragraphs:=WP Get elements($body;wk type paragraph)
For each($paragraph;$_paragraphs)
$insert:=WP New($paragraph)
//memorize current range and page number
$rangeBefore:=WP Create range(WParea;wk end text;wk end text)
$info:=WP Get position($rangeBefore)
$memoPage:=$info.page
//insert the contents, keep the range untouched
WP INSERT DOCUMENT($rangeBefore;$insert;wk append;wk exclude from range)
//check the position after insertion
$rangeAfter:=WP Create range(WParea;wk end text;wk end text)
$info:=WP Get position($rangeAfter)
If($memoPage#$info.page) // if the page has changed, insert a page break
WP INSERT BREAK($rangeBefore;wk page break;wk replace)
End if
End for each
Product: 4D
Theme: 4D Write Pro Language
Number:
1577
Created: 4D v17
4D Write Pro Reference ( 4D v17)