Search code examples
wpfmergelandscapexps

Merge XPS landscape orientation


I've implemented this solution and it worked for me: Can multiple xps documents be merged to one in WPF?

My problem is that the pages I want to merge are in landscape orientation. When the ContainerVisual is added it creates by default a page in vertical orientation. How can I change the orientation to ContainerVisual?

private void AddXPSDocument(string sourceDocument, SerializerWriterCollator vxpsd)
    {
        XpsDocument xpsOld = new XpsDocument(sourceDocument, FileAccess.Read);
        FixedDocumentSequence seqOld = xpsOld.GetFixedDocumentSequence();
        foreach (DocumentReference r in seqOld.References)
        {
            FixedDocument d = r.GetDocument(false);
            foreach (PageContent pc in d.Pages)
            {
                FixedPage fixedPage = pc.GetPageRoot(false);
                double width = fixedPage.Width;
                double height = fixedPage.Height;

                Size sz = new Size(width, height);
                fixedPage.Width = width;
                fixedPage.Height = height;
                fixedPage.Measure(sz);

                fixedPage.Arrange(new Rect(new Point(), sz));


                //fixedPage.UpdateLayout();

                ContainerVisual newPage = new ContainerVisual();
                newPage.Children.Add(fixedPage);

                vxpsd.Write(newPage);

            }
        }
        xpsOld.Close();
    }

Solution

  • You need to add a RotateTransform to the page visual.

    Visual originalPage = Paginator.GetPage(pageNumber).Visual;
    
    var pageContentVisual = new ContainerVisual();
    
    TransformGroup group = new TransformGroup();
    group.Children.Add(new RotateTransform { Angle = 90.0 });
    
    pageContentVisual.Transform = group;
    pageContentVisual.Children.Add(originalPage);
    

    Note: The above was copied from a custom DocumentPaginator however you should be able to apply it your situation.