c# .net xps xpsdocument

c# - ¿Cómo crear un documento XPS?



.net xpsdocument (3)

Me gustaría crear un documento XPS para almacenar e imprimir.

¿Cuál es la forma más fácil de crear un documento XPS (por ejemplo, con una cuadrícula simple con algunos datos dentro) en mi programa, y ​​pasarlo?


Todo lo que es, es realmente XML. Si se siente cómodo trabajando con archivos XML, no debería tener problemas para trabajar con documentos XPS. Aquí hay un tutorial simple que he usado en el pasado para comenzar:

http://blogs.ocrasoft.nl/jeroenveurink/?p=21


Nada fácil al respecto. Pero puede hacerse. Tengo algunos (por desgracia, aún con errores) código de muestra e información en mi blog para crear el documento en la memoria.

Aquí hay un código que preparé para las pruebas que encapsula todo (escribe una colección de páginas fijas en un documento XPS en la memoria). Incluye código para serializar el documento en una matriz de bytes, pero puede omitir esa parte y simplemente devolver el documento:

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; }

Tenga en cuenta el código de enhebrado de rosca. No puedes hacer esto en los hilos de MTA; si estás en un hilo STA, puedes deshacerte de eso también.


Si está trabajando en .NET (v2 o posterior), puede generar fácilmente un documento XPS válido a partir de una imagen visual de WPF.

Por ejemplo, eche un vistazo a esta publicación de blog mía:

http://nixps.blogspot.com/2008/12/wpf-to-pdf.html

En el ejemplo, creo un visual WPF y lo convierto como un archivo XPS, antes de seguir procesando.

Si no está trabajando en .NET, o quiere más control en la salida de XPS, le aconsejo que use una biblioteca (como el SDK de NiXPS ) para esto. Es mucho más fácil de codificar y mucho menos propenso a errores que escribir las construcciones XML usted mismo (y hacer una gestión de recursos adecuada, etc.).