簡體   English   中英

如何使用C#替換書簽圖像以及Word中的格式?

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

我正在使用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;




        }
    }
}

如果我正確理解了您的問題,則Word的對象模型不支持您想要的。 無法將其他圖形粘貼到書簽中,而不用替換原始InlineShape並采用該原始屬性。 Word的最新版本在用戶界面中具有針對用戶的功能。 但是對象模型中尚未提供它。

您需要存儲所有相關屬性,並在插入圖形后重新應用它。

調整大小的一種可能的解決方法是將圖形存儲在一個單元格表格中,該表格具有圖像所需的確切寬度(插入的圖形的高度將按比例調整為寬度)。 標記整個單元格,而不只是圖形。 以下代碼段將插入新圖形,然后刪除舊圖形:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM