openoffice.txt 4.5 KB
Newer Older
1 2 3
REM  *****  BASIC  *****

Sub Main
4
    H2Pdf
5 6 7
End Sub

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

11
    Url = BaseDir & "h2/docs/html/onePage.html"
12 13 14 15
    dim FileProperties(1) As New com.sun.star.beans.PropertyValue
    FileProperties(0).Name = "FilterName"
    FileProperties(0).Value = "HTML (StarWriter)"
    FileProperties(1).Name = "UpdateDocMode"
16
    FileProperties(1).Value = 3 'full update
17 18 19 20 21
    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
22

23 24 25 26
    ParagraphStyles = StyleFamilies.getByName("ParagraphStyles")
    BodyStyle = ParagraphStyles.getByName("Text body")
    BodyStyle.ParaOrphans = 2
    BodyStyle.ParaWidows = 2
27

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

32 33 34 35 36 37
    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
38
            wait 100
39 40 41
        end if
    Next
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
42

43
    PageStyles = StyleFamilies.getByName("PageStyles")
44

45 46
    Standard = PageStyles.getByName("HTML")
    Standard.FooterIsOn = True
47

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

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

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

59
    FooterCursor = oText.Text.createTextCursor()
60
    oText.insertString(FooterCursor, Chr(09)& Chr(09), False)
61 62 63 64 65 66 67 68
    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)
69

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

72
    toc = document.createInstance("com.sun.star.text.ContentIndex")
73 74
    toc.Title= "Table of Contents"
    toc.CreateFromOutline = True
75
    toc.Level = 4
76
    toc.IsProtected = false
77 78 79 80 81 82 83 84 85 86
    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
87

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

91 92 93 94 95
    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)
96 97 98
        linkStart(0).Name = "TokenType"
        linkStart(0).Value = "TokenHyperlinkStart"
        linkStart(0).Handle = -1
99
        linkStart(0).State = com.sun.star.beans.PropertyState.DIRECT_VALUE
100
        linkEnd(0).Name = "TokenType"
101 102
        linkEnd(0).Value = "TokenHyperlinkEnd"
        linkEnd(0).Handle = -1
103 104
        linkEnd(0).State = com.sun.star.beans.PropertyState.DIRECT_VALUE
        toc.LevelFormat.replaceByIndex(i, x)
105
    next
106

107 108 109
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
    toc.update()
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
110
    toc.update()
111
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
112

113 114
    pdfTemp = BaseDir & "h2web/h2temp.pdf"
    pdf = BaseDir & "h2web/h2.pdf"
115 116 117 118

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

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

    ThisComponent.close(true)
125
end sub