REM  *****  BASIC  *****

Sub Main
    H2Pdf
End Sub

sub H2Pdf
    BaseDir = "file:///C:/data/h2database/"
    REM BaseDir = "file:///Users/tmueller/data/"

    Url = BaseDir & "h2/docs/html/onePage.html"
    dim FileProperties(1) As New com.sun.star.beans.PropertyValue
    FileProperties(0).Name = "FilterName"
    FileProperties(0).Value = "HTML (StarWriter)"
    FileProperties(1).Name = "UpdateDocMode"
    FileProperties(1).Value = 3 'full update
    document = StarDesktop.loadComponentFromURL(Url, "_blank", 0, FileProperties)
    docs = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
    StyleFamilies = document.StyleFamilies

    ParagraphStyles = StyleFamilies.getByName("ParagraphStyles")
    BodyStyle = ParagraphStyles.getByName("Text body")
    BodyStyle.ParaOrphans = 2
    BodyStyle.ParaWidows = 2

    HeadingStyle = ParagraphStyles.getByName("Heading 1")
    HeadingStyle.BreakType = 3 ' Insert Page Break Before
    HeadingStyle.ParaKeepTogether = false

    images = document.GraphicObjects
    For i = 0 to images.getCount() - 1
        image = images.getByIndex(i)
        if image.Size.Width <> image.ActualSize.Width or image.Size.Height <> image.ActualSize.Height then
            image.Size.Width = image.ActualSize.Width
            image.Size.Height = image.ActualSize.Height
            wait 100
        end if
    Next
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())

    PageStyles = StyleFamilies.getByName("PageStyles")

    Standard = PageStyles.getByName("HTML")
    Standard.FooterIsOn = True

    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())

    oText = Standard.FooterText
    oText.setString("")
    PageNumber = document.createInstance("com.sun.star.text.TextField.PageNumber")
    PageNumber.NumberingType = 4 ' magic constant: 4=Arabic numbers
    PageNumber.SubType = 1 ' magic constant: use current page number

    PageCount = document.createInstance("com.sun.star.text.TextField.PageCount")
    PageCount.NumberingType = 4 ' magic constant: 4=Arabic numbers

    FooterCursor = oText.Text.createTextCursor()
    oText.insertString(FooterCursor, Chr(09)& Chr(09), False)
    oText.insertTextContent(FooterCursor, PageNumber, False)
    oText.insertString(FooterCursor, " of ", False)
    oText.insertTextContent(FooterCursor, PageCount, False)

    Cursor = document.Text.createTextCursor()
    Cursor.gotoStart(false)
    Cursor.gotoNextParagraph(false)
    Cursor.gotoNextParagraph(false)

    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())

    toc = document.createInstance("com.sun.star.text.ContentIndex")
    toc.Title= "Table of Contents"
    toc.CreateFromOutline = True
    toc.Level = 4
    toc.IsProtected = false
    document.Text.insertTextContent(Cursor, toc, false)

    Cursor.gotoStart(false)
    result = true
    while result
        result = Cursor.gotoNextParagraph(false)
        if Cursor.ParaStyleName = "Heading 1" then
            Cursor.BreakType = 4 ' Insert Page Break After
        end if
    wend

    dim linkStart(0) As New com.sun.star.beans.PropertyValue
    dim linkEnd(0) As New com.sun.star.beans.PropertyValue

    For i = 1 To 4
        oLevel = toc.LevelFormat.getByIndex(i)
        x = DimArray(5)
        x = Array(linkStart, oLevel(0), oLevel(1), oLevel(2), oLevel(3), linkEnd)
        old = oLevel(0)
        linkStart(0).Name = "TokenType"
        linkStart(0).Value = "TokenHyperlinkStart"
        linkStart(0).Handle = -1
        linkStart(0).State = com.sun.star.beans.PropertyState.DIRECT_VALUE
        linkEnd(0).Name = "TokenType"
        linkEnd(0).Value = "TokenHyperlinkEnd"
        linkEnd(0).Handle = -1
        linkEnd(0).State = com.sun.star.beans.PropertyState.DIRECT_VALUE
        toc.LevelFormat.replaceByIndex(i, x)
    next

    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
    toc.update()
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
    toc.update()
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())

    pdfTemp = BaseDir & "h2web/h2temp.pdf"
    pdf = BaseDir & "h2web/h2.pdf"

    dim pdfProperties(1) as new com.sun.star.beans.PropertyValue
    pdfProperties(0).Name = "FilterName"
    pdfProperties(0).Value = "writer_pdf_Export"
    document.storeToURL(pdfTemp, pdfProperties())

    fileAccessService = createUnoService("com.sun.star.ucb.SimpleFileAccess")
    fileAccessService.move(pdfTemp, pdf)

    ThisComponent.close(true)
end sub
