Working with String Template

GroupDocs.Assembly allows you to use a string as an input template and to get the output as a string instead of a document file. This is convenient when templates are stored in a database or generated by code, and when the result is consumed by the application directly.

DocumentAssembler.assembleDocument has overloads that take a Java InputStream and a Java OutputStream. In Node.js, create them through the java module (a dependency of the package), or use the StreamBuffer helper exported by the package.

Using Java byte-array streams

Follow these steps:

  • Initialize DocumentAssembler
  • Define the template string, for example "<<[yourValue]>>"
  • Convert the string to a Java byte array and wrap it into a java.io.ByteArrayInputStream
  • Create a java.io.ByteArrayOutputStream for the result and call DocumentAssembler.assembleDocument()
  • Convert the bytes of the output stream back to a string with Buffer
const groupdocs = require('@groupdocs/groupdocs.assembly');
const java = require('java');

const assembler = new groupdocs.DocumentAssembler();

// Template as a string
const sourceString = "<<[yourValue]>>";

// Wrap the string bytes into a Java input stream
const sourceBytes = java.newArray('byte', Array.from(Buffer.from(sourceString, 'utf8')));
const sourceStream = java.newInstanceSync('java.io.ByteArrayInputStream', sourceBytes);

// Collect the result in a Java output stream
const targetStream = java.newInstanceSync('java.io.ByteArrayOutputStream');

assembler.assembleDocument(sourceStream, targetStream,
    new groupdocs.DataSourceInfo("Hello, World!", "yourValue"));

// Convert the result bytes back to a string
const targetString = Buffer.from(targetStream.toByteArray()).toString('utf8');
console.log(targetString); // Hello, World!

process.exit(0);

Using the StreamBuffer helper

StreamBuffer is a java.io.ByteArrayOutputStream with two extra methods: write(chunk) accepts a Node.js Buffer, and toInputStream() returns a java.io.ByteArrayInputStream over the written bytes. It can therefore serve both as the template source and as the output target. The example also sets the input format explicitly with LoadSaveOptions and passes two data sources:

const groupdocs = require('@groupdocs/groupdocs.assembly');

const assembler = new groupdocs.DocumentAssembler();

// Template source
const source = new groupdocs.StreamBuffer();
source.write(Buffer.from('Dear <<[name]>>, your total is <<[total]:"0.00">>.', 'utf8'));

// Output target
const target = new groupdocs.StreamBuffer();

assembler.assembleDocument(source.toInputStream(), target,
    new groupdocs.LoadSaveOptions(groupdocs.FileFormat.TEXT),
    new groupdocs.DataSourceInfo("John", "name"),
    new groupdocs.DataSourceInfo(12.5, "total"));

console.log(target.toString("UTF-8")); // Dear John, your total is 12.50.

process.exit(0);
Note
The number format in the output follows the current culture (the JVM default locale). For example, with a Russian locale the result is 12,50.
Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.