1. 程式人生 > >Docx4j將html轉成word時,br標簽為軟回車的問題修改

Docx4j將html轉成word時,br標簽為軟回車的問題修改

peek aras -i lin 位置 org pac 回車 tco

docx4j版本:3.0.1

修改jar包:docx4j-ImportXHTML

maven配置為:技術分享

具體代碼位置:\org\docx4j\convert\in\xhtml\XHTMLImporterImpl.java 中 processInlineBoxContent方法

代碼修改前:技術分享

代碼修改後:技術分享

	private void processInlineBoxContent(InlineBox inlineBox, Styleable s,
			Map<String, CSSValue> cssMap) {
				
		
		if (inlineBox.getTextNode()==null) {
                
            if (s.getElement().getNodeName().equals("br") ) {
                
//                R run = Context.getWmlObjectFactory().createR();
//                getListForRun().getContent().add(run);                
//           		run.getContent().add(Context.getWmlObjectFactory().createBr());
            	Element ele = s.getElement();
		    	if(null != ele){
		    		Attr attrNode = ele.getAttributeNode("style");
		        	if (attrNode != null && attrNode.getValue().contains("page-break-after: always")) {
		        		Br br = Context.getWmlObjectFactory().createBr();
		        		P p = Context.getWmlObjectFactory().createP();
		        		// Create object for r
		        		R r = Context.getWmlObjectFactory().createR();
		        		p.getContent().add(r);
		        	    br.setType(STBrType.PAGE);
		        	    r.getContent().add(br);
		        	    this.contentContextStack.peek().getContent().add(p);
		        	}
//		        	wordMLPackage.getMainDocumentPart().addObject(p);
		    	}
            	
            	
            } else {
            	log.debug("InlineBox has no TextNode, so skipping" );
            	
            	// TODO .. a span in a span or a?
            	// need to traverse, how?
            	
            }
            
        } else  {
            log.debug( inlineBox.getTextNode().getTextContent() );  // don‘t use .getText()

            String theText = inlineBox.getTextNode().getTextContent(); 
            log.debug("Processing " + theText);
            
            paraStillEmpty = false;   
            
            String cssClass = getClassAttribute(s.getElement());
        	if (cssClass!=null) {
        	 	cssClass=cssClass.trim();
        	}
            addRun(cssClass, cssMap, theText);
    	            
//                                    else {
//                                    	// Get it from the parent element eg p
//                        	            //Map cssMap = styleReference.getCascadedPropertiesMap(e);
//                        	            run.setRPr(
//                        	            		addRunProperties( cssMap ));                                    	                                    	
//                                    }
        }
	}

  

Docx4j將html轉成word時,br標簽為軟回車的問題修改