Module: Asposewordsjavaforruby::CompressImages

Defined in:
lib/asposewordsjavaforruby/compressimages.rb

Instance Method Summary collapse

Instance Method Details

#compress_images(doc, srcFileName) ⇒ Object



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
# File 'lib/asposewordsjavaforruby/compressimages.rb', line 14

def compress_images(doc, srcFileName)
    messageFormat = Rjb::import("java.text.MessageFormat")
    file_size = get_file_size(srcFileName)
    
    # 220ppi Print - said to be excellent on most printers and screens.
    # 150ppi Screen - said to be good for web pages and projectors.
    # 96ppi Email - said to be good for minimal document size and sharing.
    desiredPpi = 150
    # In Java this seems to be a good compression / quality setting.
    jpegQuality = 90

    # Resample images to desired ppi and save.
    resampler = Rjb::import("com.aspose.words.Resampler").new
    count = resampler.resample(doc, desiredPpi, jpegQuality)
    puts MessageFormat.format("Resampled {0} images.", count)
    if (count != 1) then
         puts "We expected to have only 1 image resampled in this test document!"
    end    
    dstFileName = @data_dir + "TestCompressImages Out.docx"
    doc.save(dstFileName)
    puts messageFormat.format("Saving {0}. Size {1}.", dstFileName, get_file_size(dstFileName))

    # Verify that the first image was compressed by checking the new Ppi.
    dst_doc = Rjb::import("com.aspose.words.Document").new(dstFileName)
    nodeType = Rjb::import("com.aspose.words.NodeType")
    shape = dst_doc.getChild(nodeType.DRAWING_ML, 0, true)
    convertUtil = Rjb::import("com.aspose.words.ConvertUtil")
    imagePpi = shape.getImageData().getImageSize().getWidthPixels() / convertUtil.pointToInch(shape.getSize().getX())
    if (imagePpi < 150) then
        puts "Image was not resampled successfully."
    end
end

#get_file_size(file_name) ⇒ Object



47
48
49
50
# File 'lib/asposewordsjavaforruby/compressimages.rb', line 47

def get_file_size(file_name)
    file = Rjb::import("java.io.File").new(file_name)
    return file.length()
end

#initializeObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/asposewordsjavaforruby/compressimages.rb', line 3

def initialize()
    # The path to the documents directory.
    @data_dir = File.dirname(File.dirname(File.dirname(__FILE__))) + '/data/'
    srcFileName = @data_dir + "TestCompressImages.docx"

    doc = Rjb::import('com.aspose.words.Document').new(@data_dir + "TestCompressImages.docx")

    # Demonstrate autofitting a table to the window.
    compress_images(doc, srcFileName)
end