我正在尝试遍历一个Dataset
,使用 Aspose.Words Mail-Merge 功能为每个项目创建一个页面。下面的代码循环通过Dataset
- 并将一些值传递给 Mail-Merge Execute 函数。
var blankDocument = new Document();
var pageDocument = new Document(sFilename);
...
foreach (DataRow row in ds.Tables[0].Rows){
var sBarCode = row["BarCode"].ToString();
var imageFilePath = HttpContext.Current.Server.MapPath("\\_temp\\") + sBarCode + ".png";
var tempDoc = (Document)pageDocument.Clone(true);
var fieldNames = new string[] { "Test", "Barcode" };
var fieldData = new object[] { imageFilePath, imageFilePath };
tempDoc.MailMerge.Execute(fieldNames, fieldData);
blankDocument.AppendDocument(tempDoc, ImportFormatMode.KeepSourceFormatting);
}
var stream = new MemoryStream();
blankDocument.Save(stream, SaveFormat.Docx);
// I then output this stream using headers,
// to cause the browser to download the document.
邮件合并项{ MERGEFIELD Test }
从Dataset
. 但是,实际图像使用以下方法在所有页面上显示第 1 页的图像:
{ INCLUDEPICTURE "{MERGEFIELD Barcode }" \* MERGEFORMAT \d }
假设这是我的“条形码”字段数据:
c:\img1.png
c:\img2.png
c:\img3.png
本文档的第一页,c:\img1.png
以文本形式显示“测试”字段。显示的图像是img1.png
。
但是第 2 页显示c:\img2.png
为文本,但显示img1.png
为实际图像。
有人对此有任何见解吗?
编辑:这似乎更像是一个Word问题。当我在 Word 中的Alt+F9模式之间切换时,图像实际上显示c:\img1.png
为源。这就是为什么它会显示在每一页上的原因。
我已将其简化为:
{ INCLUDEPICTURE "{MERGEFIELD Barcode }" \d }
此外,在 Word 的邮件收件人列表中添加了该字段的测试数据。当我预览时,它不会拉入数据,改变图像。所以,这是根本问题。