Skip to main content

FormFragment Class

A visual fragment that draws a reusable form template on a PDF page.

Declaration

public class FormFragment extends VisualFragment

Remarks

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 FormFragment based on a template and add it to a page. A form fragment defines where and how the template appears.

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.

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
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
JavaObject

FormFragment(FormTemplate formTemplate)

Initializes a new instance of the FormFragment class with the specified form template.

Declaration

public FormFragment(FormTemplate formTemplate)

Parameters

Name Type Description
formTemplate FormTemplate

The form template to render.

Methods

getFillOpacity() Method

Returns the opacity applied to filled content in the form template.

Declaration

public @Nullable Double getFillOpacity()

Returns

Type Description

The opacity. A value of 1.0 specifies fully opaque content. A value of 0.0 specifies fully transparent content.

getFormTemplate() Method

Returns the form template rendered by the fragment.

Declaration

public FormTemplate getFormTemplate()

Returns

Type Description
FormTemplate

The form template.

getStrokeOpacity() Method

Returns the opacity applied to stroked content in the form template.

Declaration

public @Nullable Double getStrokeOpacity()

Returns

Type Description

The opacity. A value of 1.0 specifies fully opaque content. A value of 0.0 specifies fully transparent content.

setFillOpacity(@Nullable Double value) Method

Sets the opacity applied to filled content in the form template.

Declaration

public void setFillOpacity(@Nullable Double value)

Parameters

Name Type Description
value

The opacity. A value of 1.0 specifies fully opaque content. A value of 0.0 specifies fully transparent content.

setStrokeOpacity(@Nullable Double value) Method

Sets the opacity applied to stroked content in the form template.

Declaration

public void setStrokeOpacity(@Nullable Double value)

Parameters

Name Type Description
value

The opacity. A value of 1.0 specifies fully opaque content. A value of 0.0 specifies fully transparent content.