How to convert WPF Control to XPS document via FixedDoc

186 Views Asked by At

I am building a test report in XAML that needs to be exported into an XPS file. This is the code I have below, however I'm running into an "Specified element is already the logical child of another element. Disconnect it first." error when running fixedPage.Children.Add(TestReportXAML); TestReportXAML is the name of my Grid in the Xaml.cs file

FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();

fixedDoc.DocumentPaginator.PageSize = new Size(96 * 11, 96 * 8.5); // LANDSCAPE
fixedPage.Width = 11 * 96;
fixedPage.Height = 8.5 * 96;

fixedPage.Children.Add(TestReportXAML);
((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);

XpsDocument xpsDoc = new XpsDocument(pathFileNameXPS, FileAccess.ReadWrite);
XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
xpsWriter.Write(fixedDoc);
xpsDoc.Close();

Initially I had this set of code that worked, however the problem with this is that my XAML gets saved in portrait mode. I don't know if there's a proper way to make this landscape so I'm trying to use a fixed document to convert the XAML orientation before exporting it to XPS.

XpsDocument xpsDoc = new XpsDocument(pathFileNameXPS, FileAccess.ReadWrite);
XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
xpsWriter.Write(TestReportXAML);
xpsDoc.Close();

I am a new developer and new to XAML/WPF, appreciate if things could be explained in less technical terms. Thanks

0

There are 0 best solutions below