Is there a way to display line breaks, a list of orders using HWPFDocument

I am trying to create a word document using HWPFDocument. I can create a document with some function, but I am not able to get a few things. My problem is simple, but I can’t understand a few things. I want to convert this simple HTML to generated worddoc:

<div xmlns="http://www.w3.org/1999/xhtml" class="formatted_content"> <strong>cloudHQ.tester.4</strong> – this is the bold text <br/> this is italic text <br/> <ul> <li>bullets 1</li> <li>bullets 2</li> <li>bullets 3</li> </ul> <br/> <ol> <li>Number1</li> <li>Number2</li> <li>Number3</li> </ol> <br/> <pre>this is simple quote</pre> <br> this is simple quote </div> 

Here I can convert bold and italic text. But unable to figure out how to create

 1) <ul><li>.... 2) <ol><li>... 3) break <br> 4) <pre> 

in WordDoc.

Is there any example for this, then please let me know I really appreciate the effort, thanks in advance.

Edited by:

library included:

  include_class "org.apache.poi.poifs.filesystem.POIFSFileSystem" include_class "org.apache.poi.hwpf.usermodel.ParagraphProperties" include_class "org.apache.poi.hwpf.usermodel.CharacterRun" include_class "org.apache.poi.hwpf.usermodel.CharacterProperties" 

And this is the main code for converting html to doc, for example:

  def convert_from_html_to_doc(html_file_name, comment_files) puts("Script start.....") puts("Parsing document comments start.....NEW") default_file = "misc/poi_experiment/empty.doc" fs = JavaPoi::POIFSFileSystem.new(JavaPoi::FileInputStream.new(default_file)) # Horrible word Document Format hwpfDocument = JavaPoi::HWPFDocument.new(fs) # range is used for getting the range of the document except header and footer range = hwpfDocument.getRange() par1 = range.insertAfter(JavaPoi::ParagraphProperties.new(), 0) par1.setSpacingAfter(200); puts("Adding given html content to doc.") main_html = Nokogiri::HTML(File.read(html_file_name)) characterRun = par1.insertAfter(main_html.text) # setting the font size characterRun.setFontSize(2 * 12) puts("Start procees on comment..... total : #{comment_files.size}") comment_files.each do |cf| file_path = "misc/poi_experiment/#{cf}" puts("The comment file path : #{file_path}") html = Nokogiri::HTML(File.read(file_path)).css('html') puts( html ) par = characterRun.insertAfter(JavaPoi::ParagraphProperties.new(), 0) par.setSpacingAfter(200); #text = "<b><u>this is bold and underlined text</u></b>" text = html.to_s.scan(/\D\d*/) index = 0 currentCharacterRun , currentCharacterStyleList = [], [] character_arr = text.to_s.scan(/\D\d*/) character_or_tag, index = get_next_character_or_tag(character_arr, index) while !character_or_tag.nil? if character_or_tag.is_char? currentCharacterRun << character_or_tag.get_char end if character_or_tag.is_start_tag? currentCharacterRunText = currentCharacterRun.join if currentCharacterRunText != "" characterproperties = JavaPoi::CharacterProperties.new characterproperties = emit_to_document_and_apply_style(characterproperties, currentCharacterStyleList) characterRun = par.insertAfter(currentCharacterRunText,characterproperties) currentCharacterRun = [] end currentCharacterStyleList << character_or_tag.get_tag end if character_or_tag.is_end_tag? currentCharacterRunText = currentCharacterRun.join if currentCharacterRunText != "" characterproperties = JavaPoi::CharacterProperties.new characterproperties = emit_to_document_and_apply_style(characterproperties, currentCharacterStyleList) characterRun = par.insertAfter(currentCharacterRunText,characterproperties) currentCharacterRun = [] end currentCharacterStyleList.reject! { |x| x == character_or_tag.get_tag.gsub("/","") } end character_or_tag, index = get_next_character_or_tag(character_arr, index) end end hwpfDocument.write(JavaPoi::FileOutputStream.new("#{html_file_name}.doc", true)) end 

Hope this helps you understand.

+6
source share
2 answers

After many attempts, I decided to switch to jod converter

+2
source

This section of poi javadocs is likely to be of use to you. For example, to create a list, I think you want to use

http://poi.apache.org/apidocs/org/apache/poi/hwpf/usermodel/HWPFList.html

This class is used to create a list in a Word document. It is used in conjunction with registerList in an HWPFDocument. In Word, lists are not medium objects, that is, you cannot actually add them to a document. Lists only act as properties for list entries. After registering a list, you can add entries to the list, which is part of the list. The only advantage of this, which I see, is that you can add a record anywhere in the document and continue the numbering from the previous list.

So in java you will do the following:

 new HWPFList(boolean numbered, StyleSheet styleSheet) 

I am not a ruby ​​expert, so I will leave the JRuby translation to you.

This gives you # 1 and # 2 on your list, and # 3 and # 4 are the stylized versions of the paragraph that I think.

+1
source

Source: https://habr.com/ru/post/916816/


All Articles