Skip to main content

LayoutAttribute Class

Defines layout-related attributes for tagged PDF structure elements.

Declaration

public class LayoutAttribute 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

LayoutAttribute()

Initializes a new instance of the LayoutAttribute class.

Declaration

public LayoutAttribute()

Methods

getBackgroundColor() Method

Returns the background color.

Declaration

public PdfColor getBackgroundColor()

Returns

Type Description
PdfColor

The background color.

getBorderColorAfter() Method

Returns the after-edge border color.

Declaration

public PdfColor getBorderColorAfter()

Returns

Type Description
PdfColor

The after-edge border color.

getBorderColorBefore() Method

Returns the before-edge border color.

Declaration

public PdfColor getBorderColorBefore()

Returns

Type Description
PdfColor

The before-edge border color.

getBorderColorEnd() Method

Returns the end-edge border color.

Declaration

public PdfColor getBorderColorEnd()

Returns

Type Description
PdfColor

The end-edge border color.

getBorderColorStart() Method

Returns the start-edge border color.

Declaration

public PdfColor getBorderColorStart()

Returns

Type Description
PdfColor

The start-edge border color.

getBorderStyleAfter() Method

Returns the after-edge border style.

Declaration

public LayoutBorderStyle getBorderStyleAfter()

Returns

Type Description
LayoutBorderStyle

The after-edge border style.

getBorderStyleBefore() Method

Returns the before-edge border style.

Declaration

public LayoutBorderStyle getBorderStyleBefore()

Returns

Type Description
LayoutBorderStyle

The before-edge border style.

getBorderStyleEnd() Method

Returns the end-edge border style.

Declaration

public LayoutBorderStyle getBorderStyleEnd()

Returns

Type Description
LayoutBorderStyle

The end-edge border style.

getBorderStyleStart() Method

Returns the start-edge border style.

Declaration

public LayoutBorderStyle getBorderStyleStart()

Returns

Type Description
LayoutBorderStyle

The start-edge border style.

getBorderThicknessAfter() Method

Returns the after-edge border thickness.

Declaration

public double getBorderThicknessAfter()

Returns

Type Description
double

The after-edge border thickness.

getBorderThicknessBefore() Method

Returns the before-edge border thickness.

Declaration

public double getBorderThicknessBefore()

Returns

Type Description
double

The before-edge border thickness.

getBorderThicknessEnd() Method

Returns the end-edge border thickness.

Declaration

public double getBorderThicknessEnd()

Returns

Type Description
double

The end-edge border thickness.

getBorderThicknessStart() Method

Returns the start-edge border thickness.

Declaration

public double getBorderThicknessStart()

Returns

Type Description
double

The start-edge border thickness.

getPaddingAfter() Method

Returns the after-edge padding.

Declaration

public double getPaddingAfter()

Returns

Type Description
double

The after-edge padding.

getPaddingBefore() Method

Returns the before-edge padding.

Declaration

public double getPaddingBefore()

Returns

Type Description
double

The before-edge padding.

getPaddingEnd() Method

Returns the end-edge padding.

Declaration

public double getPaddingEnd()

Returns

Type Description
double

The end-edge padding.

getPaddingStart() Method

Returns the start-edge padding.

Declaration

public double getPaddingStart()

Returns

Type Description
double

The start-edge padding.

getPlacement() Method

Returns the layout placement.

Declaration

public LayoutPlacement getPlacement()

Returns

Type Description
LayoutPlacement

The layout placement.

getTextColor() Method

Returns the text color.

Declaration

public PdfColor getTextColor()

Returns

Type Description
PdfColor

The text color.

getWritingMode() Method

Returns the writing mode.

Declaration

public WritingMode getWritingMode()

Returns

Type Description
WritingMode

The writing mode.

setBackgroundColor(PdfColor value) Method

Sets the background color.

Declaration

public void setBackgroundColor(PdfColor value)

Parameters

Name Type Description
value PdfColor

The background color.

