All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.docx4j.model.images.WordXmlPictureE20 Maven / Gradle / Ivy

Go to download

docx4j is a library which helps you to work with the Office Open XML file format as used in docx documents, pptx presentations, and xlsx spreadsheets.

There is a newer version: 11.4.11
Show newest version
/*
 *  Copyright 2010, Plutext Pty Ltd.
 *   
 *  This file is part of docx4j.

    docx4j is licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 

    You may obtain a copy of the License at 

        http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License.

 */
package org.docx4j.model.images;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.docx4j.XmlUtils;
import org.docx4j.convert.out.common.AbstractWmlConversionContext;
import org.docx4j.dml.CTBlip;
import org.docx4j.dml.CTNonVisualDrawingProps;
import org.docx4j.dml.CTPositiveSize2D;
import org.docx4j.dml.picture.Pic;
import org.docx4j.dml.wordprocessingDrawing.Anchor;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.docx4j.relationships.Relationship;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.NodeIterator;

/**
 * Generate HTML/XSLFO from 
 * 
 * E20 example:
 * 
				
					
						
						
						
						
							
						
						
							
								
									
										
										
											
										
									
									
										
:
									
									
:
									
								
							
						
					
				
				
			
					
						
						
							3400425
						
						
							1991360
						
						
						
						
						
						
							
						
						
							
								
									
										
										
											
										
									
									
										
											
												
													
												
												
													
														
													
												
											
											
										
										
										
											
										
									
									
										
											
											
										
										
											
										
										
										
											
											
											
											
										
									
								
							
						
					
								
 * 
 *
 */
public class WordXmlPictureE20 extends AbstractWordXmlPicture {
	
	protected static Logger log = LoggerFactory.getLogger(WordXmlPictureE20.class);

	// It'll be one or the other of these
	private Inline inline;
	private Anchor anchor;
    
