我是靠谱客的博主 深情过客,最近开发中收集的这篇文章主要介绍xps数据怎么导出为txt_WFP: 读取XPS文件或将word、txt文件转化为XPS文件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

读取XPS格式文件或将doc,txt文件转化为XPS文件,效果图如下:

1.XAML页面代码:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="WordReader" Height="500" Width="900">

2.后台CS文件代码:

首先引用Microsoft.Office.Interop.Word.dll;

//具体代码如下↓

using System;

using System.IO;

using System.Linq;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Xps.Packaging;

using System.Xml;

using Microsoft.Office.Interop.Word;

namespace WpfWord

{

///

/// MainWindow.xaml 的交互逻辑

///

public partial class MainWindow : System.Windows.Window

{

#region 全局变量

///

/// 用于存放目录文档各节点OutlineLevel值,并转化为int型

///

int[] array = null;

///

/// 用于存放目录文档各节点OutlineLevel值

///

string[] array1 = null;

///

/// 用于存放目录文档各节点Description值,章节信息

///

string[] arrayName = null;

///

/// 用于存放目录文档各节点OutlineTarget值,页码信息

///

string[] pages = null;

#endregion

public MainWindow()

{

InitializeComponent();

OpenFile(null);

}

///

/// 构造函数

///

/// 文件路径

public MainWindow(string strFilePath)

: this()

{

}

#region 方法

///

/// 读取导航目录

///

private void ReadDoc(XpsDocument xpsDoc)

{

IXpsFixedDocumentSequenceReader docSeq = xpsDoc.FixedDocumentSequenceReader;

IXpsFixedDocumentReader docReader = docSeq.FixedDocuments[0];

XpsStructure xpsStructure = docReader.DocumentStructure;

Stream stream = xpsStructure.GetStream();

XmlDocument doc = new XmlDocument();

doc.Load(stream);

//获取节点列表

XmlNodeList nodeList = doc.ChildNodes.Item(0).FirstChild.FirstChild.ChildNodes;

if (nodeList.Count <= 0)//判断是否存在目录节点

{

//tvTree.Visibility = System.Windows.Visibility.Hidden;

tvTree.Items.Add(new TreeViewItem { Header = "没有导航目录" });

return;

}

tvTree.Visibility = System.Windows.Visibility.Visible;

array = new int[nodeList.Count];

array1 = new string[nodeList.Count];

arrayName = new string[nodeList.Count];

pages = new string[nodeList.Count];

for (int i = 0; i < nodeList.Count; i++)

{

array[i] = Convert.ToInt32(nodeList[i].Attributes["OutlineLevel"].Value);

array1[i] = nodeList[i].Attributes["OutlineLevel"].Value.ToString();

arrayName[i] = nodeList[i].Attributes["Description"].Value.ToString();

pages[i] = nodeList[i].Attributes["OutlineTarget"].Value.ToString();

}

for (int i = 0; i < array.Length - 1; i++)

{

//对array进行转换组装成可读的树形结构,通过ASCII值进行增加、转换

array1[0] = "A";

if (array[i + 1] - array[i] == 1)

{

array1[i + 1] = array1[i] + 'A';

}

if (array[i + 1] == array[i])

{

char s = Convert.ToChar(array1[i].Substring((array1[i].Length - 1), 1));

array1[i + 1] = array1[i].Substring(0, array1[i].Length - 1) + (char)(s + 1);

}

if (array[i + 1] < array[i])

{

int m = array[i + 1];

char s = Convert.ToChar(array1[i].Substring(0, m).Substring(m - 1, 1));

array1[i + 1] = array1[i].Substring(0, m - 1) + (char)(s + 1);

}

}

//添加一个节点作为根节点

TreeViewItem parent = new TreeViewItem();

TreeViewItem parent1 = null;

parent.Header = "目录导航";

Boolean flag = false;

for (int i = 0; i < array.Length; i++)

{

if (array[i] == 1)

{

flag = true;

}

if (flag) //如果找到实际根节点,加载树

{

parent1 = new TreeViewItem();

parent1.Header = arrayName[i];

parent1.Tag = array1[i];

parent.Items.Add(parent1);

parent.IsExpanded = true;

parent1.IsExpanded = true;

FillTree(parent1, array1, arrayName);

flag = false;

}

}

tvTree.Items.Clear();

tvTree.Items.Add(parent);

}

///

/// 填充树的方法

///

///

///

///

public void FillTree(TreeViewItem parentItem, string[] str1, string[] str2)

{

string parentID = parentItem.Tag as string;

for (int i = 0; i < str1.Length; i++)

{

if (str1[i].IndexOf(parentID) == 0 && str1[i].Length == (parentID.Length + 1) && str1[i].ElementAt(0).Equals(parentID.ElementAt(0)))

{

TreeViewItem childItem = new TreeViewItem();

childItem.Header = str2[i];

childItem.Tag = str1[i];

parentItem.Items.Add(childItem);

FillTree(childItem, str1, str2);

}

}

}

///

/// 打开文件-如果传入路径为空则在此打开选择文件对话框

///

/// 传入文件全路径

private void OpenFile(string strFilepath)

{

if (string.IsNullOrEmpty(strFilepath))

{

Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();

openFileDialog.DefaultExt = ".doc|.txt|.xps";

openFileDialog.Filter = "*(.xps)|*.xps|Word documents (.doc)|*.doc|Word(2007-2010)(.docx)|*.docx|*(.txt)|*.txt";

Nullable result = openFileDialog.ShowDialog();

strFilepath = openFileDialog.FileName;

if (result != true)

{

return;

}

}

this.Title = strFilepath.Substring(strFilepath.LastIndexOf("\")+1);

if (strFilepath.Length > 0)

{

XpsDocument xpsDoc = null;

//如果是xps文件直接打开,否则需转换格式

if (!strFilepath.EndsWith(".xps"))

{

string newXPSdocName = String.Concat(System.IO.Path.GetDirectoryName(strFilepath), "\", System.IO.Path.GetFileNameWithoutExtension(strFilepath), ".xps");

xpsDoc = ConvertWordToXPS(strFilepath, newXPSdocName);

}

else

{

xpsDoc = new XpsDocument(strFilepath, System.IO.FileAccess.Read);

}

if (xpsDoc != null)

{

dvShow.Document = xpsDoc.GetFixedDocumentSequence();

//读取文档目录

ReadDoc(xpsDoc);

xpsDoc.Close();

}

this.lblCurPage.Content = 0;

this.lblPage.Content = "/" + dvShow.PageCount;

}

}

///

/// 将word文档转换为xps文档

///

/// word文档全路径

/// xps文档全路径

///

private XpsDocument ConvertWordToXPS(string wordDocName, string xpsDocName)

{

XpsDocument result = null;

//创建一个word文档,并将要转换的文档添加到新创建的对象

Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();

try

{

wordApplication.Documents.Add(wordDocName);

Document doc = wordApplication.ActiveDocument;

doc.ExportAsFixedFormat(xpsDocName, WdExportFormat.wdExportFormatXPS, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0, WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateHeadingBookmarks, true, true, false, Type.Missing);

result = new XpsDocument(xpsDocName, System.IO.FileAccess.ReadWrite);

}

catch (Exception ex)

{

string error = ex.Message;

wordApplication.Quit(WdSaveOptions.wdDoNotSaveChanges);

}

wordApplication.Quit(WdSaveOptions.wdDoNotSaveChanges);

return result;

}

#endregion

///

/// 导航树跳转事件

///

///

///

private void tvTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs e)

{

int x = 0;

TreeViewItem selectTV = this.tvTree.SelectedItem as TreeViewItem;

if (null == selectTV)

return;

if (null == selectTV.Tag)

return;

string page = selectTV.Tag.ToString();

for (int i = 0; i < array1.Length; i++)

{

if (array1[i].Equals(page))

{

x = i;

}

}

string[] strPages = pages[x].Split('_');

dvShow.GoToPage(Int32.Parse(strPages[1]));

}

private void cbNav_Click(object sender, RoutedEventArgs e)

{

this.cdTree.Width = this.cbNav.IsChecked == true ? new GridLength(300) : new GridLength(0);

}

private void btnPrev_Click(object sender, RoutedEventArgs e)

{

this.dvShow.PreviousPage();

}

private void btnNext_Click(object sender, RoutedEventArgs e)

{

this.dvShow.NextPage();

}

}

}

最后

以上就是深情过客为你收集整理的xps数据怎么导出为txt_WFP: 读取XPS文件或将word、txt文件转化为XPS文件的全部内容,希望文章能够帮你解决xps数据怎么导出为txt_WFP: 读取XPS文件或将word、txt文件转化为XPS文件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部