Sha256: 6b4bec8fa0a63249e0c1414bbe0e68c31c75f4fbda9731e884bc50d584e67574

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

module Asposewordsjavaforruby
  module HandleMergeField
    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

    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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
asposewordsjavaforruby-0.0.5 lib/asposewordsjavaforruby/mergefield.rb