在C#在相同的位置替换为一个图像的文字 [英] in C# replace text with an image at the same position

查看:463
本文介绍了在C#在相同的位置替换为一个图像的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此码与图像替换文本,但其放置的图像的多个拷贝,并放置在该文件的开头。我希望图像被放置在相同的位置文本存在。我发现文本在表格单元格可用。难道是因为那个

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;使用System.Diagnostics程序
;
:使用System.IO;
使用文字=的Microsoft.Office.Interop.Word;使用System.Runtime.InteropServices
;
//使用System.Drawing中;


命名空间WritingIntoDocx
{
[标记有​​ComVisible特性(真)]
公共接口IMyClass
{

无效DocumentDigitalSign(字符串filep,串findt,串replacet);
}


[标记有​​ComVisible特性(真)]
[ClassInterface(ClassInterfaceType.None)

公共类节目:IMyClass
{
公共无效DocumentDigitalSign(字符串filep,串findt,字符串的ImagePath)
{
字符串的文件路径= filep;
串FINDTEXT = findt;
word.Application应用=新word.Application();
word.Document DOC = app.Documents.Open(文件路径);

word.Range myStoryRange = doc.Range();

//首先使用选择
word.Find myFind = myStoryRange.Find搜索的主要文件;
myFind.Text = FINDTEXT; myFind.Replacement.Application.Selection.InlineShapes.AddPicture(的ImagePath);
myFind.Forward = TRUE;
myFind.Wrap = word.WdFindWrap.wdFindContinue;
myFind.Format = FALSE;
myFind.MatchCase = FALSE;
myFind.MatchWholeWord = FALSE;
myFind.MatchWildcards = FALSE;
myFind.MatchSoundsLike = FALSE;
myFind.MatchAllWordForms = FALSE;
myFind.Execute(更换:word.WdReplace.wdReplaceAll);

//'现在搜索使用范围
的foreach(word.Range otherStoryRange在doc.StoryRanges)所有其他的故事
{
如果(otherStoryRange.StoryType!=字.WdStoryType.wdMainTextStory)
{
word.Find myOtherFind = otherStoryRange.Find;
myOtherFind.Text = FINDTEXT; myOtherFind.Replacement.Application.Selection.InlineShapes.AddPicture(的ImagePath);
myOtherFind.Wrap = word.WdFindWrap.wdFindContinue;
myOtherFind.Execute(更换:word.WdReplace.wdReplaceAll);
}

//'现在搜索其他故事的下一个全部的故事(doc.storyRanges不似乎子的故事级联)
word.Range nextStoryRange = otherStoryRange.NextStoryRange;
,而(nextStoryRange!= NULL)
{
word.Find myNextStoryFind = nextStoryRange.Find;
myNextStoryFind.Text = FINDTEXT;
myNextStoryFind.Replacement.Application.Selection.InlineShapes.AddPicture(的ImagePath);
myNextStoryFind.Wrap = word.WdFindWrap.wdFindContinue;
myNextStoryFind.Execute(更换:word.WdReplace.wdReplaceAll);

nextStoryRange = nextStoryRange.NextStoryRange;
}

}
app.Documents.Save();
app.Documents.Close();
}

}


}


< DIV CLASS =h2_lin>解决方案

Replacement.Application 是将应用程序对象的引用。当你调用给AddPicture()上的照片立即在即使执行查找操作之前,在当前位置插入。



我看到两种可能性:




  1. 加载的图片,将其放置到Windows剪贴板中,然后执行查找操作指定^ C作为替换文本。 Word将替换^ C与剪贴板的当前内容。这就是文档说:




ReplaceWith

类型:System.Object

可选的对象。

替换文本。若要删除由Find参数指定的文本,使用空字符串()。您可以指定特殊字符和高级搜索条件,就像您查找的说法去做。要指定一个图形对象或其他非文本项作为替代,移动项目到剪贴板,并指定^ c按钮ReplaceWith。





  • 请不要使用wdReplaceAll,但wdReplaceNone,使查找操作本身并不做任何更换。但是,你就必须在发现地方插入你的内容的机会。这样做,在一个循环,直到不再出现被发现。


  • This code is replacing the text with an image but its placing the multiple copies of an image and placing them in the beginning of the document. I want the image to be placed at the same position where text was present. My find text is available in the table cell. Is it due to that?

    using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Diagnostics;
        using System.IO;
        using word = Microsoft.Office.Interop.Word;
        using System.Runtime.InteropServices;
        //using System.Drawing;
    
    
        namespace WritingIntoDocx
        {
            [ComVisible(true)]
            public interface IMyClass
            {
    
              void DocumentDigitalSign(string filep,string findt,string replacet);
            }
    
    
            [ComVisible(true)]
            [ClassInterface(ClassInterfaceType.None)]
    
           public class Program : IMyClass
            {
                 public void DocumentDigitalSign(string filep, string findt, string imagepath)
                {
                    string filepath = filep;
                    string Findtext = findt;
                    word.Application app = new word.Application();
                    word.Document doc = app.Documents.Open(filepath);
    
                    word.Range myStoryRange = doc.Range();
    
                    //First search the main document using the Selection
                    word.Find myFind = myStoryRange.Find;
                    myFind.Text = Findtext;               myFind.Replacement.Application.Selection.InlineShapes.AddPicture(imagepath);
                    myFind.Forward = true;
                    myFind.Wrap = word.WdFindWrap.wdFindContinue;
                    myFind.Format = false;
                    myFind.MatchCase = false;
                    myFind.MatchWholeWord = false;
                    myFind.MatchWildcards = false;
                    myFind.MatchSoundsLike = false;
                    myFind.MatchAllWordForms = false;
                    myFind.Execute(Replace: word.WdReplace.wdReplaceAll);
    
                    //'Now search all other stories using Ranges
                    foreach (word.Range otherStoryRange in doc.StoryRanges)
                    {
                        if (otherStoryRange.StoryType != word.WdStoryType.wdMainTextStory)
                        {
                            word.Find myOtherFind = otherStoryRange.Find;
                            myOtherFind.Text = Findtext;          myOtherFind.Replacement.Application.Selection.InlineShapes.AddPicture(imagepath);
                            myOtherFind.Wrap = word.WdFindWrap.wdFindContinue;
                            myOtherFind.Execute(Replace: word.WdReplace.wdReplaceAll);
                        }
    
                        // 'Now search all next stories of other stories (doc.storyRanges dont seem to cascades in sub story)
                        word.Range nextStoryRange = otherStoryRange.NextStoryRange;
                        while (nextStoryRange != null)
                        {
                            word.Find myNextStoryFind = nextStoryRange.Find;
                            myNextStoryFind.Text = Findtext;
                            myNextStoryFind.Replacement.Application.Selection.InlineShapes.AddPicture(imagepath);
                            myNextStoryFind.Wrap = word.WdFindWrap.wdFindContinue;
                            myNextStoryFind.Execute(Replace: word.WdReplace.wdReplaceAll);
    
                            nextStoryRange = nextStoryRange.NextStoryRange;
                        }
    
                    }
                    app.Documents.Save();
                    app.Documents.Close();
                }
    
            }
    
    
     }
    

    解决方案

    Replacement.Application is a reference to the application object. When you call AddPicture() on that, the picture is immediately inserted at the current position before the find operation is even executed.

    I see two possibilities:

    1. Load the picture, place it into the Windows clipboard and then execute the find operation specifying "^c" as replacement text. Word will replace "^c" with the current content of the clipboard. This is what the documentation says:

    ReplaceWith
    Type: System.Object
    Optional Object.
    The replacement text. To delete the text specified by the Find argument, use an empty string (""). You specify special characters and advanced search criteria just as you do for the Find argument. To specify a graphic object or other non-text item as the replacement, move the item to the Clipboard and specify "^c" for ReplaceWith.

    1. Do not use wdReplaceAll, but wdReplaceNone, so that the find operation itself does not do any replacing. But you then have the chance to insert your content at the place found. Do that in a loop until no more occurrence is found.

    这篇关于在C#在相同的位置替换为一个图像的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