Generate Output in Memory

Use PrintToStream when the selected output format produces one stream. The method returns the generated output as a Variant containing a byte array, which you can pass directly to another component without creating a temporary file.

For output that creates several streams, use the PrintToStreams example. It shows how to connect the VBA or VBScript event receiver and handle each stream's data chunks.

Setup: COM installation and setup. For a supplied project and layout, see the Quick Start.

Using PrintToStream

Select PDF output and assign the result of PrintToStream to a Variant. No output filename is needed. The following excerpt assumes that printJob already has its form layout and data configured.

Const PdfFileOutput As Long = 6

Dim pdfData As Variant
printJob.PrinterType = PdfFileOutput
pdfData = printJob.PrintToStream

pdfData now contains the complete PDF as a VBA byte array.

Complete Example

The complete late-bound macro below includes the layout and data setup. Replace the layout filename and data-field name before calling CreatePdfInMemory. Apply your license as described in Licensing when required.

Option Explicit
Private Sub CreatePdfInMemory()
    Const PdfFileOutput As Long = 6
    Dim tformer As Object
    Dim printJob As Object
    Dim jobData As Object
    Dim pdfData As Variant
    Set tformer = CreateObject("TFORMer9Lib.TFORMer")
    Set printJob = tformer.CreateJob
    ' Select the form layout.
    printJob.RepositoryName = "C:\path\to\layout.tff"
    ' Provide the data.
    Set jobData = printJob.NewJobDataRecordSet
    jobData.AddNewRecord
    jobData.SetDataField "NAME_OF_YOUR_DATAFIELD", "Value"
    ' Generate PDF output in memory.
    printJob.PrinterType = PdfFileOutput
    pdfData = printJob.PrintToStream
    ' Use pdfData here, for example as the body of a response.
    Debug.Print "PDF size: " & _
        (UBound(pdfData) - LBound(pdfData) + 1) & " bytes"
End Sub

The macro prints the byte count in the VBA Immediate window. It does not write a PDF file.

Search documentation

Search in
More scopes
Additional scopes

All contents