setBorderColorAfter(PdfColor value) Method

Sets the after-edge border color.

Declaration

public void setBorderColorAfter(PdfColor value)

Parameters

Name Type Description
value PdfColor

The after-edge border color.

setBorderColorBefore(PdfColor value) Method

Sets the before-edge border color.

Declaration

public void setBorderColorBefore(PdfColor value)

Parameters

Name Type Description
value PdfColor

The before-edge border color.

setBorderColorEnd(PdfColor value) Method

Sets the end-edge border color.

Declaration

public void setBorderColorEnd(PdfColor value)

Parameters

Name Type Description
value PdfColor

The end-edge border color.

setBorderColorStart(PdfColor value) Method

Sets the start-edge border color.

Declaration

public void setBorderColorStart(PdfColor value)

Parameters

Name Type Description
value PdfColor

The start-edge border color.

setBorderStyleAfter(LayoutBorderStyle value) Method

Sets the after-edge border style.

Declaration

public void setBorderStyleAfter(LayoutBorderStyle value)

Parameters

Name Type Description
value LayoutBorderStyle

The after-edge border style.

setBorderStyleBefore(LayoutBorderStyle value) Method

Sets the before-edge border style.

Declaration

public void setBorderStyleBefore(LayoutBorderStyle value)

Parameters

Name Type Description
value LayoutBorderStyle

The before-edge border style.

setBorderStyleEnd(LayoutBorderStyle value) Method

Sets the end-edge border style.

Declaration

public void setBorderStyleEnd(LayoutBorderStyle value)

Parameters

Name Type Description
value LayoutBorderStyle

The end-edge border style.

setBorderStyleStart(LayoutBorderStyle value) Method

Sets the start-edge border style.

Declaration

public void setBorderStyleStart(LayoutBorderStyle value)

Parameters

Name Type Description
value LayoutBorderStyle

The start-edge border style.

setBorderThicknessAfter(double value) Method

Sets the after-edge border thickness.

Declaration

public void setBorderThicknessAfter(double value)

Parameters

Name Type Description
value double

The after-edge border thickness.

setBorderThicknessBefore(double value) Method

Sets the before-edge border thickness.

Declaration

public void setBorderThicknessBefore(double value)

Parameters

Name Type Description
value double

The before-edge border thickness.

setBorderThicknessEnd(double value) Method

Sets the end-edge border thickness.

Declaration

public void setBorderThicknessEnd(double value)

Parameters

Name Type Description
value double

The end-edge border thickness.

setBorderThicknessStart(double value) Method

Sets the start-edge border thickness.

Declaration

public void setBorderThicknessStart(double value)

Parameters

Name Type Description
value double

The start-edge border thickness.

setPaddingAfter(double value) Method

Sets the after-edge padding.

Declaration

public void setPaddingAfter(double value)

Parameters

Name Type Description
value double

The after-edge padding.

setPaddingBefore(double value) Method

Sets the before-edge padding.

Declaration

public void setPaddingBefore(double value)

Parameters

Name Type Description
value double

The before-edge padding.

setPaddingEnd(double value) Method

Sets the end-edge padding.

Declaration

public void setPaddingEnd(double value)

Parameters

Name Type Description
value double

The end-edge padding.

setPaddingStart(double value) Method

Sets the start-edge padding.

Declaration

public void setPaddingStart(double value)

Parameters

Name Type Description
value double

The start-edge padding.

setPlacement(LayoutPlacement value) Method

Sets the layout placement.

Declaration

public void setPlacement(LayoutPlacement value)

Parameters

Name Type Description
value LayoutPlacement

The layout placement.

setTextColor(PdfColor value) Method

Sets the text color.

Declaration

public void setTextColor(PdfColor value)

Parameters

Name Type Description
value PdfColor

The text color.

setWritingMode(WritingMode value) Method

Sets the writing mode.

Declaration

public void setWritingMode(WritingMode value)

Parameters

Name Type Description
value WritingMode

The writing mode.