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
|
# File 'lib/asposepdfjava/Document/addtoc.rb', line 3
def initialize()
data_dir = File.dirname(File.dirname(File.dirname(File.dirname(__FILE__)))) + '/data/'
doc = Rjb::import('com.aspose.pdf.Document').new(data_dir + "input1.pdf")
toc_page = doc.getPages().insert(1)
toc_info = Rjb::import('com.aspose.pdf.TocInfo').new
title = Rjb::import('com.aspose.pdf.TextFragment').new("Table Of Contents")
title.getTextState().setFontSize(20)
toc_info.setTitle(title)
toc_page.setTocInfo(toc_info)
titles = Array["First page", "Second page"]
i = 0
while i < 2
heading2 = Rjb::import('com.aspose.pdf.Heading').new(1)
segment2 = Rjb::import('com.aspose.pdf.TextSegment').new
heading2.setTocPage(toc_page)
heading2.getSegments().add(segment2)
heading2.setDestinationPage(doc.getPages().get_Item(i + 2))
heading2.setTop(doc.getPages().get_Item(i + 2).getRect().getHeight())
segment2.setText(titles[i])
toc_page.getParagraphs().add(heading2)
i +=1
end
doc.save(data_dir + "TOC.pdf")
puts "Added TOC Successfully, please check the output file."
end
|