Skip to main content

TableAttribute Class

Defines table-related attributes for tagged PDF structure elements.

Declaration

public class TableAttribute extends StructureElementAttribute

Remarks

Refer to the following help topic for additional information: Generate and Edit Tagged PDF Documents.

The following code snippet creates a tagged PDF document that contains an accessible invoice. It builds a logical structure tree with a document, section, heading, table, and paragraph elements.

The example assigns layout and table attributes, adds text fragments to the corresponding structure elements, and saves the document as a tagged PDF.

package pdf;

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

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

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

            Page page = document.getPages().add(DXPaperKind.A4);

            // Create the root Document element.
            StructureElement doc = document.getStructureTree()
                    .addChildElement(Pdf17StructureType.DOCUMENT);

            // Add a section to the document.
            StructureElement section =
                    doc.addChildElement(Pdf17StructureType.SECT);

            // Add a heading.
            StructureElement heading =
                    section.addChildElement(Pdf17StructureType.H1);
            heading.addFragment(page, new TextFragment(){{
                    setText("Invoice");
                    setLocation(new PointF(50, 800));
                    setFont(new TextFont("Arial", TextFontStyle.BOLD));
                    setFontSize(24);}});

            // Create a table with layout attributes.
            StructureElement table =
                    section.addChildElement(Pdf17StructureType.TABLE);

            table.getAttributes().add(new LayoutAttribute() {{
                    setPlacement(LayoutPlacement.BLOCK);
                    setWritingMode(WritingMode.LEFT_TO_RIGHT);
                }});

            table.getAttributes().add(
                    new TableAttribute() {{
                        setSummary("Invoice items");
                    }});

            StructureElement header =
                    table.addChildElement(Pdf17StructureType.T_HEAD);
            StructureElement headerRow =
                    header.addChildElement(Pdf17StructureType.TR);

            // Add table header cells.
            StructureElement th1 =
                    headerRow.addChildElement(Pdf17StructureType.TH);
            th1.getAttributes().add(
                    new TableAttribute(){{
                        setScope(TableScope.COLUMN);
                    }});
            th1.addFragment(page, new TextFragment() {
                {
                    setText("Item");
                    setLocation(new PointF(50, 700));
                    setFont(new TextFont("Arial", TextFontStyle.BOLD));
                    setFontSize(12);
                }});

            StructureElement th2 =
                    headerRow.addChildElement(Pdf17StructureType.TH);
            th2.getAttributes().add(
                    new TableAttribute() {{
                        setScope(TableScope.COLUMN);
                    }});
            th2.addFragment(page, new TextFragment() {{
                    setText("Qty");
                    setLocation(new PointF(250, 700));
                    setFont(new TextFont("Arial", TextFontStyle.BOLD));
                }});

            StructureElement th3 =
                    headerRow.addChildElement(Pdf17StructureType.TH);
            th3.getAttributes().add(
                    new TableAttribute() {{
                        setScope(TableScope.COLUMN);
                    }});
            th3.addFragment(page, new TextFragment() {{
                    setText("Price");
                    setLocation(new PointF(320, 700));
                    setFont(new TextFont("Arial", TextFontStyle.BOLD));
                }});

            StructureElement th4 =
                    headerRow.addChildElement(Pdf17StructureType.TH);
            th4.getAttributes().add(
                    new TableAttribute() {{
                        setScope(TableScope.COLUMN);
                    }});
            th4.addFragment(page, new TextFragment() {{
                    setText("Total");
                    setLocation(new PointF(400, 700));
                    setFont(new TextFont("Arial", TextFontStyle.BOLD));
                }});

            // Add table body with a data row.
            StructureElement body =
                    table.addChildElement(Pdf17StructureType.T_BODY);
            StructureElement dataRow =
                    body.addChildElement(Pdf17StructureType.TR);

            StructureElement td1 =
                    dataRow.addChildElement(Pdf17StructureType.TD);
            td1.addFragment(page, new TextFragment() {{
                    setText("Product A");
                    setLocation(new PointF(50, 670));
                }});

            StructureElement td2 =
                    dataRow.addChildElement(Pdf17StructureType.TD);
            td2.addFragment(page, new TextFragment() {{
                    setText("2");
                    setLocation(new PointF(250, 670));
                }});

            StructureElement td3 =
                    dataRow.addChildElement(Pdf17StructureType.TD);
            td3.addFragment(page, new TextFragment() {{
                    setText("$25.00");
                    setLocation(new PointF(320, 670));
                }});

            StructureElement td4 =
                    dataRow.addChildElement(Pdf17StructureType.TD);
            td4.addFragment(page, new TextFragment() {{
                    setText("$50.00");
                    setLocation(new PointF(400, 670));
                }});

            // Add a paragraph with the total amount.
            StructureElement total =
                    section.addChildElement(Pdf17StructureType.P);
            total.addFragment(page, new TextFragment() {{
                    setText("Total: $50.00");
                    setLocation(new PointF(50, 620));
                    setFont(new TextFont("Arial", TextFontStyle.BOLD));
                }});

            try(WritableByteChannel writableByteChannel =
                    FileChannel.open(
                        Path.of("TaggedInvoice.pdf"),
                        StandardOpenOption.CREATE,
                        StandardOpenOption.WRITE,
                        StandardOpenOption.TRUNCATE_EXISTING)) {
                document.save(writableByteChannel);
            }
        }
    }
}

The following screenshot illustrates the result:

 Accessible Invoice, DevExpress PDF Document API for Java

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
JavaObject

TableAttribute()

Initializes a new instance of the TableAttribute class.

Declaration

public TableAttribute()

Methods

getColSpan() Method

Returns the column span.

Declaration

public int getColSpan()

Returns

Type Description
int

The column span.

getHeaders() Method

Returns table header references.

Declaration

public List<String> getHeaders()

Returns

Type Description
java.util.List<java.lang.String>

Table header references.

getRowSpan() Method

Returns the row span.

Declaration

public int getRowSpan()

Returns

Type Description
int

The row span.

getScope() Method

Returns the table header scope.

Declaration

public @Nullable TableScope getScope()

Returns

Type Description
TableScope

The table header scope.

getSummary() Method

Returns the table summary text.

Declaration

public String getSummary()

Returns

Type Description
String

The table summary text.

setColSpan(int value) Method

Sets the column span.

Declaration

public void setColSpan(int value)

Parameters

Name Type Description
value int

The column span.

setRowSpan(int value) Method

Sets the row span.

Declaration

public void setRowSpan(int value)

Parameters

Name Type Description
value int

The row span.

setScope(@Nullable TableScope value) Method

Sets the table header scope.

Declaration

public void setScope(@Nullable TableScope value)

Parameters

Name Type Description
value TableScope

The table header scope.

setSummary(String value) Method

Sets the table summary text.

Declaration

public void setSummary(String value)

Parameters

Name Type Description
value String

The table summary text.