    private WordXmlPictureE20(WordprocessingMLPackage wmlPackage, Object anchorOrInline) {
    	
    	this.wmlPackage = wmlPackage;
    	
		if (anchorOrInline != null) {
			if (anchorOrInline instanceof Inline) {
				this.inline = (Inline) anchorOrInline;				
			} else if (anchorOrInline instanceof Anchor) {
				this.anchor = (Anchor) anchorOrInline;				
			} else if (anchorOrInline instanceof NodeIterator) { // from Xalan/XSLT
				Node n = ((NodeIterator) anchorOrInline).nextNode();
				if (n != null) {
					Object jaxb = null;
					try {
						Unmarshaller u = Context.jc.createUnmarshaller();
						u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());
						jaxb = u.unmarshal(n);
					} catch (JAXBException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
						if (jaxb instanceof JAXBElement) {

							JAXBElement jb = (JAXBElement) jaxb;
							if (jb.getDeclaredType()
									.getName()
									.equals("org.docx4j.dml.wordprocessingDrawing.Inline")) {
								this.inline = (Inline) jb.getValue();
							} else if (jb
									.getDeclaredType()
									.getName()
									.equals("org.docx4j.dml.wordprocessingDrawing.Anchor")) {
								this.anchor = (Anchor) jb.getValue();
							} else {
								log.error("UNEXPECTED "
										+ XmlUtils.JAXBElementDebug(jb));
								return;
							}
						} else if (jaxb instanceof Inline) {
							this.inline = (Inline) jaxb;
						} else if (jaxb instanceof Anchor) {
							this.anchor = (Anchor) jaxb;
						} else {
							log.error(jaxb.getClass().getName());
							return;
						}
					} catch (ClassCastException e) {
						log.error("Couldn't cast " + jaxb.getClass().getName()
								+ " to Anchor or Inline ");
					}
				}
			}
		}
    	
    }

    
    private static void debug(WordXmlPictureE20 converter) {
    	
    	if (converter.inline!=null)
    		log.error(XmlUtils.marshaltoString(converter.inline, true, true,
    				Context.jc, "foo", "bar", Inline.class ));
    	else 
    		log.error(XmlUtils.marshaltoString(converter.anchor, true, true,
    				Context.jc, "foo", "bar", Anchor.class ));
    }
    
    /**
     * @param wmlPackage
     * @param imageDirPath - images won't be saved if this is not set
     * @param pictureData
     * @param picSize
     * @param picLink
     * @param linkData
     * @return
     */
    private static WordXmlPictureE20 createWordXmlPictureFromE20(
    		WordprocessingMLPackage wmlPackage,
    		ConversionImageHandler imageHandler,
    		Object anchorOrInline,
    		Part sourcePart) {

    	WordXmlPictureE20 converter = new WordXmlPictureE20(wmlPackage, anchorOrInline);
    	
    	converter.readDimensions();
    	converter.readHyperlink();
    	
    	
    	Pic pic = converter.getPic();
    	if (pic==null) {
    		log.error("pic missing!!");
    		debug(converter);
    		return null;    		
    	}
    	
    	if (pic.getBlipFill()==null
    			|| pic.getBlipFill().getBlip()==null) {
    		log.error("blip missing!!");
    		return null;    		    		
    	}
    	
    	CTBlip blip = pic.getBlipFill().getBlip();
    	
    	String imgRelId = blip.getEmbed();
    	if ((imgRelId == null) || (imgRelId.length() == 0)) {
    		imgRelId = blip.getLink();
    	}
    	if ((imgRelId != null) && (imgRelId.length() > 0)) {
    		converter.handleImageRel(imageHandler, imgRelId, sourcePart);
    	}
    	else {
    		log.error("not linked or embedded?!");
    	}

		return converter;
	}
    
    private Pic getPic() {
    	
    	if (inline!=null) {
        	if (inline.getGraphic()==null
        			|| inline.getGraphic().getGraphicData()==null
        			|| inline.getGraphic().getGraphicData().getPic()==null) {
        		log.error("pic missing!!");
        		return null;    		
        	}
        	return inline.getGraphic().getGraphicData().getPic();
    	}

    	if (anchor!=null) {
        	if (anchor.getGraphic()==null
        			|| anchor.getGraphic().getGraphicData()==null
        			|| anchor.getGraphic().getGraphicData().getPic()==null) {
        		log.error("pic missing!!");
        		return null;    		
        	}
        	return anchor.getGraphic().getGraphicData().getPic();
    	}
    	
    	log.error("Anchor and inline both null!");
    	return null;
    }


    
    /** Extension function to create an HTML  element
     * from "E2.0 images" 
     *      //w:drawing/wp:inline
     *     |//w:drawing/wp:anchor
     * @param context
     * @param wpInline
     * @return
     */
    public static DocumentFragment createHtmlImgE20(
    		AbstractWmlConversionContext context,
    		Object wpInline) {

    	
    	return createHtmlImgE20(context, wpInline, null);
    }

    /** Extension function to create an HTML  element
     * from "E2.0 images" 
     *      //w:drawing/wp:inline
     *     |//w:drawing/wp:anchor
     * with a custom ID.
     * @param context
     * @param wpInline
     * @return
     */
    public static DocumentFragment createHtmlImgE20(
    		AbstractWmlConversionContext context,
    		Object wpInline, String id) {

    	Part sourcePart = context.getCurrentPart();
    	
    	WordXmlPictureE20 converter = createWordXmlPictureFromE20(context.getWmlPackage(),
        		 context.getImageHandler(), wpInline, sourcePart );
    	
    	if (id!=null) {
    		converter.setID(id);
    	}
    	
    	return getHtmlDocumentFragment(converter);
    }
    
    /** Extension function to create an XSL FO  element
     * from "E2.0 images" 
     *      //w:drawing/wp:inline
     *     |//w:drawing/wp:anchor
     * @param wmlPackage
     * @param imageDirPath
     * @param pictureData
     * @param picSize
     * @param picLink
     * @param linkData
     * @return
     */
    public static DocumentFragment createXslFoImgE20(
    		AbstractWmlConversionContext context,
    		NodeIterator wpInline) {

    	Part sourcePart = context.getCurrentPart();
    	
    	WordXmlPictureE20 converter = createWordXmlPictureFromE20(context.getWmlPackage(),
        		 context.getImageHandler(), wpInline, sourcePart);
    	
        Document d = converter.createXslFoImageElement();

		DocumentFragment docfrag = d.createDocumentFragment();
		docfrag.appendChild(d.getDocumentElement());

		return docfrag;
    }

    /**
     * for XSLFOExporterNonXSLT
     * @since 3.0
     */
    public static DocumentFragment createXslFoImgE20(
    		AbstractWmlConversionContext context,
    		Object wpInline) {

    	Part sourcePart = context.getCurrentPart();
    	
    	WordXmlPictureE20 converter = createWordXmlPictureFromE20(context.getWmlPackage(),
        		 context.getImageHandler(), wpInline, sourcePart);
    	
        Document d = converter.createXslFoImageElement();

		DocumentFragment docfrag = d.createDocumentFragment();
		docfrag.appendChild(d.getDocumentElement());

		return docfrag;
    }
    
    private void readDimensions() {
    	CTPositiveSize2D size2d = getExtent();
    	if (size2d==null) {
    		log.warn("wp:inline/wp:extent missing!");
    		return;
    	}
    	dimensions = new Dimensions();
    	if (size2d.getCx()!=0) {
    		dimensions.width= (int) size2d.getCx() / extentToPixelConversionFactor;
    		dimensions.widthUnit = "px";
    	}
    	if (size2d.getCy()!=0) {
    		dimensions.height= (int) size2d.getCy() / extentToPixelConversionFactor;
    		dimensions.heightUnit = "px";
    	}    	
    }
    
    
    private CTPositiveSize2D getExtent() {
    	
    	if (inline!=null) {
        	return inline.getExtent();
    	}

    	if (anchor!=null) {
        	return anchor.getExtent();
    	}
    	
    	log.error("Anchor and inline both null!");
    	return null;
    }
    
    
    private final int extentToPixelConversionFactor = 12700;
    
    private void readHyperlink() {
    	if (getDocPr()!=null
    			&& getDocPr().getHlinkClick()!=null) {
    		
    		String linkRelId = getDocPr().getHlinkClick().getId();
    		
            if ( linkRelId!=null && !linkRelId.equals("") ) 
            {
            	Relationship rel = wmlPackage.getMainDocumentPart().getRelationshipsPart().getRelationshipByID(linkRelId);
            	
            	if (rel.getTargetMode() == null
            			|| rel.getTargetMode().equals("Internal") ) {
            		
            		setHlinkReference("TODO - save this object");
            	} else {
                    setHlinkReference( rel.getTarget() );            	
            	}
            }
            
            targetFrame = getDocPr().getHlinkClick().getTgtFrame();
            tooltip = getDocPr().getHlinkClick().getTooltip();
    	}
    }

    private CTNonVisualDrawingProps getDocPr() {
    	
    	if (inline!=null) {
        	if (inline.getDocPr()==null) {
        		log.error("DocPr missing!!");
        		return null;    		
        	}
        	return inline.getDocPr();
    	}

    	if (anchor!=null) {
        	if (anchor.getDocPr()==null) {
        		log.error("DocPr missing!!");
        		return null;    		
        	}
        	return anchor.getDocPr();
    	}
    	
    	log.error("Anchor and inline both null!");
    	return null;
    }
    

