Module: Asposewordsjavaforruby::ExtractContentBasedOnStyles
- Defined in:
- lib/asposewordsjavaforruby/styles.rb
Instance Method Summary collapse
- #initialize ⇒ Object
- #paragraphs_by_style_name(doc, para_style) ⇒ Object
- #runs_by_style_name(doc, run_style) ⇒ Object
Instance Method Details
#initialize ⇒ Object
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 |
# File 'lib/asposewordsjavaforruby/styles.rb', line 3 def initialize() # The path to the documents directory. data_dir = File.dirname(File.dirname(File.dirname(__FILE__))) + '/data/' # Open the document. doc = Rjb::import('com.aspose.words.Document').new(data_dir + "Test.Styles.doc") # Define style names as they are specified in the Word document. para_style = "Heading 1" run_style = "Intense Emphasis" # Collect paragraphs with defined styles. # Show the number of collected paragraphs and display the text of this paragraphs. paragraphs = paragraphs_by_style_name(doc, para_style) para_size = paragraphs.size() #para_size = java_values($para_size) puts "Paragraphs with #{para_style} styles #{para_size}" paragraphs = paragraphs.toArray() save_format = Rjb::import("com.aspose.words.SaveFormat") paragraphs.each do |paragraph| puts paragraph.toString(save_format.TEXT) end # Collect runs with defined styles. # Show the number of collected runs and display the text of this runs. runs = runs_by_style_name(doc, run_style) runs_size = runs.size() puts "Runs with #{run_style} styles #{runs_size}" runs = runs.toArray() runs.each do |run| puts run.getRange().getText() end end |
#paragraphs_by_style_name(doc, para_style) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/asposewordsjavaforruby/styles.rb', line 40 def paragraphs_by_style_name(doc, para_style) # Create an array to collect paragraphs of the specified style. paragraphsWithStyle = Rjb::import("java.util.ArrayList").new # Get all paragraphs from the document. nodeType = Rjb::import("com.aspose.words.NodeType") paragraphs = doc.getChildNodes(nodeType.PARAGRAPH, true) paragraphs = paragraphs.toArray() # Look through all paragraphs to find those with the specified style. paragraphs.each do |paragraph| para_name = paragraph.getParagraphFormat().getStyle().getName() if (para_name == para_style) then paragraphsWithStyle.add(paragraph) end end paragraphsWithStyle end |
#runs_by_style_name(doc, run_style) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/asposewordsjavaforruby/styles.rb', line 58 def runs_by_style_name(doc, run_style) # Create an array to collect runs of the specified style. runsWithStyle = Rjb::import("java.util.ArrayList").new # Get all runs from the document. nodeType = Rjb::import("com.aspose.words.NodeType") runs = doc.getChildNodes(nodeType.RUN, true) # Look through all runs to find those with the specified style. runs = runs.toArray() runs.each do |run| if (run.getFont().getStyle().getName() == run_style) then runsWithStyle.add(run) end end runsWithStyle end |