// Get a text cursor for the first cell
XTextCursor xTableCursor = xTableText.createTextCursor();
// Get the XTextContent interface of the reference mark so we can insert it
XTextContent xContent = ( XTextContent ) UnoRuntime.queryInterface (
XTextContent.class, xRefMark );
// Insert the reference mark into the first cell of the table
xTableText.insertTextContent ( xTableCursor, xContent, false );
// Create a 'GetReference' text field to refer to the reference mark we just inserted,
// and get it's XPropertySet interface
XPropertySet xFieldProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, mxDocFactory.createInstance (
"com.sun.star.text.TextField.GetReference" ) );
// Get the XReferenceMarksSupplier interface of the document
XReferenceMarksSupplier xRefSupplier = ( XReferenceMarksSupplier )
UnoRuntime.queryInterface( XReferenceMarksSupplier.class, mxDoc );
// Get an XNameAccess which refers to all inserted reference marks
XNameAccess xMarks = ( XNameAccess ) UnoRuntime.queryInterface ( XNameAccess.class,
xRefSupplier.getReferenceMarks() );
// Put the names of each reference mark into an array of strings
String[] aNames = xMarks.getElementNames();
// Make sure that at least 1 reference mark actually exists
// (well, we just inserted one!)
if ( aNames.length > 0 )
{
// Output the name of the first reference mark ('TableHeader')
System.out.println ( "GetReference text field inserted for ReferenceMark : "
+ aNames[0] );
// Set the SourceName of the GetReference text field to 'TableHeader'
xFieldProps.setPropertyValue ( "SourceName", aNames[0] );
// specify that the source is a reference mark (could also be a footnote,
// bookmark or sequence field )
xFieldProps.setPropertyValue ( "ReferenceFieldSource", new Short (
ReferenceFieldSource.REFERENCE_MARK ) );
// We want the reference displayed as 'above' or 'below'
xFieldProps.setPropertyValue ( "ReferenceFieldPart",
new Short (ReferenceFieldPart.UP_DOWN ) );
// Get the XTextContent interface of the GetReference text field
XTextContent xRefContent = (XTextContent) UnoRuntime.queryInterface(
XTextContent.class, xFieldProps );
// Go to the end of the document
mxDocCursor.gotoEnd( false );