我有一个包含文字和图像的Word文档。当我阅读此文档并将内容写回到另一个文档时,图像丢失。 我还如何复制图像?
示例:Word文档具有
将内容拧到其他文档后,输出为
代码示例:
object missing = System.Reflection.Missing.Value;
object filename = "C:\\SampleInput.docx";
Microsoft.Office.Interop.Word.ApplicationClass AC = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
object readOnly = false;
object isVisible = true;
doc = AC.Documents.Open(ref filename, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref isVisible, ref missing, ref missing, ref missing);
string inputText = doc.Content.Text;
Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
winword.ShowAnimation = false;
winword.Visible = false;
Microsoft.Office.Interop.Word.Document document = winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
document.Content.SetRange(0, 0);
document.Content.Text = inputText;
object outputPath = @"c:\FormattedOutput.docx";
document.SaveAs2(ref outputPath);
document.Close(ref missing, ref missing, ref missing);
document = null;
winword.Quit(ref missing, ref missing, ref missing);
winword = null;
答案 0 :(得分:0)
您应该使用Range.FormattedText属性。换句话说,在打开源文件和目标文件并建立范围的情况下,命令如下所示:
DestinationRange.FormattedText = SourceRange.FormattedText;
您当前的代码只是获取纯文本并将其存储在字符串变量中。字符串变量不能容纳图像对象。