Word中截取部分内容并保存为jpg图片的方法
2014-04-29 19:41
483 查看
private void button1_Click(object sender, EventArgs e)
{
var appWord = new Microsoft.Office.Interop.Word.Application();
var doc = new Microsoft.Office.Interop.Word.Document();
object oMissing = System.Reflection.Missing.Value;//这个是什么东西,我始终没搞明白-_-
//打开模板文档,并指定doc的文档类型
object objTemplate = @"c:\18_0000f032-1c39-4bc9-a47c-327d8d493978.docx";
object objDocType = WdDocumentType.wdTypeDocument;
appWord.Visible = true;
doc = appWord.Documents.Add(ref objTemplate, ref oMissing, ref objDocType, ref oMissing);
object start = 0;
object end = 160;
doc.Range(ref start, ref end).Select();
appWord.Selection.Range.Copy();
var process = new Process();
process = Process.Start("mspaint.exe",@"c:\1.jpg");
var processId = process.Id;
var element = FindWindowByProcessId(processId);
SendKeys.SendWait("^v"); //发送 Ctrl + s 键
SendKeys.SendWait("^s"); //发送 Ctrl + s 键
SendKeys.SendWait("%{F4}"); // 发送 Alt + F4 键
}
public static AutomationElement FindWindowByProcessId(int processId)
{
AutomationElement targetWindow = null;
int count = 0;
try
{
Process p = Process.GetProcessById(processId);
targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
return targetWindow;
}
catch (Exception ex)
{
count++;
StringBuilder sb = new StringBuilder();
string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
if (count > 5)
{
throw new InvalidProgramException(message, ex);
}
else
{
return FindWindowByProcessId(processId);
}
}
}
{
var appWord = new Microsoft.Office.Interop.Word.Application();
var doc = new Microsoft.Office.Interop.Word.Document();
object oMissing = System.Reflection.Missing.Value;//这个是什么东西,我始终没搞明白-_-
//打开模板文档,并指定doc的文档类型
object objTemplate = @"c:\18_0000f032-1c39-4bc9-a47c-327d8d493978.docx";
object objDocType = WdDocumentType.wdTypeDocument;
appWord.Visible = true;
doc = appWord.Documents.Add(ref objTemplate, ref oMissing, ref objDocType, ref oMissing);
object start = 0;
object end = 160;
doc.Range(ref start, ref end).Select();
appWord.Selection.Range.Copy();
var process = new Process();
process = Process.Start("mspaint.exe",@"c:\1.jpg");
var processId = process.Id;
var element = FindWindowByProcessId(processId);
SendKeys.SendWait("^v"); //发送 Ctrl + s 键
SendKeys.SendWait("^s"); //发送 Ctrl + s 键
SendKeys.SendWait("%{F4}"); // 发送 Alt + F4 键
}
public static AutomationElement FindWindowByProcessId(int processId)
{
AutomationElement targetWindow = null;
int count = 0;
try
{
Process p = Process.GetProcessById(processId);
targetWindow = AutomationElement.FromHandle(p.MainWindowHandle);
return targetWindow;
}
catch (Exception ex)
{
count++;
StringBuilder sb = new StringBuilder();
string message = sb.AppendLine(string.Format("Target window is not existing.try #{0}", count)).ToString();
if (count > 5)
{
throw new InvalidProgramException(message, ex);
}
else
{
return FindWindowByProcessId(processId);
}
}
}
相关文章推荐
- 截取JPanel的部分内容并保存为图片
- 用Canvas实现截取网页内容保存为图片
- WORD 文档中的图片另存为 .jpg 格式图片方法
- 利用ffmpeg截取视频图像并保存为ppm和jpg图片格式
- 保存图片,文本文件,word文档等等方法都是一样的
- Vim 中截取部分内容保存到其他文件
- Vim 中截取部分内容保存到其他文件
- word或者pdf文件全部保存为图片的方法
- 截取部分网页以图片保存到本地
- php采集内容中带有图片地址的远程图片并保存的方法
- C# WebBrowser的DrawToBitmap方法 截取网页保存为图片
- VC++实现View内容保存为图片的方法
- FireMonkey 保存图片到JPG的方法 BMP转JPG
- 截取部分视图作为图片,保存图片到相册
- web中,如何读取Word内容(包含表格,但不包含图片)并且显示在页面中或保存进数据库中
- php采集内容中带有图片地址的远程图片并保存的方法
- 【框架-MFC】MFC下截取窗口特定部分并保存为图片
- iphone(objective-c)相当于j2me的drawRegion方法 截取部分图片显示到指定位置
- 用Cocoa截取屏幕内容并保存为图片(z)
- MFC下截取窗口特定部分并保存为bmp图片的类