Use custom Power Fx functions
Docentric Template Designer provides custom Power Fx helper functions that extend the standard Power Fx language capabilities. You can use these functions in the Power Fx editor and script view when binding tagging elements to data in your document templates. These functions help you work with GS1 barcodes, download external content, and encode or decode data.
Prerequisites
- Docentric Template Designer installed in Microsoft Word or Excel
- A document template with a loaded Data Source
- Basic understanding of Power Fx expressions
Encode and decode data
Encoding and decoding functions help you convert data between different formats. Use these functions when working with Base64-encoded data or when you need to encode binary content.
Decode Base64 to binary content
Use the Base64ToBinary function to decode a Base64-encoded string to binary content. In Dataverse, file attachments (Notes, Email Attachments, and Annotations) are stored as Base64-encoded strings in fields like documentbody or body. Use this function to convert those Base64 strings back to binary format so they can be rendered as images or included as subdocuments in your generated documents. This function is also useful when working with data from Power Automate flows or custom APIs that return Base64-encoded files.
- Open the binding control for an Image or Subdocument tagging element.
- Switch to Script view.
- Enter the
Base64ToBinaryfunction with the Base64-encoded string.
Example:
// Decodes a Base64-encoded HTML document from a Dataverse attachment field
// Common scenario: Converting Note.DocumentBody to binary for use in a Subdocument tagging element
Base64ToBinary(Note.DocumentBody)
Real-world scenarios:
- Include email attachments in documents: Use
Base64ToBinary(EmailAttachment.Body)to convert an email attachment stored in Dataverse into binary format for rendering in an Image or Subdocument tagging element. - Display note attachments as images: Use
Base64ToBinary(Note.DocumentBody)to convert a note attachment (like a company logo or signature) into an image in your document template. - Embed files from custom fields: If you store Base64-encoded files in custom text fields, decode them with this function to include them as images or subdocuments in generated documents.
Encode text to Base64
Use the StringToBase64 function to encode text as Base64. This is useful when you need to pass text data to external systems or APIs that require Base64-encoded input, or when storing text data in fields that expect Base64 format.
- Open the binding control for a Field tagging element.
- Switch to Script view.
- Enter the
StringToBase64function with the text to encode.
Example:
// Encodes a text string to Base64 format
StringToBase64("This is a test string")
Decode text from Base64
Use the Base64ToString function to decode Base64-encoded text. This is useful when working with data from external systems that return Base64-encoded text strings, or when you need to display text that was previously encoded.
- Open the binding control for a Field tagging element.
- Switch to Script view.
- Enter the
Base64ToStringfunction with the Base64-encoded text.
Example:
// Decodes a Base64-encoded string back to readable text
Base64ToString("VGhpcyBpcyBhIHRlc3Qgc3RyaW5n")
Encode binary content to Base64
Use the BinaryToBase64 function to encode binary content as a Base64 string. This is useful when you need to display binary data as text, pass binary content to web services or APIs, or create data URIs for embedding images in HTML content.
- Open the binding control for a Field tagging element.
- Switch to Script view.
- Enter the
BinaryToBase64function with the binary data field.
Example:
// Encodes a binary image field to Base64 string format
BinaryToBase64(Quote.HeaderImage)
Use GS1 barcode functions
GS1 barcode functions help you generate barcodes with GS1 application identifiers. Use these functions when binding Barcode tagging elements.
Create a single GS1 barcode element
Use the Gs1Element function to create a barcode with a single GS1 value.
- Insert a Barcode tagging element in your template.
- Open the binding control for the Barcode tagging element.
- Switch to Script view.
- Enter the
Gs1Elementfunction with the required parameters:- Identifier: The GS1 application identifier (see GS1 Application Identifiers)
- Value: A valid value for the identifier
- GenerateControlCharacter (optional): Set to
trueto automatically calculate and append a check character for elements that need it
Examples:
// Creates a GS1 barcode for Global Trade Item Number (GTIN) with a complete value including check digit
Gs1Element("01","00000000030007")
// Creates a GS1 barcode for GTIN and automatically calculates the check digit
Gs1Element("01","0000000003000", true)
NOTE
The Gs1Element function validates a value against the GS1 element rules for the specified Application Identifier (AI). If the value doesn’t meet the GS1 specification, document generation reports an error. This helps ensure the GS1 data encoded in your barcodes is valid.
Combine multiple GS1 values
Use the Gs1Data function to combine multiple GS1 values into a single barcode.
- Insert a Barcode tagging element in your template.
- Open the binding control for the Barcode tagging element.
- Switch to Script view.
- Enter the
Gs1Datafunction with one or moreGs1Elementfunctions as parameters.
Example:
// Creates a composite GS1 barcode with multiple data elements:
// - AI 01: GTIN with auto-calculated check character
// - AI 13: Packaging date (YYMMDD format)
// - AI 30: Variable count (quantity)
Gs1Data(
Gs1Element("01","0000000003000", true),
Gs1Element("13", "200101"),
Gs1Element("30", "64")
)
Download external content
Download functions allow you to retrieve content from HTTP addresses during document generation. Use these functions when you need to include external files or text in your templates.
Download a file from a URL
Use the DownloadFile function to download a file from an HTTP address. The downloaded file can be used as input for tagging elements that require binary data, such as Subdocument or Image tagging elements.
- Insert an Image or Subdocument tagging element in your template.
- Open the binding control for the tagging element.
- Switch to Script view.
- Enter the
DownloadFilefunction with the required parameters:- Url: The URL of the file to download
- SuppressError (optional): Set to
trueto ignore any errors and generate the document without the file
Examples:
// Downloads a company logo from a URL to include in the document
DownloadFile("https://example.com/images/logo.png")
// Downloads an image file and suppresses errors if the download fails
DownloadFile("https://example.com/images/logo.png", true)
Download text content from a URL
Use the DownloadText function to download text content from an HTTP address.
- Insert a Field tagging element in your template.
- Open the binding control for the Field tagging element.
- Switch to Script view.
- Enter the
DownloadTextfunction with the required parameters:- Url: The URL of the text content
- SuppressError (optional): Set to
trueto ignore any errors and generate the document without the downloaded content
Examples:
// Downloads text content from a URL, such as terms and conditions to include in a contract
DownloadText("https://example.com/terms.txt")
// Downloads text content and continues document generation even if the download fails
DownloadText("https://example.com/terms.txt", true)