1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
For i = 1 to 4
ParagraphStyles.getByName("Heading " + i).OutlineLevel = i
Next
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)
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
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