如何使用C#替换书签图像以及单词格式?

问题描述 投票:0回答:1

我正在使用interop.word自动化文档。 我有一个模板,其中的内容通过WinForms控件填充。休息一切都是和平的蛋糕,但是当涉及到图像时,代码不是替换图像而是插入图像而不保留任何格式,因此我的所有模板努力都归零。

我想在书签处插入图像,它应该替换现有的书签图像并保持其格式。谁能给我最好的方法呢?

这是代码:

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;




        }
    }
}
c# winforms ms-word office-interop
1个回答
0
投票

如果我理解你的问题,Word的对象模型不支持你想要的。没有办法将另一个图形粘贴到书签中并让它替换原始的InlineShape并采用该原始的属性。更新版本的Word在UI中为用户提供了这样做的功能。但它没有在对象模型中提供。

您需要存储每个相关属性,并在插入图形后重新应用它。

尺寸调整的一种可能的解决方法是将图形存储在具有图像所需的精确宽度的单格表格中(插入的图形的高度将按比例调整为宽度)。为整个单元格添加书签,而不仅仅是图形。以下代码片段将插入新图形,然后删除旧图形:

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

但是,如果您有任何其他类型的格式需要重新应用。

© www.soinside.com 2019 - 2024. All rights reserved.