Module: Asposewordsjavaforruby::HandleMergeField

Defined in:
lib/asposewordsjavaforruby/mergefield.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
# File 'lib/asposewordsjavaforruby/mergefield.rb', line 3

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

    # Open the document.
    doc = Rjb::import('com.aspose.words.Document').new(data_dir + "Template.doc")
    #$doc->getMailMerge()->setFieldMergingCallback(new HandleMergeField())

    fieldNames = Array["RecipientName","SenderName","FaxNumber","PhoneNumber","Subject","Body","Urgent","ForReview","PleaseComment"]
    fieldValues = Array["Josh","Jenny","123456789","","Hello","Test Pakistan 1", true, false, true]
    doc.getMailMerge().execute(fieldNames,fieldValues)

    # Save the document.
    doc.save(data_dir + "Template Out.doc")

    remove_empty_regions()        
end

#remove_empty_regionsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/asposewordsjavaforruby/mergefield.rb', line 21

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

    # Open the document.
    doc = Rjb::import('com.aspose.words.Document').new(data_dir + "TestFile.doc")

    # Create a dummy data source containing no data.
    data = Rjb::import('com.aspose.words.DataSet').new
    #DataSet data = new DataSet()

    # Set the appropriate mail merge clean up options to remove any unused regions from the document.
    mailmerge_cleanup_options = Rjb::import('com.aspose.words.MailMergeCleanupOptions')
    doc.getMailMerge().setCleanupOptions(mailmerge_cleanup_options.REMOVE_UNUSED_REGIONS)

    # Execute mail merge which will have no effect as there is no data. However the regions found in the document will be removed
    # automatically as they are unused.
    doc.getMailMerge().executeWithRegions(data)

    # Save the output document to disk.
    doc.save(data_dir + "TestFile.RemoveEmptyRegions Out.doc")
end