Python程序教程

您现在的位置是:首页 >  Python

当前栏目

python解析xps文件_xps文件的基本操作

xps,文件,python,解析,基本操作
2025-04-01 16:27:53 时间

大家好,又见面了,我是你们的朋友全栈君。

最近一直研究XPS文件,目前已经解决了二进制流转XPS文件、XPS文件转二进流、XPS文件的解析、XPS文件转图片、XPS文件打印等。但是一直没有找到如何向xps文件中插入图片的方法,好烦恼啊!!!!如果那位大神有向xps文件中插入图片的方法请及时联系我谢谢,QQ470163177。本人研究的成果如下,需要的码友可以学习下。

注意:

xps命名空间在 ReachFramework.dll中

using xps2img;引用需要的是.net4.0版本

1.二进制流生成xps文件:

if (File.Exists(AppDomain.CurrentDomain.BaseDirectory+”yuan.xps”))

{

File.Delete(AppDomain.CurrentDomain.BaseDirectory + “yuan.xps”);

}

string xpsTempFilePath = string.Format(“{0}” + “yuan.xps”, AppDomain.CurrentDomain.BaseDirectory);

using (BinaryWriter writer = new System.IO.BinaryWriter(File.OpenWrite(xpsTempFilePath)))

{

writer.Write(文件);

writer.Flush();

}

2.XPS文件转二进流:

try

{

string strPath = AppDomain.CurrentDomain.BaseDirectory + “DW1031221.xps”;

byte[] xpsBytes = File.ReadAllBytes(strPath);

string sql2 = “insert into 打印任务文件(任务id,文件内容) values((select case when max(任务id) IS NULL then ‘1’ else max(任务id)+1 end from 打印任务文件),@wjnr)”;

SqlParameter param = new SqlParameter(“wjnr”, xpsBytes);

DBHelper.ExecuteNonQuery(sql2, CommandType.Text, param);

System.Windows.MessageBox.Show(“转换成功!”);

}

catch (Exception ex)

{

System.Windows.MessageBox.Show(“转换失败!”);

}

3.XPS文件的解析:

//打开待识别的XPS文件

StringBuilder sb = new StringBuilder();

XpsDocument xpsDocument = new System.Windows.Xps.Packaging.XpsDocument(pdffile, FileAccess.Read);

var reader = xpsDocument.FixedDocumentSequenceReader;

foreach (var document in reader.FixedDocuments)

{

foreach (var page in document.FixedPages)

{

XmlReader xrdr = page.XmlReader;

while (xrdr.Read())

{

switch (xrdr.NodeType)

{

case XmlNodeType.Element:

if (xrdr.Name == “Glyphs”)

sb.Append(xrdr[“UnicodeString”]+”\n”);

break;

default: break;

}

}

}

}

//将xps内容赋值给文本框

if (!string.IsNullOrEmpty(sb.ToString()))

{

richTextBox1.Text = sb.ToString();

}

else

{

MessageBox.Show(“该XPS文件无法识别!”);

}

4.XPS文件转图片:

try

{

using (var xpsConverter = new Xps2Image(this.Tag.ToString()))

{

IEnumerable images = xpsConverter.ToBitmap(new Parameters

{

ImageType = ImageType.Png,

//转成图片的大小比例

Dpi = 73

});

foreach (var item in images)

{

pdfp.Image = item;

}

}

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString());

}

注意:需要引用xps2img类库

5.XPS文件打印:

xpsClass.PrintXPS(comboBox1.Text, strPath);

public static class xpsClass

{

///

/// 打印

///

/// 打印机名称

/// 打印文件路径

public static void PrintXPS(string strPrint, string xpsFilePath)

{

PrintQueue defaultPrintQueue = new PrintQueue(new PrintServer(), strPrint);

string filename = Path.GetFileName(xpsFilePath);

String nextFile = xpsFilePath;

try

{

// 打印Xps文件同时提供Xps验证和进展通知

PrintSystemJobInfo xpsPrintJob = defaultPrintQueue.AddJob(filename, nextFile, false);

}

catch (PrintJobException e)

{

Console.WriteLine(e.ToString());

}

}

}

注意:需要添加System.Printing.dll引用

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/136233.html原文链接:https://javaforall.cn