Skip to main content

Create Reusable PDF Templates

  • 3 minutes to read

The PDF Document API allows you to create reusable content containers in a PDF document. A FormTemplate object is a Form XObject that defines a self-contained content block that can contain text, images, and graphics.

You can reuse a template across multiple pages without recreating its content. Each instance behaves as an independent object with its own position, scale, and rotation.

Create a Form Template

Create a FormTemplate object and specify the template bounds. The bounds define the visible area of the template. Content outside these bounds is clipped.

The following code snippet creates a form template with text and a separator line:

FormTemplate headerTemplate = new FormTemplate();

// Define template bounds (width and height of reusable area).
headerTemplate.setBounds(new RectangleF(0, 0, (float)page.getWidth(), 150));

// Add text fragments to the template.
headerTemplate.addTextFragment("Company Name: DevExpress", 50, 140);
headerTemplate.addTextFragment("Invoice Date: " + java.time.LocalDate.now(), 50, 120);
headerTemplate.addTextFragment("Invoice Number: 1001", 50, 100);

// Create a horizontal line.
GraphicsPath line = new GraphicsPath(new PointF(0, 80));
line.appendLineSegment(new PointF(headerTemplate.getBounds().getWidth(), 80));

PathFragment lineFragment = new PathFragment();
lineFragment.getPaths().add(line);
lineFragment.setFill(Fill.createSolid(PdfColor.getDarkOrange()));

// Add the line to the template.
headerTemplate.getFragments().add(lineFragment);

Add a Template to a Page

Create a FormFragment based on a template and add it to a page. A form fragment defines where and how the template appears.

import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.*;

import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try (PdfDocument pdfDocument = new PdfDocument()) {

            // Add a page to the document.
            Page page = pdfDocument.getPages().add(DXPaperKind.LETTER);

            // Create a reusable form template (content defined earlier in the topic).
            FormTemplate headerTemplate = new FormTemplate();

            // ...

            FormFragment form = new FormFragment(headerTemplate);
            form.setLocation(new PointF(0, (float)page.getHeight() - 160));
            page.addFragment(form);

            // Save the document to a PDF file.
            try (WritableByteChannel writableByteChannel =
                     FileChannel.open(Path.of("result.pdf"),
                         StandardOpenOption.CREATE,
                         StandardOpenOption.WRITE,
                         StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

Reuse a Template Across Multiple Pages

You can reuse the same template on multiple pages. Each FormFragment instance can have its own position, scale, or rotation.

// Add the first page to the document.
Page firstPage = pdfDocument.getPages().add(DXPaperKind.LETTER);

// Add the second page to the document.
Page secondPage = pdfDocument.getPages().add(DXPaperKind.LETTER);

// Add the 'headerTemplate' template to the first page.
FormFragment firstForm = new FormFragment(headerTemplate);
firstForm.setLocation(new PointF(50, (float)page.getHeight() - 160));
page.addFragment(firstForm);

// Add the 'headerTemplate' template to the second page.
FormFragment secondForm = new FormFragment(headerTemplate);
secondForm.setLocation(new PointF(50, 0));
secondPage.addFragment(secondForm);

Next Steps