简体   繁体   中英

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). 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.

Note: One of the stack panel name is '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. With the above code, I could able to serialize any one of the stackpanel to xps and get print from it.

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

Finally lots of digging later found out a solution.

In WPF Application, we can serialize multiple xaml elements individually. All the serialized xaml elements will be in .xps format. Then we can collect back all .xps file and form a single .xps file. This way, we can serialize multiple xaml elements. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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