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

com.hfg.xml.msofficexml.docx.drawingml.DmlGraphicData Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.msofficexml.docx.drawingml;


import com.hfg.graphics.units.EMUs;
import com.hfg.util.StringUtil;
import com.hfg.xml.msofficexml.docx.Docx;
import com.hfg.xml.msofficexml.docx.DocxException;
import com.hfg.xml.msofficexml.docx.RelationshipXML;
import com.hfg.xml.msofficexml.docx.drawingml.fill.DmlNoFill;
import com.hfg.xml.msofficexml.docx.drawingml.line.join.DmlMiterLineJoin;
import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXMLTag;
import com.hfg.xml.msofficexml.part.MediaPart;

//------------------------------------------------------------------------------
/**
 Represents graphic data (<graphicData:r>) tag in drawing markup language (DML) from Office Open XML.

 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class DmlGraphicData extends WmlXMLTag
{
   private DmlPicture mPicture;

   //---------------------------------------------------------------------------
   public DmlGraphicData(Docx inDocx)
   {
      super(DmlXML.GRAPHIC_DATA, inDocx);
   }


   //---------------------------------------------------------------------------
   public void addMedia(MediaPart inMediaPart)
   {
      Docx docx = getParentDoc();
      if (null == docx)
      {
         throw new DocxException("No reference to the parent doc available!?");
      }

      // TODO: The URI should change based on the media type
      setAttribute("uri", DmlXML.PIC_NAMESPACE.getURI());

      String id = docx.getMainDocPartRelationshipPart().addMedia(inMediaPart);

      DmlPicture picture = getPic();

      DmlNonVisualDrawingProperties drawingProperties = picture.getNonVisualPictureProperties().getNonVisualDrawingProperties();
      drawingProperties.setName(inMediaPart.getFile().getName());

      if (StringUtil.isSet(inMediaPart.getDescription()))
      {
         drawingProperties.setDescription(inMediaPart.getDescription());
      }

      if (StringUtil.isSet(inMediaPart.getTitle()))
      {
         drawingProperties.setDescription(inMediaPart.getTitle());
      }

      picture.getNonVisualPictureProperties().getNonVisualPictureDrawingProperties().getPictureLocks().setDisallowAspectRatioChange(true).setDisallowArrowheadChanges(true);

      DmlBlip blip = picture.getBlipFill().getBlip();
      blip.setAttribute(RelationshipXML.EMBED_ATT, id);
      picture.getBlipFill().getStretch().fillRect();

      if (inMediaPart.getDimensions() != null)
      {
         picture.getShapeProperties().getTransform().setWidth(inMediaPart.getDimensions().getWidth())
               .setHeight(inMediaPart.getDimensions().getHeight());
      }
      picture.getShapeProperties().getPresetGeometry().setShape(DmlShapeType.rect);
      picture.getShapeProperties().setFill(new DmlNoFill());
      picture.getShapeProperties().getLine().setWidth(new EMUs(9525)).setFill(new DmlNoFill()).setLineJoin(new DmlMiterLineJoin().setLimit(new EMUs(800000)));
   }


   //---------------------------------------------------------------------------
   /**
    * Returns the pic tag if one exists or else instantiates a new one.
    * @return the pic tag for this graphic data
    */
   public DmlPicture getPic()
   {
      if (null == mPicture)
      {
         // Check if it has been added via addSubtag()...
         mPicture = getOptionalSubtagByName(DmlXML.PIC);
         if (null == mPicture)
         {
            mPicture = new DmlPicture(getParentDoc());
            addSubtag(mPicture);
         }
      }

      return mPicture;
   }

   //---------------------------------------------------------------------------
   /**
    * Creates a new shape object and adds it as a subtag of this object.
    * @return the new shape object
    */
   public DmlShape addShape()
   {
      DmlShape shape = new DmlShape(getParentDoc());
      addSubtag(shape);

      return shape;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy