合併列印中圖片欄位的處理

來源:互聯網
上載者:User

網上找了個Aspose.Words來解決使用者匯出列印的要求,模板中還有圖片。以前都是採用調DOM的方式來實現,其實發現用郵件匯出的方式是最省的。研究了一下其中關於圖片的處理。

 public class WordHelper
    {
        public static void MailMerge(DataSet datasource, Page page,string TempDocName,string FileNmae)
        {
            Document doc = new Document(TempDocName);
            doc.MailMerge.MergeImageField += new Aspose.Words.Reporting.MergeImageFieldEventHandler(MailMerge_MergeImageField);
            doc.MailMerge.ExecuteWithRegions(datasource);
            doc.Save(FileNmae, SaveFormat.Doc, SaveType.OpenInWord, page.Response);
        }
        /// <summary>
        /// 圖片欄位的處理。需要在模板加照片或圖片的地方,同樣插入域,但網域名稱設定為:Image:資料表欄位名,該處的資料表欄位可以為String(也就是圖片地址),也可以是二進位的圖片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void MailMerge_MergeImageField(object sender, Aspose.Words.Reporting.MergeImageFieldEventArgs e)
        {
             if ( e.FieldValue!=System.DBNull.Value)
           {
               Image img = Image.FromStream(new System.IO.MemoryStream((byte[])e.FieldValue));
              img= img.GetThumbnailImage(120, 140, null, IntPtr.Zero);//產生格式的圖片
               e.Image = img;
  }
 }
模板設定時。圖片的地方 《Image:myimg》

 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.