简体   繁体   English

如何使用C#替换书签图像以及Word中的格式?

[英]How to replace bookmark image along with formating in word using C#?

I am automating a document using interop.word. 我正在使用interop.word自动化文档。
I have a template which contents are populated through WinForms controls. 我有一个模板,其内容是通过WinForms控件填充的。 Rest all is peace of cake but when it comes to an image, the code is not replacing the image rather it inserts the image and doesn't keep any format so all my template effort goes to zero. 一切都是安心的,但是当涉及到图像时,代码不会替换图像而是插入图像并且不保留任何格式,因此我的所有模板工作都归零。

I want to insert image at bookmark where it should replace the existing bookmark image and keep its formatting. 我想在书签处插入图像,以替换现有的书签图像并保持其格式。 Can anyone give me the best way of doing it? 谁能给我最好的方式呢?

Here is the code: 这是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace GISv1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string address = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);


            //Creating Object for word application
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();

            //Creating Object for documnt
            Microsoft.Office.Interop.Word.Document doc = new Document();

            //Opening the document
            doc = app.Documents.Open(address + "//ReportTemplate11.dotx");

            //Activating for Editing
            doc.Activate();

            //Bookmarks Finding and Replacing
            Bookmarks books = doc.Bookmarks;

            books["Ref"].Range.Text = tb_Reference.Text;
            books["Id"].Range.Text = tb_RptId.Text;
            books["RptNo"].Range.Text = tb_RptNo.Text;
            books["ClientName"].Range.Text = tb_Client.Text;
            books["ProjectName1"].Range.Text = tb_Project.Text;
            books["ProjectName2"].Range.Text = tb_Project.Text;
            books["ProjectName3"].Range.Text = tb_Project.Text;
            books["ProjectArea"].Range.Text = cb_Area.Text;
            books["FieldTestName"].Range.Text = tb_FieldName.Text;
            books["FieldTestDesignation"].Range.Text = tb_FieldDesignation.Text;
            books["FieldTestDate"].Range.Text = tb_FieldDate.Text;
            books["LabTestName"].Range.Text = tb_LabName.Text;
            books["LabTestDesignation"].Range.Text = tb_LabDesignation.Text;
            books["LabTestDate"].Range.Text = tb_LabDate.Text;
            books["PreparedName"].Range.Text = tb_PreparedName.Text;
            books["PreparedDesignation"].Range.Text = tb_PreparedDesignation.Text;
            books["PreparedDate"].Range.Text = tb_PreparedDate.Text;
            books["VettedName"].Range.Text = tb_VettedName.Text;
            books["VettedDesignation"].Range.Text = tb_VettedDesignation.Text;
            books["VettedDate"].Range.Text = tb_VettedDate.Text;
            books["ApprovedName"].Range.Text = tb_ApprovedName.Text;
            books["ApprovedDesignation"].Range.Text = tb_ApprovedDesignation.Text;
            books["ApprovedDate"].Range.Text = tb_ApprovedDate.Text;


            books["TitlePagePic"].Range.InlineShapes.AddPicture(address + "//PAF SIte Water Tanks.jpg");


            MessageBox.Show("Data is saved");
            app.Visible = true;




        }
    }
}

If I understand your question correctly, Word's object model doesn't support what you want. 如果我正确理解了您的问题,则Word的对象模型不支持您想要的。 There's no way to stick another graphic into a bookmark and have it replace the original InlineShape and take on the properties of that original. 无法将其他图形粘贴到书签中,而不用替换原始InlineShape并采用该原始属性。 More recent versions of Word have functionality in the UI, for the user, that do that. Word的最新版本在用户界面中具有针对用户的功能。 But it hasn't been provided in the object model. 但是对象模型中尚未提供它。

You'd need to store every relevant property and re-apply it after inserting the graphic. 您需要存储所有相关属性,并在插入图形后重新应用它。

A possible work-around for the sizing is to store the graphic in a one-cell table with the exact width needed for the image (the height of an inserted graphic will adjust to the width proportionally). 调整大小的一种可能的解决方法是将图形存储在一个单元格表格中,该表格具有图像所需的确切宽度(插入的图形的高度将按比例调整为宽度)。 Bookmark the entire cell, not just the graphic. 标记整个单元格,而不只是图形。 The following code snippet will insert the new graphic then delete the old: 以下代码段将插入新图形,然后删除旧图形:

Word.Range rngImage = books["TitlePagePic"].Range;
rngImage.InlineShapes.AddPicture(address + "//PAF SIte Water Tanks.jpg");
rngImage.InlineShapes[2].Delete();

But if you have any other kind of formatting that will need to be re-applied. 但是,如果您有任何其他类型的格式,则需要重新应用。

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

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