/// <summary> /// 行内插入方式。向WordDoc中插入图像,原控件有Bug,故采用自定义函数 /// </summary> /// <param name="CurRun">当前段落中的Run</param> /// <param name="id"></param> /// <param name="width"></param> /// <param name="height"></param> private void CreatePictureInLine(XWPFRun CurRun, string id, int width, int height) { int EMU = 9525; width *= EMU; height *= EMU; string picXml = "" //+ "<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\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" + " <pic:nvPicPr>" + "<pic:cNvPr id=\"" + "0" + "\" name=\"Generated\"/>" + " <pic:cNvPicPr/>" + " </pic:nvPicPr>" + " <pic:blipFill>" + " <a:blip r:embed=\"" + id + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" + " <a:stretch>" + " <a:fillRect/>" + " </a:stretch>" + " </pic:blipFill>" + " <pic:spPr>" + " <a:xfrm>" + " <a:off x=\"0\" y=\"20\"/>" + " <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" + " </a:xfrm>" + " <a:prstGeom prst=\"rect\">" + " <a:avLst/>" + " </a:prstGeom>" + " </pic:spPr>" + " </pic:pic>"; //+ " </a:graphicData>" + "</a:graphic>"; CT_Inline inline = CurRun.GetCTR().AddNewDrawing().AddNewInline(); inline.graphic = new CT_GraphicalObject(); inline.graphic.graphicData = new CT_GraphicalObjectData(); inline.graphic.graphicData.uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"; // CT_GraphicalObjectData graphicData = inline.graphic.AddNewGraphicData(); // graphicData.uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"; //XmlDocument xmlDoc = new XmlDocument(); try { //xmlDoc.LoadXml(picXml); //var element = xmlDoc.DocumentElement; inline.graphic.graphicData.AddPicElement(picXml); } catch (XmlException xe) { } NPOI.OpenXmlFormats.Dml.WordProcessing.CT_PositiveSize2D extent = inline.AddNewExtent(); extent.cx = width; extent.cy = height; NPOI.OpenXmlFormats.Dml.WordProcessing.CT_NonVisualDrawingProps docPr = inline.AddNewDocPr(); docPr.id = 1; docPr.name = "Image" + id; }
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); } }
/** * Adds a picture to the run. This method handles * attaching the picture data to the overall file. * * @see NPOI.XWPF.UserModel.Document#PICTURE_TYPE_EMF * @see NPOI.XWPF.UserModel.Document#PICTURE_TYPE_WMF * @see NPOI.XWPF.UserModel.Document#PICTURE_TYPE_PICT * @see NPOI.XWPF.UserModel.Document#PICTURE_TYPE_JPEG * @see NPOI.XWPF.UserModel.Document#PICTURE_TYPE_PNG * @see NPOI.XWPF.UserModel.Document#PICTURE_TYPE_DIB * * @param pictureData The raw picture data * @param pictureType The type of the picture, eg {@link Document#PICTURE_TYPE_JPEG} * @param width width in EMUs. To convert to / from points use {@link org.apache.poi.util.Units} * @param height height in EMUs. To convert to / from points use {@link org.apache.poi.util.Units} * @throws NPOI.Openxml4j.exceptions.InvalidFormatException * @throws IOException */ public XWPFPicture AddPicture(Stream pictureData, int pictureType, String filename, int width, int height) { XWPFDocument doc = parent.Document; // Add the picture + relationship String relationId = doc.AddPictureData(pictureData, pictureType); XWPFPictureData picData = (XWPFPictureData)doc.GetRelationById(relationId); // 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>"; //inline.Set((xml)); XmlDocument xmlDoc = new XmlDocument(); //XmlElement el = xmlDoc.CreateElement("pic", "pic", "http://schemas.openxmlformats.org/drawingml/2006/picture"); 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); 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); }
public CT_NonVisualDrawingProps AddNewDocPr() { if (this.docPrField == null) this.docPrField = new CT_NonVisualDrawingProps(); return this.docPrField; }
public static CT_NonVisualDrawingProps Parse(XmlNode node, XmlNamespaceManager namespaceManager) { if (node == null) return null; CT_NonVisualDrawingProps ctObj = new CT_NonVisualDrawingProps(); ctObj.id = XmlHelper.ReadUInt(node.Attributes["id"]); ctObj.name = XmlHelper.ReadString(node.Attributes["name"]); ctObj.descr = XmlHelper.ReadString(node.Attributes["descr"]); ctObj.hidden = XmlHelper.ReadBool(node.Attributes["hidden"]); foreach (XmlNode childNode in node.ChildNodes) { if (childNode.LocalName == "hlinkClick") ctObj.hlinkClick = CT_Hyperlink.Parse(childNode, namespaceManager); else if (childNode.LocalName == "hlinkHover") ctObj.hlinkHover = CT_Hyperlink.Parse(childNode, namespaceManager); else if (childNode.LocalName == "extLst") ctObj.extLst = CT_OfficeArtExtensionList.Parse(childNode, namespaceManager); } return ctObj; }