简体   繁体   English

如何使用C#在word 2007文档中的特定坐标处插入图片/文本标签?

[英]How to insert a picture/text label at specific coordinates in a word 2007 document using C#?

I can do this to insert picture and text at a default location: 我可以这样做,在默认位置插入图片和文字:

        private static object objTrue = true;
        private static object objFalse = false;
        private static object objMissing = Missing.Value;
        private static object objEndOfDoc = @"\endofdoc"; // \endofdoc is a predefined bookmark.

        ...

        this.WordApp = new Word.Application();
        this.WordDoc = new Word.Document();
        this.WordApp.Visible = true;
        this.WordDoc = this.WordApp.Documents.Add(ref objMissing, ref objMissing, ref objMissing, ref objMissing);

        this.WordDoc.Content.InlineShapes.AddPicture(
        FileName: @"C:\MyLogo.png",
        LinkToFile: ref objMissing,
        SaveWithDocument: ref objTrue,
        Range: objMissing);

        // Insert a paragraph at the beginning of the document.
        var paragraph1 = this.WordDoc.Content.Paragraphs.Add(ref objMissing);
        paragraph1.Range.Text = "Heading 1";
        paragraph1.Range.Font.Bold = 1;
        paragraph1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
        paragraph1.Range.InsertParagraphAfter();

How can I move it to any location I want? 如何将它移动到我想要的任何位置? When I am creating PowerPoint (manually), I can position stuff anywhere. 当我创建PowerPoint(手动)时,我可以在任何地方放置东西。 When I manually insert a shape, I can position it anywhere. 当我手动插入一个形状时,我可以将它放在任何地方。 How can I achieve similar result when inserting pictures and text labels programmatically using c#? 使用c#以编程方式插入图片和文本标签时,如何获得类似的结果? I have not had success figuring this out using Google searches. 我没有成功使用Google搜索来解决这个问题。

The Range class is used in word to determine where almost everything goes in a document. Range类用于单词以确定文档中几乎所有内容的位置。 If you replaced the Range parameter in this.WordDoc.Content.InlineShapes.AddPicture with a valid Range object, it would insert the picture at that place in the word document, and the same goes for paragraphs. 如果使用有效的Range对象替换this.WordDoc.Content.InlineShapes.AddPictureRange参数,它会将图片插入到word文档中的那个位置,并且段落也是如此。

According to MSDN for the AddPicture method on InlineShapes: 根据MSDN对InlineShapes的AddPicture方法:

Range: Optional Object. 范围:可选对象。 The location where the picture will be placed in the text. 图片放置在文本中的位置。 If the range isn't collapsed, the picture replaces the range; 如果范围未折叠,则图片将替换范围; otherwise, the picture is inserted. 否则,插入图片。 If this argument is omitted, the picture is placed automatically. 如果省略此参数,则会自动放置图片。

Another way would be to use the Shapes member of the document instead of InlineShapes . 另一种方法是使用文档的Shapes成员而不是InlineShapes The AddPicture method on the Shapes class allows you to specify the coordinates: Shapes类上的AddPicture方法允许您指定坐标:

this.WordDoc.Content.Shapes.AddPicture(
    [In] string FileName, 
    [In, Optional] ref object LinkToFile, 
    [In, Optional] ref object SaveWithDocument, 
    [In, Optional] ref object Left, 
    [In, Optional] ref object Top, 
    [In, Optional] ref object Width, 
    [In, Optional] ref object Height, 
    [In, Optional] ref object Anchor
);

InlineShapes.AddPicture InlineShapes.AddPicture

Shapes.AddPicture Shapes.AddPicture

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM