关于c#打开一个文件,并且指定打开第几页,应该怎么实现呢?
比如我现在浏览打开一个word文档,我怎么指定它直接打开第几页呢?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace 如何打开各种文件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\";//注意这里写路径时要用c:\\而不是c:\
openFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog.FileName;
}
}
private void button1_Click(object sender, EventArgs e)
{
//定义一个ProcessStartInfo实例
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
//设置启动进程的初始目录
info.WorkingDirectory = Application.StartupPath;
//设置启动进程的应用程序或文档名
info.FileName = @textBox1.Text;
//设置启动进程的参数
info.Arguments = "";
//启动由包含进程启动信息的进程资源
try
{
//我在这里是应该怎么指定文件打开第几页呢?
System.Diagnostics.Process.Start(info);
}
catch (System.ComponentModel.Win32Exception we)
{
MessageBox.Show(this, we.Message);
return;
}
}
}
}
[解决办法]
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MSWord.Application app = new MSWord.Application();
MSWord.Document doc = app.Documents.Open(@"F:\桌面\20130322\新建文件夹\FinalProject\Word.docx");
NextPage(doc, 1);//向后跳转一页
string text = doc.ActiveWindow.Selection.Paragraphs[1].Range.Text.ToString();
PreviousPage(doc, 1);//向前跳转一页
string text1 = doc.ActiveWindow.Selection.Paragraphs[1].Range.Text.ToString();
}
private static void PreviousPage(MSWord.Document doc, int Count)
{
for (int i = 0; i < Count; i++)
{
Microsoft.Office.Interop.Word.WdGoToItem goPage = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
doc.ActiveWindow.Selection.GoToPrevious(goPage);
}
}
private static void NextPage(MSWord.Document doc, int Count)
{
for (int i = 0; i < Count; i++)
{
Microsoft.Office.Interop.Word.WdGoToItem goPage = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
doc.ActiveWindow.Selection.GoToNext(goPage);
}
}
}