Skip to main content

FormTemplate Class

A reusable form XObject that contains a collection of page fragments.

Declaration

public class FormTemplate extends JavaObject implements ICloneable

Remarks

A FormTemplate stores graphical content, such as text and images, that can be reused multiple times in a PDF document. Use FormFragment to render a form template on a page.

The following code snippet creates a form template with text and a separator line, and adds the template to a page:

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();

            // 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);

            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);
            }
        }
    }
}

Refer to the following help topic for additional information: Create Reusable PDF Templates.

Implements

com.devexpress.system.ICloneable

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.initFields()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

Object
com.devexpress.system.JavaObject
FormTemplate

FormTemplate()

Initializes a new instance of the FormTemplate class.

Declaration

public FormTemplate()

Methods

addFragment(PageFragment fragment) Method

Adds the specified page fragment to the form template.

Declaration

public void addFragment(PageFragment fragment)

Parameters

Name Type Description
fragment PageFragment

The page fragment to add.

addImageFragment(DXImage image, PointF location) Method

Adds an image fragment with the specified image and location to the form template.

Declaration

public ImageFragment addImageFragment(DXImage image, PointF location)

Parameters

Name Type Description
image DXImage

The image to add.

location com.devexpress.system.drawing.PointF

The image location.

Returns

Type Description
ImageFragment

The created ImageFragment object.

addTextFragment(String text, float x, float y) Method

Adds a text fragment with the specified text and location to the form template.

Declaration

public TextFragment addTextFragment(String text, float x, float y)

Parameters

Name Type Description
text String

The text content.

x float

The X-coordinate of the text location.

y float

The Y-coordinate of the text location.

Returns

Type Description
TextFragment

The created TextFragment object.

clone(DocumentCloneContext context) Method

Creates a copy of the current object.

Declaration

public FormTemplate clone(DocumentCloneContext context)

Parameters

Name Type Description
context DocumentCloneContext

The context used to clone document objects.

Returns

Type Description
FormTemplate

A copy of the current FormTemplate instance.

deepClone() Method

Creates a deep copy of the current object.

Declaration

public FormTemplate deepClone()

Returns

Type Description
FormTemplate

A deep copy of the current object.

getBounds() Method

Returns the bounding rectangle of the form template.

Declaration

public RectangleF getBounds()

Returns

Type Description
RectangleF

The bounding rectangle.

getFragments() Method

Returns the collection of page fragments contained in the form template.

Declaration

public IFragmentCollection getFragments()

Returns

Type Description
IFragmentCollection

The collection of page fragments.

getGroup() Method

Returns the transparency group associated with the form template.

Declaration

public FormTransparencyGroup getGroup()

Returns

Type Description
FormTransparencyGroup

The transparency group.

getMatrix() Method

Returns the transformation matrix applied to the form template.

Declaration

public DXMatrix getMatrix()

Returns

Type Description
DXMatrix

The transformation matrix.

objectClone() Method

Creates a copy of the current object.

Declaration

public Object objectClone()

Returns

Type Description
Object

A copy of the current object.

setBounds(RectangleF value) Method

Sets the bounding rectangle of the form template.

Declaration

public void setBounds(RectangleF value)

Parameters

Name Type Description
value RectangleF

The bounding rectangle.

setGroup(FormTransparencyGroup value) Method

Sets the transparency group associated with the form template.

Declaration

public void setGroup(FormTransparencyGroup value)

Parameters

Name Type Description
value FormTransparencyGroup

The transparency group.

setMatrix(DXMatrix value) Method

Sets the transformation matrix applied to the form template.

Declaration

public void setMatrix(DXMatrix value)

Parameters

Name Type Description
value DXMatrix

The transformation matrix.