Example #1
0
        XWPFPicture AddPicture(Stream pictureData, int pictureType, String filename, int width, int height, Action <XWPFDocument, CT_Blip> extAct)
        {
            // Add the picture + relationship
            String          relationId;
            XWPFPictureData picData;
            XWPFDocument    doc = null;

            // Work out what to add the picture to, then add both the
            //  picture and the relationship for it
            // TODO Should we have an interface for this sort of thing?
            if (parent.Part is XWPFHeaderFooter)
            {
                XWPFHeaderFooter headerFooter = (XWPFHeaderFooter)parent.Part;
                relationId = headerFooter.AddPictureData(pictureData, pictureType);
                picData    = (XWPFPictureData)headerFooter.GetRelationById(relationId);
            }
            else
            {
                doc        = parent.Document;
                relationId = doc.AddPictureData(pictureData, pictureType);
                picData    = (XWPFPictureData)doc.GetRelationById(relationId);
            }

            try
            {
                // Create the Drawing entry for it
                CT_Drawing Drawing = run.AddNewDrawing();
                CT_Inline  inline  = Drawing.AddNewInline();

                // Do the fiddly namespace bits on the inline
                // (We need full control of what goes where and as what)
                //CT_GraphicalObject tmp = new CT_GraphicalObject();
                //String xml =
                //    "<a:graphic xmlns:a=\"" + "http://schemas.openxmlformats.org/drawingml/2006/main" + "\">" +
                //    "<a:graphicData uri=\"" + "http://schemas.openxmlformats.org/drawingml/2006/picture" + "\">" +
                //    "<pic:pic xmlns:pic=\"" + "http://schemas.openxmlformats.org/drawingml/2006/picture" + "\" />" +
                //    "</a:graphicData>" +
                //    "</a:graphic>";
                //InputSource is = new InputSource(new StringReader(xml));
                //org.w3c.dom.Document doc = DocumentHelper.readDocument(is);
                //inline.set(XmlToken.Factory.parse(doc.getDocumentElement(), DEFAULT_XML_OPTIONS));

                inline.graphic                 = new CT_GraphicalObject();
                inline.graphic.graphicData     = new CT_GraphicalObjectData();
                inline.graphic.graphicData.uri = "http://schemas.openxmlformats.org/drawingml/2006/picture";


                // Setup the inline
                inline.distT = (0);
                inline.distR = (0);
                inline.distB = (0);
                inline.distL = (0);

                NPOI.OpenXmlFormats.Dml.WordProcessing.CT_NonVisualDrawingProps docPr = inline.AddNewDocPr();
                long id = parent.Document.DrawingIdManager.ReserveNew();
                docPr.id = (uint)(id);
                /* This name is not visible in Word 2010 anywhere. */
                docPr.name  = ("Drawing " + id);
                docPr.descr = (filename);

                NPOI.OpenXmlFormats.Dml.WordProcessing.CT_PositiveSize2D extent = inline.AddNewExtent();
                extent.cx = (width);
                extent.cy = (height);

                // Grab the picture object
                NPOI.OpenXmlFormats.Dml.Picture.CT_Picture pic = new OpenXmlFormats.Dml.Picture.CT_Picture();

                // Set it up
                NPOI.OpenXmlFormats.Dml.Picture.CT_PictureNonVisual nvPicPr = pic.AddNewNvPicPr();

                NPOI.OpenXmlFormats.Dml.CT_NonVisualDrawingProps cNvPr = nvPicPr.AddNewCNvPr();
                /* use "0" for the id. See ECM-576, 20.2.2.3 */
                cNvPr.id = (0);
                /* This name is not visible in Word 2010 anywhere */
                cNvPr.name  = ("Picture " + id);
                cNvPr.descr = (filename);

                CT_NonVisualPictureProperties cNvPicPr   = nvPicPr.AddNewCNvPicPr();
                cNvPicPr.AddNewPicLocks().noChangeAspect = true;

                CT_BlipFillProperties blipFill = pic.AddNewBlipFill();
                CT_Blip blip = blipFill.AddNewBlip();
                blip.embed = (picData.GetPackageRelationship().Id);
                if (doc != null)
                {
                    extAct(doc, blip);
                }
                blipFill.AddNewStretch().AddNewFillRect();

                CT_ShapeProperties spPr = pic.AddNewSpPr();
                CT_Transform2D     xfrm = spPr.AddNewXfrm();

                CT_Point2D off = xfrm.AddNewOff();
                off.x = (0);
                off.y = (0);

                NPOI.OpenXmlFormats.Dml.CT_PositiveSize2D ext = xfrm.AddNewExt();
                ext.cx = (width);
                ext.cy = (height);

                CT_PresetGeometry2D prstGeom = spPr.AddNewPrstGeom();
                prstGeom.prst = (ST_ShapeType.rect);
                prstGeom.AddNewAvLst();

                using (var ms = new MemoryStream())
                {
                    StreamWriter sw = new StreamWriter(ms);
                    pic.Write(sw, "pic:pic");
                    sw.Flush();
                    ms.Position = 0;
                    var sr     = new StreamReader(ms);
                    var picXml = sr.ReadToEnd();
                    inline.graphic.graphicData.AddPicElement(picXml);
                }
                // Finish up
                XWPFPicture xwpfPicture = new XWPFPicture(pic, this);
                pictures.Add(xwpfPicture);
                return(xwpfPicture);
            }
            catch (XmlException e)
            {
                throw new InvalidOperationException("XWPFRun.Addpicture error", e);
            }
        }
Example #2
0
 private XmlSerializerNamespaces Commit(XWPFHeaderFooter wrapper)
 {
 //    XmlOptions xmlOptions = new XmlOptions(wrapper.DEFAULT_XML_OPTIONS);
 //    Dictionary<String, String> map = new Dictionary<String, String>();
 //    map.Put("http://schemas.Openxmlformats.org/officeDocument/2006/math", "m");
 //    map.Put("urn:schemas-microsoft-com:office:office", "o");
 //    map.Put("http://schemas.Openxmlformats.org/officeDocument/2006/relationships", "r");
 //    map.Put("urn:schemas-microsoft-com:vml", "v");
 //    map.Put("http://schemas.Openxmlformats.org/markup-compatibility/2006", "ve");
 //    map.Put("http://schemas.Openxmlformats.org/wordProcessingml/2006/main", "w");
 //    map.Put("urn:schemas-microsoft-com:office:word", "w10");
 //    map.Put("http://schemas.microsoft.com/office/word/2006/wordml", "wne");
 //    map.Put("http://schemas.Openxmlformats.org/drawingml/2006/wordProcessingDrawing", "wp");
 //    xmlOptions.SaveSuggestedPrefixes=(map);
 //    return xmlOptions;
     XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(new[] {
         new XmlQualifiedName("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006"),
         new XmlQualifiedName("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"),
         new XmlQualifiedName("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"),
         new XmlQualifiedName("v", "urn:schemas-microsoft-com:vml"),
         new XmlQualifiedName("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"),
         new XmlQualifiedName("w10", "urn:schemas-microsoft-com:office:word"),
         new XmlQualifiedName("wne", "http://schemas.microsoft.com/office/word/2006/wordml"),
          new XmlQualifiedName("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")
      });
     return namespaces;
 }
Example #3
0
 private void SetHeaderReference(ST_HdrFtr type, XWPFHeaderFooter wrapper)
 {
     CT_HdrFtrRef ref1 = doc.Document.body.sectPr.AddNewHeaderReference();
     ref1.type = (type);
     ref1.id = (wrapper.GetPackageRelationship().Id);
 }
Example #4
0
 /**
  * MB 24 May 2010. Created this overloaded buildHdrFtr() method because testing demonstrated
  * that the XWPFFooter or XWPFHeader object returned by calls to the CreateHeader(int, XWPFParagraph[])
  * and CreateFooter(int, XWPFParagraph[]) methods or the GetXXXXXHeader/Footer methods where
  * headers or footers had been Added to a document since it had been Created/opened, returned
  * an object that Contained no XWPFParagraph objects even if the header/footer itself did contain
  * text. The reason was that this line of code; CTHdrFtr ftr = CTHdrFtr.Factory.NewInstance(); 
  * Created a brand new instance of the CTHDRFtr class which was then populated with data when
  * it should have recovered the CTHdrFtr object encapsulated within the XWPFHeaderFooter object
  * that had previoulsy been instantiated in the CreateHeader(int, XWPFParagraph[]) or 
  * CreateFooter(int, XWPFParagraph[]) methods.
  */
 private CT_HdrFtr buildHdrFtr(String pStyle, XWPFParagraph[] paragraphs, XWPFHeaderFooter wrapper)
 {
     CT_HdrFtr ftr = wrapper._getHdrFtr();
     if (paragraphs != null) {
         for (int i = 0 ; i < paragraphs.Length ; i++) {
             CT_P p = ftr.AddNewP();
             ftr.SetPArray(i, paragraphs[i].GetCTP());
         }
     }
     else {
         CT_P p = ftr.AddNewP();
         byte[] rsidr = doc.Document.body.GetPArray(0).rsidR;
         byte[] rsidrdefault = doc.Document.body.GetPArray(0).rsidRDefault;
         p.rsidP=(rsidr);
         p.rsidRDefault=(rsidrdefault);
         CT_PPr pPr = p.AddNewPPr();
         pPr.AddNewPStyle().val = (pStyle);
     }
     return ftr;
 }
Example #5
0
 private CT_HdrFtr buildHdr(ST_HdrFtr type, String pStyle, XWPFHeaderFooter wrapper, XWPFParagraph[] pars)
 {
     //CTHdrFtr hdr = buildHdrFtr(pStyle, pars);				// MB 24 May 2010
     CT_HdrFtr hdr = buildHdrFtr(pStyle, pars, wrapper);		// MB 24 May 2010
     SetHeaderReference(type, wrapper);
     return hdr;
 }
Example #6
0
 private XmlSerializerNamespaces Commit(XWPFHeaderFooter wrapper)
 {
     XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(new XmlQualifiedName[] {
         new XmlQualifiedName("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006"),
         new XmlQualifiedName("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"),
         new XmlQualifiedName("m", "http://schemas.openxmlformats.org/officeDocument/2006/math"),
         new XmlQualifiedName("v", "urn:schemas-microsoft-com:vml"),
         new XmlQualifiedName("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"),
         new XmlQualifiedName("w10", "urn:schemas-microsoft-com:office:word"),
         new XmlQualifiedName("wne", "http://schemas.microsoft.com/office/word/2006/wordml"),
          new XmlQualifiedName("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")
      });
     return namespaces;
 }