Module: Asposewordsjavaforruby::UpdateFields

Defined in:
lib/asposewordsjavaforruby/updatefields.rb

Instance Method Summary collapse

Instance Method Details

#initializeObject



3
4
5
# File 'lib/asposewordsjavaforruby/updatefields.rb', line 3

def initialize()
    update_fields()
end

#update_fieldsObject



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/asposewordsjavaforruby/updatefields.rb', line 7

def update_fields()
    # The path to the documents directory.
    data_dir = File.dirname(File.dirname(File.dirname(__FILE__))) + '/data/quickstart/'

    # Demonstrates how to insert fields and update them using Aspose.Words.
    # First create a blank document.
    doc = Rjb::import('com.aspose.words.Document').new()
    # Use the document builder to insert some content and fields.
    builder = Rjb::import('com.aspose.words.DocumentBuilder').new(doc)
    # Insert a table of contents at the beginning of the document.
    builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u")
    builder.writeln()
    # Insert some other fields.
    builder.write("Page: ")
    builder.insertField("PAGE")
    builder.write(" of ")
    builder.insertField("NUMPAGES")
    builder.writeln()
    builder.write("Date: ")
    builder.insertField("DATE")

    # Start the actual document content on the second page.
    break_type = Rjb::import("com.aspose.words.BreakType")
    builder.insertBreak(break_type.SECTION_BREAK_NEW_PAGE)

    # Build a document with complex structure by applying different heading styles thus creating TOC entries.
    style_identifier = Rjb::import("com.aspose.words.StyleIdentifier")
    builder.getParagraphFormat().setStyleIdentifier(style_identifier.HEADING_1)
    builder.writeln("Heading 1")
    builder.getParagraphFormat().setStyleIdentifier(style_identifier.HEADING_2)
    builder.writeln("Heading 1.1")
    builder.writeln("Heading 1.2")
    builder.getParagraphFormat().setStyleIdentifier(style_identifier.HEADING_1)
    builder.writeln("Heading 2")
    builder.writeln("Heading 3")

    # Move to the next page.
    builder.insertBreak(break_type.PAGE_BREAK)
    builder.getParagraphFormat().setStyleIdentifier(style_identifier.HEADING_2)
    builder.writeln("Heading 3.1")
    builder.getParagraphFormat().setStyleIdentifier(style_identifier.HEADING_3)
    builder.writeln("Heading 3.1.1")
    builder.writeln("Heading 3.1.2")
    builder.writeln("Heading 3.1.3")
    builder.getParagraphFormat().setStyleIdentifier(style_identifier.HEADING_2)
    builder.writeln("Heading 3.2")
    builder.writeln("Heading 3.3")
    puts "Updating all fields in the document."
    
    # Call the method below to update the TOC.
    doc.updateFields()
    doc.save(data_dir + "Document Field Update Out.docx")
end