2012-08-31 167 views
-1

如何在Word文档的标题中插入图像。我已经编写了一个插件,用于在将文档附加到实体中时进行重命名。如何在保存文件之前在Word文档的标题中插入徽标。 C#中的代码也可以帮助我。如何在Word文档标题中插入图像

+0

@Andrew理发 - 为什么它已经关闭?请重新打开它。我面临同样的问题,并得到了另一个解决方案,在下面的答案中不可用。请求您重新打开它 –

回答

0
Document doc = new Document(); 
    doc.LoadFromFile(@"E:\JaneEyre.docx", FileFormat.Docx); 
HeaderFooter header = doc.Sections[0].HeadersFooters.Header; 
    header.Paragraphs[0].Text = "Preview"; 
    Image logo = Image.FromFile(@"D:\E-ICEBLUE.png"); 
    header.Paragraphs[0].AppendPicture(logo).TextWrappingStyle = TextWrappingStyle.Tight; 
    HeaderFooter footer = doc.Sections[0].HeadersFooters.Footer; 
    doc.SaveToFile("Sample.docx", FileFormat.Docx); 
    System.Diagnostics.Process.Start("Sample.docx");footer.Paragraphs[0].Text = "Author: Charlotte Brontë"; 
+0

哈桑,我只使用过它,但它给我在评论复制消息头上,当我尝试打开文件告诉我,该文件已损坏。我已经使用spire.DOC dll只有 – Maddy

+0

你确定你的spire.doc dll可以在你的word版本中使用吗? –

+0

是的。它发出警告,当我尝试打开文件附加后,文件已损坏单击是打开文件。如果我打开文件,图像将被附加。 – Maddy

2

您可以使用此代码的Word文档的标题中插入图片:

foreach (wd.Section section in CurrentDocument.Sections) 
{ 
    word.HeaderFooter header= section.Headers[wd.WdHeaderFooterIndex.wdHeaderFooterPrimary]; 
    wd.Shape oshape = header.Shapes.AddPicture(@"C:\Users\mahammadi\Desktop\icon\plus2.png", left, top, width, height); 
}