Tech Tip: Adding alternating text to a 4D Write Pro document via code
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: June 28, 2018
When working with a 4D Write Pro area, a devloper may find themself needing to add text with alternating alignment to an existing document. For example, they may need to add some right aligned text followed by some left aligned text.
Here is some sample code that accomplishes this task:
C_OBJECT($range;$someText) C_POINTER($ptr) // get pointer to Write Pro Area $ptr:=OBJECT Get pointer(Object named;"WriteProArea") // create some right align text & insert @ end of document $someText:=WP New ST SET TEXT($someText;"This text is on the right") WP SET ATTRIBUTES($someText;wk text align;wk right) $range:=WP Create range($ptr->;wk end text;wk end text) WP INSERT DOCUMENT($range;$someText;wk append) // add line break to end of document $range:=WP Create range($ptr->;wk end text;wk end text) WP INSERT BREAK($range;wk line break;wk append) // create some left aligned text & insert @ end of document ST SET TEXT($someText;"This text is on the left!") WP SET ATTRIBUTES($someText;wk text align;wk left) $range:=WP Create range($ptr->;wk end text;wk end text) WP INSERT DOCUMENT($range;$someText;wk append) |
The example above performed the following actions:
- Get a pointer to the 4D Write Pro area ($ptr)
- Create a temporary 4D Write Pro document in memory ($someText)
- Add text to the temporary document in memory ($someText)
- Align text to the right in the temporary document ($someText)
- Create a range at the end of the 4D Write Pro area ($ptr)
- Insert the temporary document ($someText) into the 4D Write Pro area ($ptr)
- Insert a line break into the 4D Write Pro area ($ptr)
- Reuse the temporary 4D Write Pro document adding new text ($someText)
- Align text to the left in the temporary document ($someText)
- Create a range at the end of the 4D Write Pro area ($ptr)
- Insert the temporary document ($someText) into the 4D Write Pro area ($ptr)