2018年2月7日 星期三

[程式類]Aspose如何將樣板WORD插入多筆來自資料庫(Blob)的圖片

Aspose word是一個非常好用的文件產生器, 像是要在本文填入文字, 就要利用WORD
按一下 [插入] > [快速組件] > [功能變數]
[快速組件] 功能表上的 [插入功能變數] 選項。

在左方功能變數名稱選擇MergeField, 在欄位名稱填入例如: NAME 後按下確定
«NAME»

這是WORD裡的功能變數裡的顯示。

而在程式裡C#為例:
        Document docTemplate = new Document(ExportFile);

        DataTable mainTbHeader = new DataTable();
        mainTbHeader.Columns.Add("NAME");     

        DataRow mainDr = mainTbHeader.NewRow();
        mainDr["NAME"] = "NAME1";
        mainTbHeader.Rows.Add(mainDr);
        docTemplate.MailMerge.Execute(mainTbHeader);
        docTemplate.Save(ExportFile);
這樣檔案裡就會照著你的意思,有一文字ASPOSE。

以上為Aspose裡插入文字時的做法。

那如果是多筆文字的話在Word Tamplate要使用TableStart/TableEnd 如下所示:
«TableStart:NameList»
     «Name»
«TableEnd:NameList»

在程式中
DataTable NameList = new DataTable("NameList");

        NameList.Columns.Add("Name");

        // set row data 
DataRow dr = NameList.NewRow();
        dr["Name"]="Name1";
        NameList.Rows.Add(dr);

dr["Name"]="Name2";
NameList.Rows.Add(dr);

ds.Tables.Add(NameList );
        docTemplate.MailMerge.ExecuteWithRegions(ds);
這樣便可以將多筆資料內容,一筆一筆的填入。

重點來了, 本旨想要解釋的圖片呢?
Word Tamplate要使用Table,這一點和上面多筆文字內容一樣要使用table, 但是在Word的功能變數內容有些不同。如果是圖面要使用[Image:]這個特別的識別Tag
«TableStart:NAMELIST»
     «NAME»
     «Image:Picture»
«TableEnd:NAMELIST»

在程式中
        DataTable NameList = new DataTable("NameList");

        NameList.Columns.Add("Name");
        NameList.Columns.Add("Picture", typeof(Byte[]));// Byte Image

        // set row data 
        DataRow dr = NameList.NewRow();
        dr["Name"] = "Name1";
        byte[] myByteArray = GetImageFile(PK); //try to get the Image file
        dr["Picture"] = myByteArray;
        NameList.Rows.Add(dr);

        dr["Name"] = "Name2";
        byte[] myByteArray = GetImageFile(PK2); //try to get the Image file
        dr["Picture"] = myByteArray;
        NameList.Rows.Add(dr);

        ds.Tables.Add(NameList);
        docTemplate.MailMerge.ExecuteWithRegions(ds);

        //this part for Bind Image
        DocumentB起彼落uilder builder = new DocumentBuilder(docTemplate);
        ReportingEngine engine = new ReportingEngine();

        engine.BuildReport(docTemplate, ds, "ds");

請加入此Class
public class HandleMergeImageFieldFromBlob : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {

    }

    /// <summary>
    /// This is called when mail merge engine encounters Image:XXX merge field in the document.
    /// You have a chance to return an Image object, file name or a stream that contains the image.
    /// </summary>
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        if (e.FieldValue == null || e.FieldValue is System.DBNull) return;
        // The field value is a byte array, just cast it and create a stream on it.

        MemoryStream imageStream = new MemoryStream((byte[])e.FieldValue);
        // Now the mail merge engine will retrieve the image from the stream.

        e.ImageStream = imageStream;

        e.ImageHeight.Value = 166;
        e.ImageWidth.Value = 250;
    }
}

沒有留言:

張貼留言

POSTGRE SQL 想使用變數去定義一段日期區間, 但遍尋不到像是在 MSSQL 時常用的 DECLARE @count int, @x int, @y nvarchar(10)... 想到一招利用 CTE 方式去定義一個這樣類似變數的作法來實現... 原來的script w...