C#Aspose.word在指定点处插入图片

///


        /// 在指定点处插入一个图片(按长宽比例缩放)
        ///

        ///
        ///
        ///
        /// 设置宽,默认200
        public static void ImportPictureAtPoint(Document doc,string pic,string bm,double width=200)
        {
            try
            {
                if (AsposeWordHelper.BookMarkIsExist(doc,bm))
                {
                    Bookmark bookmark = doc.Range.Bookmarks[bm];

                    DocumentBuilder builder = new DocumentBuilder(doc);

                    builder.MoveToBookmark(bm);

                    //获取当前所在的段落
                    Paragraph paragraph = builder.CurrentParagraph;


                    Shape shape = builder.InsertImage(pic);

                    double w = shape.Width;

                    shape.Width = width;

                    shape.Height = (width / w) * shape.Height;

                    shape.RelativeVerticalPosition = RelativeVerticalPosition.Paragraph;

                    shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Character;

                    shape.Left = paragraph.GetText().Length;

                    shape.WrapType = WrapType.None;


                    //随机上下平移和旋转
                    Random random = new Random();
                    int top_random = random.Next(-5, 5);
                    int left_random = random.Next(-5, -5);
                    int rotation_random = random.Next(-15, 15);

                    shape.Top += top_random;

                    shape.Left += left_random;

                    shape.Rotation = rotation_random;


                }
                
            }
            catch (Exception)
            {

                throw;
            }
        }

你可能感兴趣的:(Word,Excel,TXT,XML)