2012-05-28 21 views
6

का उपयोग कर रहा HWPFDocument का उपयोग कर शब्द दस्तावेज़ बनाने के लिए कोशिश कर रहा हूँ के साथ लाइन टूट जाता है, ताकि सूची प्रदर्शित करने के कोई तरीका है। मैं कुछ फीचर के साथ दस्तावेज़ बनाने में सक्षम हूं लेकिन कुछ चीजें पाने में सक्षम नहीं हूं। मेरी समस्या सरल है लेकिन मैं कुछ चीजों को समझने में सक्षम नहीं हूं। मेरे द्वारा बनाए गए worddoc में इस साधारण HTML कनवर्ट करना चाहते हैं:वहाँ HWPFDocument

<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> 

यहाँ मैं बोल्ड और इटैलिक पाठ परिवर्तित करने के लिए कर रहा हूँ। लेकिन यह समझने में सक्षम नहीं है कि

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

WordDoc में टैग कैसे बनाएं।

यह करने के लिए किसी भी उदाहरण नहीं है है, तो मुझे पता है मैं वास्तव में प्रयास, अग्रिम धन्यवाद सराहना बताएं।

संपादित:

शामिल पुस्तकालय:

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" 

और इस तरह दस्तावेज़ में एचटीएमएल कन्वर्ट करने के लिए मुख्य कोड है:

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 

आशा यह आप समझने में मदद मिलेगी।

+0

पर स्थानांतरित करने के लिए सोचा था कि कोशिश का एक बहुत आप कैसे आप दस्तावेज़ में एचटीएमएल परिवर्तित दिखा सकते हैं? या आप ऐसे कनवर्टर लिखने की कोशिश कर रहे हैं? –

+0

मैं ApachePOI और JRuby का उपयोग कर कन्वर्ट करने के लिए कोशिश कर रहा हूँ। कृपया मेरे संपादन खंड पर जांच करें। – Vik

+0

निकिता: क्या आपके पास इसका कोई समाधान है? – Vik

उत्तर

2

बाद मैं JOD कनवर्टर

+1

ओपनऑफिस सेवा (एक सर्वर पृष्ठभूमि में चल रहा है) के लिए थंबनेल या पेज पूर्वावलोकन धन्यवाद उत्पन्न करने के लिए अल्फ्रेस्को द्वारा उस टूल का उपयोग किया जाता है। केवल रूपांतरण उद्देश्य के लिए अच्छी पसंद है। पीओआई दस्तावेज निर्माण या संशोधन पहले लक्ष्य। –

1

poi javadocs के इस खंड की संभावना आपके लिए उपयोगी हो जाएगा। उदाहरण के लिए, एक सूची बनाने के लिए, मुझे लगता है कि आप उपयोग करना

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

इस वर्ग के एक वर्ड दस्तावेज़ में एक सूची बनाने के लिए प्रयोग किया जाता है चाहता हूँ। इसका उपयोग HWPFDocument में रजिस्टरलिस्ट के साथ किया जाता है। वर्ड में, सूचियां इकाइयां नहीं हैं, जिसका अर्थ है कि आप वास्तव में दस्तावेज़ में एक नहीं जोड़ सकते हैं। सूची केवल सूची प्रविष्टियों के गुणों के रूप में कार्य करती है। एक बार जब आप एक सूची पंजीकृत कर लेते हैं, तो आप उस सूची में सूची प्रविष्टियां जोड़ सकते हैं जो सूची का हिस्सा हैं। इस है कि मैं देख रहा हूँ की केवल लाभ, आप दस्तावेज़ में कहीं भी एक सूची प्रविष्टि जोड़ें और पिछले सूची से नंबर जारी रखने के लिए कर सकते हैं।

तो जावा में आप यह कर जाएगा:

new HWPFList(boolean numbered, StyleSheet styleSheet) 

मैं एक गहरे लाल रंग का विशेषज्ञ तो मैं आप के लिए JRuby के लिए अनुवाद छोड़ देंगे नहीं हूँ।

कि आप अपनी सूची में # 1 और # 2 देता है, और # 3 और # 4 पैरा मुझे लगता है कि के संस्करणों स्टाइल रहे हैं।

+0

आपकी प्रतिक्रिया के लिए धन्यवाद। – Vik