我一直在使用Syncfusion DocIO从我的.net应用程序(winforms)生成MS Word文档。到目前为止,我已经处理了纯文本,在word文档模板中插入文本非常简单,其中书签作为文本插入的参考点。
我使用BookmarksNavigator.MoveToBookmark()浏览书签。现在我需要在书签上插入图像,但我对如何处理它感到茫然。
请帮忙......
感谢。
答案 0 :(得分:2)
专门用于将其添加到书签:
//Move to the specified bookmark
bk.MoveToBookmark(bookmark);
//Insert the picture into the specified bookmark location
bk.DeleteBookmarkContent(true);
// we assume the text is a full pathname for an image file
// get the image file
System.Drawing.Image image = System.Drawing.Image.FromFile(sText);
IWParagraph paragraph = new WParagraph(document);
paragraph.AppendPicture(image);
bk.InsertParagraph(paragraph);
答案 1 :(得分:1)
private System.Drawing.Image LoadSignature(string sFileName)
{
string sImagePath = sFileName;
System.Drawing.Image image = System.Drawing.Image.FromFile(sImagePath);
return image;
}
private void MergeSignature(WordDocument doc, string sFile, string sBalise)
{
System.Drawing.Image iSignature = LoadSignature(sFile);
WordDocument ImgDoc = new WordDocument();
ImgDoc.AddSection();
ImgDoc.Sections[0].AddParagraph().AppendPicture(iSignature);
if (iSignature != null)
{
TextSelection ts = null ;
Regex pattern = new Regex(sBalise);
ts = doc.Find(pattern);
if (ts != null)
{
doc.ReplaceFirst = true;
doc.Replace(pattern, ImgDoc, false);
}
}
iSignature.Dispose();
}
答案 2 :(得分:0)
参见此处:https://help.syncfusion.com/file-formats/docio/working-with-mailmerge
1)您应该创建名称为“ Template.docx”的docx文件。该文件将用作模板。 在您的docx文件中,创建MergeField类型的Field。
2)创建名称为 Image:Krishna 的MergeFiled
3)
using Syncfusion.DocIO.DLS;
using System.Drawing;
public class Source
{
public Image Krishna { get; set; } = Image.FromFile(@"C:\1.png");
}
并生成代码:
public static void Generate()
{
WordDocument doc = new WordDocument("Template.docx");
Source data = new Source();
var dataTable = new MailMergeDataTable("", new Source[] { data });
doc.MailMerge.ExecuteGroup(dataTable);
doc.Save("result.docx");
doc.Close();
}