简体   繁体   English

如何将多个xaml元素序列化为单个xps文件并打印WPF应用程序c#

[英]How to serialize multiple xaml elements into single xps file and print WPF Application c#

Its been very very long time could not able to figure out a solution yet. 它已经很长一段时间无法找到解决方案了。

My application contains ten stack panels with formatted text in it. 我的应用程序包含十个带有格式化文本的堆栈面板。 Each stack panel has height 800 and width 500. (Just imagine ten pages with text in wordpad application). 每个堆栈面板的高度为800,宽度为500。(假设在写字板应用程序中有十个页面带有文本)。 My main goal is to print stack panel content. 我的主要目标是打印堆栈面板内容。 Each stack panel content should appear exactly on the printed page. 每个堆栈面板的内容应准确显示在打印页面上。 There are ten stack panels so the final print document should also contain ten pages. 有十个堆叠面板,因此最终的打印文档也应包含十页。

So far, I could able to serialize any one of the stack panel in xps file and print it. 到目前为止,我可以序列化xps文件中的任何堆栈面板并进行打印。

Note: One of the stack panel name is 'spanel0' 注意:堆栈面板名称之一是“ spanel0”

My code: 我的代码:

private void PrintDocument() {
            PrintDialog pd = new PrintDialog();
            FrameworkElement fe = (spanel0 as FrameworkElement);
            fe.Measure(new Size(Int32.MaxValue, Int32.MaxValue));
            Size visualSize = fe.DesiredSize;
            fe.Arrange(new Rect(new Point(0, 0), visualSize));
            MemoryStream stream = new MemoryStream();
            string pack = "pack://temp.xps";
            Uri uri = new Uri(pack);
            DocumentPaginator paginator;
            XpsDocument xpsDoc;

            using (Package container = Package.Open(stream, FileMode.Create))
            {
                PackageStore.AddPackage(uri, container);
                using (xpsDoc = new XpsDocument(container, CompressionOption.Fast, pack))
                {
                    XpsSerializationManager rsm =
                      new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
                    rsm.SaveAsXaml(spanel0);
                    paginator = ((IDocumentPaginatorSource)
                      xpsDoc.GetFixedDocumentSequence()).DocumentPaginator;
                    paginator.PageSize = visualSize;

                }
                PackageStore.RemovePackage(uri);
            }
        }

Where is the problem? 问题出在哪儿?

I want to serialize all the stack panels to xps and so that when I print the xps document I would get ten page pdf file. 我想将所有堆栈面板序列化为xps,这样当我打印xps文档时,我将得到十页的pdf文件。 With the above code, I could able to serialize any one of the stackpanel to xps and get print from it. 使用上面的代码,我可以将任意一个stackpanel序列化为xps并从中获取打印信息。

Hope you have understood my issue. 希望你理解我的问题。 Please guide me. 请指导我。 Thanks in Advance. 提前致谢。

Note: my rest of stack panel are spanel1, spanel2, spanel3, spanel4, spanel5 and so on 注意:我其余的堆栈面板是spanel1,spanel2,spanel3,spanel4,spanel5等

Finally lots of digging later found out a solution. 最终,大量的挖掘工作后来找到了解决方案。

In WPF Application, we can serialize multiple xaml elements individually. 在WPF应用程序中,我们可以分别序列化多个xaml元素。 All the serialized xaml elements will be in .xps format. 所有序列化的xaml元素都将为.xps格式。 Then we can collect back all .xps file and form a single .xps file. 然后,我们可以收集回所有.xps文件并形成一个.xps文件。 This way, we can serialize multiple xaml elements. 这样,我们可以序列化多个xaml元素。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM