Module: Asposeslidesjava::CreateTable

Defined in:
lib/asposeslidesjava/Tables/createtable.rb

Instance Method Summary collapse

Instance Method Details

#initializeObject



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
# File 'lib/asposeslidesjava/Tables/createtable.rb', line 3

def initialize()
    data_dir = File.dirname(File.dirname(File.dirname(File.dirname(__FILE__)))) + '/data/Tables/'
            
    # Create an instance of Presentation class
    pres = Rjb::import('com.aspose.slides.Presentation').new

    # Get the first slide
    sld = pres.getSlides().get_Item(0)

    # Define columns with widths and rows with heights
    dblCols = [50, 50, 50]
    dblRows = [50, 30, 30, 30, 30]

    # Add table shape to slide
    tbl = sld.getShapes().addTable(100, 50, dblCols, dblRows)

    fill_type = Rjb::import('com.aspose.slides.FillType')    
    color = Rjb::import('java.awt.Color')    

    # Set border format for each cell
    row = 0
    while row < tbl.getRows().size()
        cell = 0
        while cell < tbl.getRows().get_Item(row).size()
            tbl.getRows().get_Item(row).get_Item(cell).getBorderTop().getFillFormat().setFillType(fill_type.Solid)
            tbl.getRows().get_Item(row).get_Item(cell).getBorderTop().getFillFormat().getSolidFillColor().setColor(color.RED)
            tbl.getRows().get_Item(row).get_Item(cell).getBorderTop().setWidth(5)

            tbl.getRows().get_Item(row).get_Item(cell).getBorderBottom().getFillFormat().setFillType(fill_type.Solid)
            tbl.getRows().get_Item(row).get_Item(cell).getBorderBottom().getFillFormat().getSolidFillColor().setColor(color.RED)
            tbl.getRows().get_Item(row).get_Item(cell).getBorderBottom().setWidth(5)

            tbl.getRows().get_Item(row).get_Item(cell).getBorderLeft().getFillFormat().setFillType(fill_type.Solid)
            tbl.getRows().get_Item(row).get_Item(cell).getBorderLeft().getFillFormat().getSolidFillColor().setColor(color.RED)
            tbl.getRows().get_Item(row).get_Item(cell).getBorderLeft().setWidth(5)

            tbl.getRows().get_Item(row).get_Item(cell).getBorderRight().getFillFormat().setFillType(fill_type.Solid)
            tbl.getRows().get_Item(row).get_Item(cell).getBorderRight().getFillFormat().getSolidFillColor().setColor(color.RED)
            tbl.getRows().get_Item(row).get_Item(cell).getBorderRight().setWidth(5)
            
            cell +=1
        end
        row +=1    
    end

    # Merge cells 1 & 2 of row 1
    tbl.mergeCells(tbl.getRows().get_Item(0).get_Item(0), tbl.getRows().get_Item(1).get_Item(0), false)

    # Add text to the merged cell
    tbl.getRows().get_Item(0).get_Item(0).getTextFrame().setText("Merged Cells")

    # Write the presentation as a PPTX file 
    save_format = Rjb::import('com.aspose.slides.SaveFormat')
    pres.save(data_dir + "CreateTable.pptx", save_format.Pptx)

    puts "Created table, please check the output file."
end