Syncfusion DocIO -- 如何使用 BookmarksNavigator 在书签处插入图像(本地文件)

发布于 2024-07-25 16:24:44 字数 241 浏览 9 评论 0原文

我一直在使用 Syncfusion DocIO 从我的 .net 应用程序 (winforms) 生成 MS Word 文档。 到目前为止,我已经处理了纯文本,并且在 Word 文档模板中插入文本相当简单,其中书签作为文本插入的参考点。

我正在使用 BookmarksNavigator.MoveToBookmark() 导航书签。 现在我需要在书签中插入图像,但我不知道如何去做。

请帮忙...

谢谢。

I have been using Syncfusion DocIO for generating MS Word documents from my .net applications (winforms). So far I have dealt with plain text and it is fairly straightforward to insert text in a word document template where bookmarks serve as reference points for text insertion.

I am navigating the bookmarks using BookmarksNavigator.MoveToBookmark() . Now I need to insert an image at a bookmark but I am at a loss at how to go about it.

Please help...

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

長街聽風 2024-08-01 16:24:44

专门用于将其添加到书签:

            //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);

Specifically for adding it to a bookmark :

            //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);
舟遥客 2024-08-01 16:24:44
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();
}
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();
}
辞慾 2024-08-01 16:24:44

请参阅此处:https://help.syncfusion.com/file-formats /docio/working-with-mailmerge

1) 您应该创建名为“Template.docx”的 docx 文件。 该文件将用作模板。
在您的 docx 文件中创建 MergeField 类型的字段。
输入图片此处描述

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();
}

See here: https://help.syncfusion.com/file-formats/docio/working-with-mailmerge

1) You should create docx file with name "Template.docx". This file will use as template.
In your docx file create Field of type MergeField.
enter image description here

2) Create MergeFiled with name Image:Krishna
enter image description here

3)

using Syncfusion.DocIO.DLS;
using System.Drawing;

public class Source
{
    public Image Krishna { get; set; } = Image.FromFile(@"C:\1.png");
}

and generating code:

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文