locked
Exception "FixedPage cannot contain another FixedPage" when printing to the XPS Document Writer RRS feed

  • Question

  • Hello,

    we are currently developing a WPF based application with printing. To be able to print page ranges insted of a whole document we use code which is similar to the code posted by Thomas Claudius Huber on his blog :

    http://www.thomasclaudiushuber.com/blog/2009/11/24/wpf-printing-how-to-print-a-pagerange-with-wpfs-printdialog-that-means-the-user-can-select-specific-pages-and-only-these-pages-are-printed
    /

    The program works perfectly with other printers (real printers and e.g. PDFCreator). Only with the XPS Document Writer to following exception is thrown:

    System.Windows.Xps.XpsSerializationException was unhandled
    Message=”FixedPage cannot contain another FixedPage.”
    Source=”ReachFramework”
    StackTrace:
    at System.Windows.Xps.Serialization.XpsSerializationManager.RegisterPageStart()
    at System.Windows.Xps.Serialization.FixedPageSerializer.PersistObjectData(SerializableObjectContext serializableObjectContext)
    at System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(Object serializedObject)
    at System.Windows.Xps.Serialization.FixedPageSerializer.SerializeObject(Object serializedObject)
    at System.Windows.Xps.Serialization.DocumentPageSerializer.SerializeChild(Visual child, SerializableObjectContext parentContext)
    at System.Windows.Xps.Serialization.DocumentPageSerializer.PersistObjectData(SerializableObjectContext serializableObjectContext)
    at System.Windows.Xps.Serialization.ReachSerializer.SerializeObject(Object serializedObject)
    at System.Windows.Xps.Serialization.DocumentPageSerializer.SerializeObject(Object serializedObject)
    at System.Windows.Xps.Serialization.DocumentPaginatorSerializer.PersistObjectData(SerializableObjectContext serializableObjectContext)
    at System.Windows.Xps.Serialization.DocumentPaginatorSerializer.SerializeObject(Object serializedObject)
    at System.Windows.Xps.Serialization.XpsSerializationManager.SaveAsXaml(Object serializedObject)
    at System.Windows.Xps.XpsDocumentWriter.SaveAsXaml(Object serializedObject, Boolean isSync)
    at System.Windows.Xps.XpsDocumentWriter.Write(DocumentPaginator documentPaginator)
    at System.Windows.Controls.PrintDialog.PrintDocument(DocumentPaginator documentPaginator, String description)
    at ThomasClaudiusHuber.PrintRangeExample.MainWindow.PrintButtonClick(Object sender, RoutedEventArgs e) in D:\work\temp\printrange\MainWindow.xaml.cs:line 62

    ...

    This problem also occurs on different OS (tested with WinXP and Win 7) and also with Beta2 of VS2010(.NET 4.0).

    Thank you for any help with this,

    Marco
    Wednesday, January 13, 2010 6:58 AM

All replies

  • Hello Marco,

    Can is this happening with some specific documents or all documents? Do you have a sample doc for repro this issue?

    Thanks,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    Thursday, January 14, 2010 2:37 PM
  • There is a whole sample solution on his blog (including a document). It seems to happen with all documents because in our application we use a complete different document content compated with his example. For your convenience here is the direct link of the example solution:

    http://www.thomasclaudiushuber.com/dotnet/20091124_ThomasClaudiusHuber_PrintRangeExample.zip

    Thanks,

    Marco
    Friday, January 15, 2010 6:51 AM
  • Thank you Marco, I'll take a look and get back to you.

    Regards,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    Friday, January 15, 2010 9:16 AM
  • Any news about the reason for this exception ?

    Tanks,
    Marco
    Friday, January 22, 2010 10:49 AM
  • The problem still exists and becomes a litte bit urgent. Can anyone help ?

    Many thanks in advance,

    Marco
    Friday, January 29, 2010 2:59 PM
  • Hello!

    I meet this problem to.

    Do you find a sollution for this problem?

    Thursday, September 8, 2011 1:59 PM
  • Hi all,

    I know it's a long time but I'll put here for others that are looking for an answer about this problem.

    The simplest solution that I've found is as follow.I haven't fully tested it, so use it on your own risk. Change the overrided method GetPage in PageRangeDocumentPaginator class created by Thomas Claudius Huber as follow:

     

    public override DocumentPage GetPage(int pageNumber)
    {
        var fixedPage = (FixedPage)_paginator.GetPage(pageNumber + _startIndex).Visual;
    
        // Create a new ContainerVisual as a new parent for page children
        var cv = new ContainerVisual();
        foreach (var child in fixedPage.Children)
        {
            // Make a shallow clone of the child using reflection
            var childClone = (UIElement)child.GetType().GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(child, null);
    
            // Setting the parent of the cloned child to the created ContainerVisual by using Reflection.
            // WARNING: If we use Add and Remove methods on the FixedPage.Children, for some reason it will
            //          throw an exception concerning event handlers after the printing job has finished.
            var parentField = childClone.GetType().GetField("_parent", BindingFlags.Instance | BindingFlags.NonPublic);
            if (parentField != null)
            {
                parentField.SetValue(childClone, null);
                cv.Children.Add(childClone);
            }
        }
    
        return new DocumentPage(cv);
    }
    

    UPDATE #1

    The above solution has problem with the page size when printing! It seems that I had forgotten about those properties on DocumentPage! And also I've forgotton to check whether the page.Visual is a FixedPage. :) So change it to:

    public override DocumentPage GetPage(int pageNumber)
    {
        var page = _paginator.GetPage(pageNumber + _startIndex);
    
        // Create a new ContainerVisual as a new parent for page children
        var cv = new ContainerVisual();
        if (page.Visual is FixedPage)
        {
            foreach (var child in ((FixedPage) page.Visual).Children)
            {
                // Make a shallow clone of the child using reflection
                var childClone = (UIElement)child.GetType().GetMethod("MemberwiseClone", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(child, null);
    
                // Setting the parent of the cloned child to the created ContainerVisual by using Reflection.
                // WARNING: If we use Add and Remove methods on the FixedPage.Children, for some reason it will
                //          throw an exception concerning event handlers after the printing job has finished.
                var parentField = childClone.GetType().GetField("_parent",
                                                                BindingFlags.Instance | BindingFlags.NonPublic);
                if (parentField != null)
                {
                    parentField.SetValue(childClone, null);
                    cv.Children.Add(childClone);
                }
            }
    
            return new DocumentPage(cv, page.Size, page.BleedBox, page.ContentBox);
        }
    
        return page;
    }

    Please report here if you have found any problems with this solution, or you have found a better solution. :)

    Hosein





    • Edited by H.Alipourian Saturday, December 24, 2011 4:03 PM
    • Proposed as answer by Sargsyan.Sevak Thursday, January 28, 2016 9:28 AM
    Saturday, December 24, 2011 2:17 PM