Aspose.Words使用word模板中的書籤插入圖片(導出含有圖片的word)

核心代碼爲:

protected void Button2_Click(object sender, EventArgs e)
    {
        string templatePath = HttpContext.Current.Server.MapPath(@"~/模板表.doc");
        Document doc = new Document(templatePath);
        foreach (Bookmark mark in doc.Range.Bookmarks)
        {
            if (mark != null)
            {
                switch (mark.Name)
                { 
                    case "NAME":
                        mark.Text = "龔輝";
                        break;
                    case "PHOTO":
                        DocumentBuilder builder = new DocumentBuilder(doc);
                        string imgPath = Server.MapPath(@"~/pretty.jpg");
                        if (File.Exists(imgPath))
                        {
                            builder.MoveToBookmark("PHOTO");                           
                            builder.InsertImage(imgPath, RelativeHorizontalPosition.Margin, 1, RelativeVerticalPosition.Margin, 1, 100, 125, WrapType.Square);
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        doc.Save(Guid.NewGuid().ToString() + ".doc", SaveFormat.Doc, SaveType.OpenInWord, this.Response);
    }


顯示效果如下:

 

 

注意:要在word模板表中插入NAME,PHOTO書籤。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章