incubator-ooo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ariel Constenla-Haile <>
Subject Re: Convert table from absolute width to relative
Date Thu, 16 Feb 2012 19:53:04 GMT

Hi Matheus,

On Thu, Feb 16, 2012 at 01:43:58PM -0200, Matheus Garcia wrote:
> Hi,
> I need to programmatically convert some documents which contains some
> table with absolute width to relative sizes. The purpose of that change
> is that when converted to HTML the width of that absolute table is also
> fixed, and that leads to scrollbars appearing when document is viewed in
> small screen devices.
> I don't know a better way to do that, so I thought on creating an event
> handler to do these changes during the onSave and onSaveAs event. The
> event is being caught, but I'm in doubt on how to get the necessary info
> to do these changes.
> More specifically, I've create a procedure to loop into all tables, and
> test if it is relative width or not. If its has absolute width, I want
> to change sizes. There are a lot of details involved, but in the
> simplest case, I just need to get the fraction of page width that the
> table occupies.
> So that's my situation: if have a XTextTable, how can I get the page
> width that contains that table? I tried to call getAnchor() without
> success, as its said the TextTable docs that "(...) the anchor of the
> actual implementation for text tables does not have a position in the
> text. Thus that anchor can not be used for some operation like
> XTextContent::attach or XText::insertTextContent or other function that
> require the object to have a position in the text. (...)"
> Does any of you can help me in solving that? Or is there another way to
> do these transformations?

The easiest way will be to play with the HoriOrient property of the

Sub TablesWidth_PageSizeAuto
    Dim oDoc
    oDoc = ThisComponent

    Dim oTables
    oTables = oDoc.getTextTables()

   Dim sName$, oTable
   For Each sName in
       oTable = oTables.getByName(sName)
       With oTable
          .HoriOrient =
          .LeftMargin = 0
          .RightMargin = 0
       End With
End Sub

Width this code, tables will fit in the page text area.

Something more complex, is calculating the table width (and its margins)
according to the page width:


Sub TablesWidth_RelWidth
    Dim oDoc
    oDoc = ThisComponent
    Dim oTables
    oTables = oDoc.getTextTables()
    Dim sName$, oTable, oCell, oCursor, sStyleName$, oStyle
    Dim iTextAreaWidth as Long, iTableWidth as Long
    For Each sName in oTables.getElementNames()
        oTable = oTables.getByName(sName)
        oCell = oTable.getCellByName("A1")
        oCursor = oCell.getText().createTextCursor()
        sStyleName = oCursor.PageStyleName
        oStyle = oDoc.getStyleFamilies().getByName("PageStyles").getByName(sStyleName)
        With oStyle
            iTextAreaWidth = .Width - .LeftMargin - .RightMargin - .LeftBorderDistance - .RightBorderDistance
        End With
        iTableWidth = (iTextAreaWidth * RELATIVEWIDTH) / 100
        iMargins = (iTextAreaWidth - iTableWidth) / 2
        With oTable
            .HoriOrient =
            .LeftMargin = iMargins
            .RightMargin = iMargins
            .Width = iTableWidth
        End With
End Sub

With this code tables will be centered, with a 75% width relative to 
the page with.

Ariel Constenla-Haile
La Plata, Argentina

View raw message