Generate Output in Memory

Use PrintToStream to generate PDF output in a MemoryStream, for example to pass the PDF to your application as a download response.

Setup: .NET assemblies and NuGet package. For a supplied project and layout, see the Quick Start.

Using PrintToStream

Select PrinterType.PdfFile to choose the PDF format; PrintToStream writes the output to the supplied stream. No output filename is needed. The following excerpt assumes that job already has its form layout and data configured.

job.PrinterType = PrinterType.PdfFile;

byte[] pdfData;
using (MemoryStream stream = new MemoryStream())
{
    job.PrintToStream(stream);
    pdfData = stream.ToArray();
}

ToArray() copies the generated PDF into pdfData. The array remains available after the stream is disposed, within the block where pdfData is declared.

Complete Example

The complete method below includes the layout and data setup. Replace the layout filename and data-field name before calling CreatePdfInMemory(). For assembly references and runtime requirements, see Assemblies and NuGet Package; the basic workflow describes the general steps.

using System;
using System.IO;
using TECIT.TFORMer;

static void CreatePdfInMemory()
{
    try
    {
        using (Job job = new Job())
        {
            // Select the form layout.
            job.RepositoryName = "FILENAME_OF_YOUR_FORMLAYOUT.tff";

            // Provide the data.
            JobDataRecordSet data = new JobDataRecordSet();
            Record record = new Record();
            record.Data.Add(
                "NAME_OF_YOUR_DATAFIELD", "Value of the data field");
            data.Records.Add(record);
            job.JobData = data;

            // Generate PDF output.
            job.PrinterType = PrinterType.PdfFile;

            byte[] pdfData;
            using (MemoryStream stream = new MemoryStream())
            {
                job.PrintToStream(stream);
                pdfData = stream.ToArray();
            }

            // Use pdfData here, after the stream has been disposed.
            Console.WriteLine("PDF size: {0} bytes", pdfData.Length);
            // End PDF output.
        }
    }
    catch (TFORMerException exception)
    {
        Console.Error.WriteLine(exception.Message);
    }
}

The method prints the PDF byte count. Use pdfData at the marked location to pass the PDF to your application. It does not write a PDF file.

For output that produces several streams, use the PrintToStreams example. It shows the stream-creation and close callbacks for page-based image output.

Search documentation

Search in
More scopes
Additional scopes

All contents