Quick Start: Create a PDF with Java

Create a product-label PDF, then change its product name and run the example again. This Windows Java example uses the same layout and data fields as the .NET Quick Start:

  1. Load the supplied ProductLabel.tff layout.
  2. Provide ProductName and ArticleNumber.
  3. Generate the PDF.

Check the Prerequisites

You need:

  • 64-bit Windows;
  • a 64-bit Java Development Kit (JDK), Java 8 or later, with java and javac available in PowerShell; and
  • the TFORMer SDK for 64-bit Windows, including its Java component and native runtime. See Windows installation.

Use the JAR and native libraries from the same SDK installation, with the same architecture as Java. Unlike the .NET Quick Start, this example uses the installed SDK, not NuGet. The supplied layout uses Arial.

For Linux integration, use the Java guide; the commands below are for Windows.

Download and Open the Example

Download the complete Java example (ZIP) — includes ProductLabel.java, the shared ProductLabel.tff layout and a README.1

Extract the ZIP and open PowerShell in that folder. Keep the Java source and layout together; no layout creation or field-name replacement is required.

Build and Run

Set sdkBin to your SDK's Bin directory if it differs from the path below, then run:

$sdkBin = 'C:\Program Files\TEC-IT\TFORMer9 SDK\Bin'
javac -cp "$sdkBin\JTFORMer9.jar" .\ProductLabel.java
if ($LASTEXITCODE -ne 0) { throw 'Java compilation failed.' }
java "-Djava.library.path=$sdkBin" -cp ".;$sdkBin\JTFORMer9.jar" ProductLabel
if ($LASTEXITCODE -ne 0) { throw 'Product-label generation failed.' }

The compiler reads the TFORMer JAR from that directory. The start command makes the matching JNI library available to this Java process; it does not change system-wide library paths.

Check the Result

Open the PDF at the absolute path printed after Created PDF:. Each run saves ProductLabel.pdf in a new temporary folder, leaving previous output unchanged.

Java example output: Coffee 250 g with article number ART-1001.

The label shows Coffee 250 g and ART-1001; its QR Code contains the same article number. This is an internal identifier, not an EAN, GTIN, or GS1 value.

Without license configuration, TFORMer may mark the PDF as evaluation output.

Change the Product Name

In ProductLabel.java, replace the value Coffee 250 g with another short product name. Keep the field name ProductName unchanged. Run the compile and start commands again, then open the new PDF.

Complete Program

The program reads ProductLabel.tff from the current working directory and supplies the two fields used by that layout. It releases the native job resources after output, including on failure.

Show complete ProductLabel.java
import com.tecit.TFORMer.TFormerException;
import com.tecit.TFORMer.Enumerations.EPrinterType;
import com.tecit.TFORMer.Printing.Job;
import com.tecit.TFORMer.Printing.JobDataRecordSet;
import com.tecit.TFORMer.Printing.JobDataRecordSet.Record;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ProductLabel {
    public static void main(String[] args) {
        try {
            Path output = createPdf();
            System.out.println("Created PDF: " + output);
        } catch (Exception | LinkageError exception) {
            System.err.println("Cannot create product label: " + exception);
            System.exit(1);
        }
    }

    private static Path createPdf() throws IOException, TFormerException {
        Path layout = Paths.get("ProductLabel.tff").toAbsolutePath();
        if (!Files.isRegularFile(layout)) {
            throw new IOException("Layout file not found: " + layout
                    + ". Run from the extracted sample folder.");
        }

        Path output = Files.createTempDirectory("TFORMerProductLabel-")
                .resolve("ProductLabel.pdf").toAbsolutePath();
        Job job = new Job();
        try {
            job.setRepositoryName(layout.toString());

            // These field names match the supplied ProductLabel.tff layout.
            JobDataRecordSet data = new JobDataRecordSet();
            Record record = new JobDataRecordSet.Record();
            record.setData("ProductName", "Coffee 250 g");
            record.setData("ArticleNumber", "ART-1001");
            data.add(record);
            job.setJobData(data);

            job.setPrinterType(EPrinterType.PDFFile);
            job.setOutputName(output.toString());
            job.print();
        } finally {
            job.dispose();
        }

        if (!Files.isRegularFile(output) || Files.size(output) == 0) {
            throw new IOException("TFORMer did not create a non-empty PDF: "
                    + output);
        }
        return output;
    }
}

Continue in the API Guide

Continue with the Java API guide for JNI setup, the general workflow and additional examples.


  1. When reading an offline PDF copy, keep its accompanying downloads folder beside it so that the example link works. ↩

Search documentation

Search in
More scopes
Additional scopes

All contents