SHARE
TWEET

CustomXWPFDocument

a guest Jun 20th, 2014 929 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  2. import org.apache.xmlbeans.XmlException;
  3. import org.apache.xmlbeans.XmlToken;
  4. import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
  5. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  6. import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
  7.  
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10.  
  11. public class CustomXWPFDocument extends XWPFDocument
  12. {
  13.     public CustomXWPFDocument() throws IOException
  14.     {
  15.         super();
  16.     }
  17.  
  18.     public void createPicture(String blipId,int id, int width, int height)
  19.     {
  20.         final int EMU = 9525;
  21.         width *= EMU;
  22.         height *= EMU;
  23.         //String blipId = getAllPictures().get(id).getPackageRelationship().getId();
  24.  
  25.  
  26.         CTInline inline = createParagraph().createRun().getCTR().addNewDrawing().addNewInline();
  27.  
  28.         String picXml = "" +
  29.                 "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
  30.                 "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
  31.                 "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
  32.                 "         <pic:nvPicPr>" +
  33.                 "            <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" +
  34.                 "            <pic:cNvPicPr/>" +
  35.                 "         </pic:nvPicPr>" +
  36.                 "         <pic:blipFill>" +
  37.                 "            <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
  38.                 "            <a:stretch>" +
  39.                 "               <a:fillRect/>" +
  40.                 "            </a:stretch>" +
  41.                 "         </pic:blipFill>" +
  42.                 "         <pic:spPr>" +
  43.                 "            <a:xfrm>" +
  44.                 "               <a:off x=\"0\" y=\"0\"/>" +
  45.                 "               <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" +
  46.                 "            </a:xfrm>" +
  47.                 "            <a:prstGeom prst=\"rect\">" +
  48.                 "               <a:avLst/>" +
  49.                 "            </a:prstGeom>" +
  50.                 "         </pic:spPr>" +
  51.                 "      </pic:pic>" +
  52.                 "   </a:graphicData>" +
  53.                 "</a:graphic>";
  54.  
  55.         //CTGraphicalObjectData graphicData = inline.addNewGraphic().addNewGraphicData();
  56.         XmlToken xmlToken = null;
  57.         try
  58.         {
  59.             xmlToken = XmlToken.Factory.parse(picXml);
  60.         }
  61.         catch(XmlException xe)
  62.         {
  63.             xe.printStackTrace();
  64.         }
  65.         inline.set(xmlToken);
  66.         //graphicData.set(xmlToken);
  67.  
  68.         inline.setDistT(0);
  69.         inline.setDistB(0);
  70.         inline.setDistL(0);
  71.         inline.setDistR(0);
  72.  
  73.         CTPositiveSize2D extent = inline.addNewExtent();
  74.         extent.setCx(width);
  75.         extent.setCy(height);
  76.  
  77.         CTNonVisualDrawingProps docPr = inline.addNewDocPr();
  78.         docPr.setId(id);
  79.         docPr.setName("Picture " + id);
  80.         docPr.setDescr("Generated");
  81.     }
  82. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top