This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v20 R8
Accessing document contents by programming

Accessing document contents by programming  


 

 

4D Write Pro provides a full set of commands allowing you to modify text and image attributes in your documents. Thanks to these features, 4D developers can design their own user interfaces (using buttons, menus, check boxes, etc.) for 4D Write Pro documents. Commands can be applied to whole documents or to specific portions (ranges), either based on user selections or custom values. Available properties include document units, margins, padding, background, paragraph settings, colors, fonts, font styles, as well as image properties. 

For example, the 4D Write Pro area library object makes extensive use of these features to provide a sophisticated 4D Write Pro interface:

4D Write Pro allows you to programmatically select and manipulate document contents. Since the selected content can include text, images, tables, etc., and also (invisible) formatting tags, 4D Write Pro works with objects called ranges.

A range is an object that represents a portion of a 4D Write Pro document:

  • a range of characters, paragraphs, pictures, or tables is defined through character positions within the parent document,
  • a range of cells, columns and rows is defined through cell positions and are anchored to the parent table.

A range is used to designate elements to be selected or manipulate attributes on a part of the document (using WP GET ATTRIBUTES and WP SET ATTRIBUTES).

There are different types of ranges. You can determine the type of a range using the wk type attribute (read-only). Each range contains several private attributes that define it:

Constant Value Comment
wk end end (Read-only attribute)
  • Range end offset, or
  • Section or subsection text end index in the document body (for subsection, text end index of the parent section).

Value type: Longint

wk owner owner (Read-only attribute) Owner of the range/object/section/subsection (reference to the document for section/subsection). Value type: Object
wk start start (Read-only attribute)
  • Range start offset, or
  • Section or subsection text start index in the document body (for subsection, text start index of the parent section).

Value type: Longint

wk type type

(Read-only attribute) Type of 4D Write Pro object. Possible values:

  • wk type default: Range or section with not defined type
  • wk type paragraph: Paragraph type range
  • wk type image: Image (anchored and inline)
  • wk type container: Header or footer, for instance
  • wk type table: Table reference

For ranges of cells, columns and rows only:

  • wk type table row: Table row reference
  • wk type table cell: Table cell reference
  • wk type table column: Table column reference

For subsections only:

  • wk first page: First page subsection
  • wk right page: Right page subsection
  • wk left page: Left page subsection

Note: Ranges of cells, columns and rows contain specific attributes (see paragraph below).

Several commands allow you to define document ranges:

You can get information about the position of a range in a document (page number, column number...) using the WP Get position command.

4D Write Pro tables are created and built programmatically by using the following commands:

(*) Columns have no equivalent in HTML. In 4D Write Pro, a column range is actually a range of cells. 

The ranges of rows, columns and cells have specific, private attributes allowing to define them: 

Constant Value Comment
wk cell count cellCount Total number of cells in the row. Value type: Longint (value for wk type table row)
wk column count columnCount (Available for tables, documents and sections) Number of columns. Value type: Longint
  • For a table: read-only attribute
  • For a document or a section: read-write attribute. Default value=1 (single column). Maximum value=20
wk first column firstColumn (Read-only attribute) Number of the first table column included in the range. Value type: Longint
wk first row firstRow (Read-only attribute) Number of the first table row included in the range. Value type: Longint
wk header row count headerRowCount

(Read/Write) Number of rows of the table with the attribute wk header set to True. 

Maximum value is 5. If you pass a value above 5, wk header is set to True for the first five rows only (see Repeated headers).

wk row count rowCount (Read-only attribute) Total number of rows. Value type: Longint
wk table table

(Read-only attribute) The parent table. Value type: Object

wk table ID tableID (Read-only attribute) ID of the parent table. Value type: String

As detailed in the Handling headers, footers, and sections paragraph, 4D Write Pro documents can contain:

  • one or more sections (one section by default)
  • for each section, up to three subsections:
    • first page subsection
    • left page(s) subsection
    • right page(s) subsection

Sections and subsections are objects can be handled programmatically using the following commands:

Note also that specific headers and footers can be attached to sections or subsections, using the following commands:

Section and subsection objects support the following attributes:

4D Write Pro allows you to create and work with dynamic references to parts of your documents, called bookmarks. A bookmark is a named reference attached to a specific range in a 4D Write Pro document.

Bookmarks are dynamic, which means that if the user moves, adds or removes text belonging to the bookmark, the associated range will be updated automatically and the bookmark will continue to reference the same content within the document. For example:

  • You create a bookmark named "MyBM" that references the "Hello world" text on page 20 of your document.
  • Then you insert 50 pages at the beginning of the document.
  • You will still be able to access the same "Hello world" text automatically, now on page 70 of the document, by means of the "MyBM" bookmark.

A document can contain an unlimited number of bookmarks. Several bookmarks can reference the same range, and bookmark ranges can be interleaved. However, each bookmark name must be unique in the document. Bookmarks are not imported when using the WP INSERT DOCUMENT command (bookmarks in the destination document cannot be overwritten).

Once created, a bookmark is stored within the document. It is saved with the document, and can be handled by several different commands. It can also be used to reference parts of a template document. These parts can then be assembled automatically with data from the database to produce dynamic output documents such as invoices or catalogs.

Several commands allow you to create, remove, and use bookmarks:

The following commands can get or set any attributes of the document:

Attributes are detailed in the 4D Write Pro Attributes section.

This command allows you to get information about style support for a range:

4D Write Pro allows you to assign hyperlinks to any target object of your document, including ranges (text, picture, etc.), elements (table, body, footer, etc.), or the whole document. For example, you can set a URL hyperlink to a picture range; if the 4D Write Pro document is exported to HTML, users can click the picture to open a page at a specified address.

Hyperlinks can also be activated from within 4D Write Pro documents using the Ctrl+click (Windows) or Cmd+click (macOS) shortcut. In a non-enterable 4D Write Pro document, a link can be activated using a simple click.

4D Write Pro supports links of the following types:

Link TypeDescription
urlLinks to web pages or to any document, opens the associated application when activated(*). Activating a URL link to 4D Write Pro document (.4wp, .4w7) replaces the current document in the 4D Write Pro area.
(*) just like the command.
bookmarkLinks to bookmarks in the document
methodActivating a link to a 4D method executes the method (provided it is registered by the method).

Hyperlinks are managed through the following commands:

  • WP SET LINK to insert a link using a target object
  • WP Get links to get the collection of all links in a target object.

Note: Links are handled as attributes, thus they can be set or get using the WP SET ATTRIBUTES and WP GET ATTRIBUTES commands along with the wk link url constant. However, we recommended using WP SET LINK and WP Get links because they automatically encode/decode hyperlinks as URLs. When reading links using the WP GET ATTRIBUTES command, if the target object contains several links, the command returns the first link string.

For example, if you want to transform the text selected by the user into a URL link to a web site:

You can write:

 $range:=WP Get selection(*;"WParea")
 WP SET LINK($range;New object("url";"http://www.4d.com"))

To remove a link from a target object, you can write either:

 WP RESET ATTRIBUTES($range;wk link url)

or

 WP SET ATTRIBUTES($range;wk link url;"")

Note: If $range does not include the whole link, the link is truncated but not entirely removed.

A style sheet is an object of defined set of attribute settings used to control the appearance of your 4D Write Pro documents. These settings can be applied to paragraphs and characters, such as the font to display, as well as the font's size, color, and weight. Once a style sheet is defined, it is saved as an object in the 4D Write Pro document so it can easily be reused. Style sheets let you give your documents a distinctive and unique appearance, while saving you time and effort. 

Default style 

All 4D Write Pro documents have a default paragraph style sheet, "Normal". New style sheets (created with the WP New style sheet command) automatically inherit from the Normal style. Attributes modified by a style sheet effect only the paragraphs they are applied to, the rest of the document retains the default Normal settings. If a style sheet is removed, the modified attributes will revert to the Normal style.

The Normal style defines a default value for every style sheet attribute in a 4D Write Pro document and can be retrieved with the WP Get style sheet command. The Normal style sheet can be modified (but not renamed) with the WP SET ATTRIBUTES command. While style sheets created with the WP New style sheet command can be deleted with the WP DELETE STYLE SHEET command, the Normal style sheet can not be deleted.

Paragraph and character style sheet attributes

Style sheets allow you to define the attributes of entire paragraphs or specific characters:

  • Paragraph - The vast majority of the attributes which can be modified apply only to paragraphs. Paragraph style sheets include character style settings, as well as formatting attributes that can only be applied at the paragraph level (e.g. margins, borders, tabs, etc.). 
  • Character - Character style sheets use only the attributes appropriate for distinquishing text (one or more characters) from the paragraph style (e.g., headers, titles, underlined text, etc.). 

Note that paragraph styles apply to whole paragraphs. To apply a style only to a specific part of a paragraph, you must use a character style sheet.

Style sheet precedence

Multiple paragraph and character style sheets can be combined within the same 4D Write Pro document. It's important to note style sheet precedence is determined by the order they are applied. 

  • If you apply a paragraph style sheet and then apply a character style sheet, the character style sheet will take precedence over the paragraph style sheet. For example, you could apply a paragraph style sheet to the body of your document, then you could create a "bold" character style sheet and apply it to specific words. The paragraph style sheet will still apply to the rest of the text, however the character style sheet will be prioritized for the designated words. 
  • If you apply a character style sheet and then apply a paragraph style sheet, the paragraph style sheet will be applied to all of the text and any character styles from the character style sheet will be removed.

Applying style sheets

Style sheets are applied with the WP SET ATTRIBUTES command using the wk style sheet or wk new line style sheet constants (or using object notation). For more information, see 4D Write Pro Attributes.

Retrieving style sheets

The WP Get style sheets command lets you retrieve all style sheets in a document according to their specific type. The WP Get style sheet command lets you retrieve a single style sheet by its name.

Importing and exporting style sheets

Because style sheets are stored as objects, they can easily be imported into other 4D Write Pro documents or maintained when exported in multiple formats. 

Style sheet attributes

The following paragraph and character style sheet attributes can be modified with the WP SET ATTRIBUTES and WP RESET ATTRIBUTES commands, or retrieved with the WP GET ATTRIBUTES command.

Note: Some attributes are linked together. Therefore when adding only one of these attributes, the other linked attributes will also be created with the default values. For linked attributes, the default value can be different than the value defined in the Normal style sheet. For example, if you define only a red left border, the other borders will be black (default value) even if the borders in the Normal style sheet were previously defined as purple.

 

AttributesParagraphCharacterLinked With
wk avoid widows and orphansX  
wk background clipX  
wk background colorX X 
wk background heightX wk background width
wk background imageX  
wk background image urlX  
wk background originX  
wk background position horizontalX wk background position vertical 
wk background position verticalX wk background position horizontal 
wk background repeatX  
wk background widthX wk background height 
wk border colorX wk border color bottom, wk border color left, wk border color right, wk border color top
wk border color bottomX wk border color, wk border color left, wk border color right, wk border color top
wk border color leftX wk border color, wk border color bottom, wk border color right, wk border color top
wk border color rightX wk border color, wk border color bottom, wk border color left, wk border color top
wk border color topX wk border color, wk border color bottom, wk border color left, wk border color right
wk border radiusX  
wk border styleX wk border style bottom, wk border style left, wk border style right, wk border style top 
wk border style bottomX wk border style, wk border style left, wk border style right, wk border style top 
wk border style leftX wk border style, wk border style bottom, wk border style right, wk border style top 
wk border style rightX wk border style, wk border style bottom, wk border style left, wk border style top
wk border style topX wk border style, wk border style, wk border style bottom, wk border style left
wk border widthX wk border width bottom, wk border width left, wk border width right, wk border width top 
wk border width bottomX wk border width, wk border width left, wk border width right, wk border width top
wk border width leftX wk border width, wk border width bottom, wk border width right, wk border width top
wk border width rightX wk border widthwk border width bottom, wk border width left, wk border width top
wk border width topX wk border widthwk border width bottom, wk border width left, wk border width right
wk directionX  
wk fontXX wk font bold, wk font italic, wk font size
wk font boldXX wk font, wk font family, wk font italic, wk font size
wk font familyXX wk font, wk font bold, wk font italic, wk font size
wk font italicXX wk font, wk font bold, wk font family, wk font size
wk font sizeXX wk font, wk font bold, wk font family, wk font italic
wk line heightX  
wk list fontX  
wk list string format LTRX  
wk list string format RTLX  
wk list style imageX  
wk list style image heightX  
wk list style image urlX  
wk list style typeX  
wk marginX wk margin bottom, wk margin left, wk margin right, wk margin top
wk margin bottomX wk margin, wk margin left, wk margin right, wk margin top
wk margin leftX wk margin, wk margin bottom, wk margin right, wk margin top
wk margin rightX wk margin, wk margin bottom, wk margin left, wk margin top
wk margin topX wk margin, wk margin bottom, wk margin left, wk margin right
wk min heightX  
wk min widthX  
wk nameXX  
wk new line style sheetX  
wk owner 
wk paddingX wk padding bottom, wk padding left, wk padding right, wk padding top
wk padding bottomX wk padding, wk padding left, wk padding right, wk padding top
wk padding boxX  
wk padding leftX wk padding, wk padding bottom, wk padding right, wk padding top
wk padding rightX wk padding, wk padding bottom, wk padding left, wk padding top
wk padding topX wk padding, wk padding bottom, wk padding left, wk padding right
_o_wk page break insideX  
wk tab defaultX wk tabs, wk tab stop offsets, wk tab stop types, wk tab stop leadings
wk tabsX wk tab default, wk tab stop offsets, wk tab stop types, wk tab stop leadings
wk tab stop offsetsX  wk tabs, wk tab default, wk tab stop types, wk tab stop leadings
wk tab stop typesX wk tabs, wk tab default, wk tab stop offsets, wk tab stop leadings
wk tab stop leadingsX wk tabs, wk tab default, wk tab stop offsets, wk tab stop types  
wk text alignX  
wk text colorXX 
wk text indentX  
wk text linethrough colorXX 
wk text linethrough styleXX wk text underline style
wk text shadow colorXX wk text shadow offset
wk text shadow offsetX X wk text shadow color
wk text transformXX 
wk text underline colorXX 
wk text underline styleXX wk text linethrough style
wk typeX (read only)X (read only) 
wk vertical alignXX 
wk widthX  



See also 

Download HDI database

 
PROPERTIES 

Product: 4D
Theme: 4D Write Pro Language

 
PAGE CONTENTS 
 
HISTORY 

Modified: 4D v16
Modified: 4D v16 R4
Modified: 4D v16 R5

 
ARTICLE USAGE

4D Write Pro Reference ( 4D v20 R8)