//    private byte[] data;
//    // / 
//    // / The decoded data from the corresponding 'w:bindata'
//    /// node of the Word Document.
//    /// 
//    /// 
//    /// This property is set by the conversion process.
//    /// 
//    /// 
//    /// 
//    /// 
//    /// 
//    public byte[] getData() {
//		return this.data;
//	}
//
//	public void setData(byte[] value) {
//		this.data = value;
//	}
//
//	private String id;
//
//	// / 
//	// / The identifier of the picture unique only within the scope of
//	// / the Word Document.
//	// / 
//	// / 
//	// / 
//	// / 
//	// / 
//	public String getID() {
//		return this.id;
//	}
//
//	public void setID(String value) {
//		this.id = value;
//	}
//
//    private String src;
//    public String getSrc() {
//		return this.src;
//	}
//	public void setSrc(String value) {
//		this.src = value;
//	}
//
//	
//	
//    private String style;
//    // / 
//    // / The attribute of the v:shape node which maps to the
//    /// 'style' attribute of and HTML 'img' tag.
//    /// 
//    /// 
//    /// 
//    /// 
//    /// 
//    public String getStyle() {
//		return this.style;
//	}
//
//	public void setStyle(String value) {
//		this.style = value;
//	}
//
//    private String pType;
//    // / 
//    // / The type of the picture as specified by the attribute of the
//	// v:shape node
//    /// within the Word Document.
//    /// 
//    /// 
//    /// This value is used as an identifier for a v:type node, which used to specify
//    /// properties of the picture within the Word Document.
//    /// 
//    /// 
//    /// 
//    /// 
//    /// 
//    public String getPType() {
//		return this.pType;
//	}
//
//	public void setPType(String value) {
//		this.pType = value;
//	}
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy