openoffice.txt 4.7 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
    For i = 1 to 4
        ParagraphStyles.getByName("Heading " + i).OutlineLevel = i
    Next

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

47
    PageStyles = StyleFamilies.getByName("PageStyles")
48

49 50
    Standard = PageStyles.getByName("HTML")
    Standard.FooterIsOn = True
51

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

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

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

63
    FooterCursor = oText.Text.createTextCursor()
64
    oText.insertString(FooterCursor, Chr(09)& Chr(09), False)
65 66 67 68 69 70 71 72
    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)
73

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

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

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

95
    for i = 1 To 4
96
        oLevel = toc.LevelFormat.getByIndex(i)
97 98 99 100 101 102 103
        bound = UBound(oLevel)
        x = DimArray(bound + 2)
        x(0) = linkStart
        for j = 0 to bound
            x(j + 1) = oLevel(j)
        next
        x(bound + 2) = linkEnd
104 105 106
        linkStart(0).Name = "TokenType"
        linkStart(0).Value = "TokenHyperlinkStart"
        linkStart(0).Handle = -1
107
        linkStart(0).State = com.sun.star.beans.PropertyState.DIRECT_VALUE
108
        linkEnd(0).Name = "TokenType"
109 110
        linkEnd(0).Value = "TokenHyperlinkEnd"
        linkEnd(0).Handle = -1
111 112
        linkEnd(0).State = com.sun.star.beans.PropertyState.DIRECT_VALUE
        toc.LevelFormat.replaceByIndex(i, x)
113
    next
114

115 116 117
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
    toc.update()
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
118
    toc.update()
119
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
120

121 122
    pdfTemp = BaseDir & "h2web/h2temp.pdf"
    pdf = BaseDir & "h2web/h2.pdf"
123 124 125 126

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

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

    ThisComponent.close(true)
133
end sub