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

10
    Url = BaseDir & "h2/docs/html/onePage.html"
11 12 13 14 15 16 17 18 19 20
    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
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    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
37
            wait 100
38 39 40
        end if
    Next
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    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() 
59
    oText.insertString(FooterCursor, Chr(09)& Chr(09), False)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    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)
95 96 97 98
        linkStart(0).Name = "TokenType"
        linkStart(0).Value = "TokenHyperlinkStart"
        linkStart(0).Handle = -1
        linkStart(0).State = com.sun.star.beans.PropertyState.DIRECT_VALUE 
99
        linkEnd(0).Name = "TokenType"
100 101 102 103
        linkEnd(0).Value = "TokenHyperlinkEnd"
        linkEnd(0).Handle = -1
        linkEnd(0).State = com.sun.star.beans.PropertyState.DIRECT_VALUE 
        toc.LevelFormat.replaceByIndex(i, x) 
104 105
    next
    
106 107 108
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
    toc.update()
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
109
    toc.update()
110 111
    dispatcher.executeDispatch(docs, ".uno:UpdateAll", "", 0, Array())
    
112 113
    pdfTemp = BaseDir & "h2web/h2temp.pdf"
    pdf = BaseDir & "h2web/h2.pdf"
114 115 116 117 118 119 120 121 122 123

    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) 
124
end sub