C# 如何创建 XPS 文档?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/352540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an XPS document?
提问by Sam
I'd like to create an XPS document for storing and printing.
我想创建一个用于存储和打印的 XPS 文档。
What is the easiest way to create an XPS document (for example with a simple grid with some data inside) in my program, and to pass it around?
在我的程序中创建 XPS 文档(例如带有一些数据的简单网格)并将其传递的最简单方法是什么?
采纳答案by BFree
Nothing easy about it. But it can be done. I've got some (sadly, still buggy) sample code and information on my blog for creating the document in memory.
没有什么容易的。但这是可以完成的。我在我的博客上有一些(遗憾的是,仍然有问题)示例代码和信息,用于在内存中创建文档。
Here's some code I whipped up for testing that encapsulates everything (it writes a collection of FixedPages to an XPS document in memory). It includes code for serializing the document to a byte array, but you can skip that part and just return the document:
这是我为测试而编写的一些代码,它们封装了所有内容(它将 FixedPage 集合写入内存中的 XPS 文档)。它包含用于将文档序列化为字节数组的代码,但您可以跳过该部分并只返回文档:
public static byte[] ToXpsDocument(IEnumerable<FixedPage> pages)
{
// XPS DOCUMENTS MUST BE CREATED ON STA THREADS!!!
// Note, this is test code, so I don't care about disposing my memory streams
// You'll have to pay more attention to their lifespan. You might have to
// serialize the xps document and remove the package from the package store
// before disposing the stream in order to prevent throwing exceptions
byte[] retval = null;
Thread t = new Thread(new ThreadStart(() =>
{
// A memory stream backs our document
MemoryStream ms = new MemoryStream(2048);
// a package contains all parts of the document
Package p = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
// the package store manages packages
Uri u = new Uri("pack://TemporaryPackageUri.xps");
PackageStore.AddPackage(u, p);
// the document uses our package for storage
XpsDocument doc = new XpsDocument(p, CompressionOption.NotCompressed, u.AbsoluteUri);
// An xps document is one or more FixedDocuments containing FixedPages
FixedDocument fDoc = new FixedDocument();
PageContent pc;
foreach (var fp in pages)
{
// this part of the framework is weak and hopefully will be fixed in 4.0
pc = new PageContent();
((IAddChild)pc).AddChild(fp);
fDoc.Pages.Add(pc);
}
// we use the writer to write the fixed document to the xps document
XpsDocumentWriter writer;
writer = XpsDocument.CreateXpsDocumentWriter(doc);
// The paginator controls page breaks during the writing process
// its important since xps document content does not flow
writer.Write(fDoc.DocumentPaginator);
//
p.Flush();
// this part serializes the doc to a stream so we can get the bytes
ms = new MemoryStream();
var writer = new XpsSerializerFactory().CreateSerializerWriter(ms);
writer.Write(doc.GetFixedDocumentSequence());
retval = ms.ToArray();
}));
// Instantiating WPF controls on a MTA thread throws exceptions
t.SetApartmentState(ApartmentState.STA);
// adjust as needed
t.Priority = ThreadPriority.AboveNormal;
t.IsBackground = false;
t.Start();
//~five seconds to finish or we bail
int milli = 0;
while (buffer == null && milli++ < 5000)
Thread.Sleep(1);
//Ditch the thread
if(t.IsAlive)
t.Abort();
// If we time out, we return null.
return retval;
}
Note the crappy threading code. You can't do this on MTA threads; if you are on an STA thread you can get rid of that as well.
注意糟糕的线程代码。您不能在 MTA 线程上执行此操作;如果您在 STA 线程上,您也可以摆脱它。
回答by BFree
All it is, is really XML. If you're comfortable working with XML files, you should have no problem working with XPS documents. Here's a simple tutorial I've used in the past to get me started:
它就是真正的 XML。如果您习惯于处理 XML 文件,那么处理 XPS 文档应该没有问题。这是我过去使用的一个简单教程,可帮助我入门:
回答by nixps
If you are working in .NET (v2 or later), you can very easily generate a valid XPS document from a WPF visual.
如果您使用 .NET(v2 或更高版本),则可以非常轻松地从 WPF 视觉对象生成有效的 XPS 文档。
For an example, take a look at this blog post of mine:
例如,看看我的这篇博文:
http://nixps.blogspot.com/2008/12/wpf-to-pdf.html
http://nixps.blogspot.com/2008/12/wpf-to-pdf.html
In the example I create a WPF visual and convert it as an XPS file, before doing further processing.
在示例中,我创建了一个 WPF 视觉对象并将其转换为 XPS 文件,然后再进行进一步处理。
If you are not working in .NET, or want more control on the XPS output, then I would advise you to use a library (like the NiXPS SDK) for this. It is a lot easier to code for, and a lot less error prone than writing out the XML constructs yourself (and doing proper resource management, etc...).
如果您不在 .NET 中工作,或者想要对 XPS 输出进行更多控制,那么我建议您为此使用一个库(如NiXPS SDK)。与自己编写 XML 构造(并进行适当的资源管理等)相比,它更容易编写代码,并且更不容易出错。